7 static memcached_return_t
set_hostinfo(memcached_server_st
*server
)
10 struct addrinfo hints
;
11 char str_port
[NI_MAXSERV
];
14 snprintf(str_port
, NI_MAXSERV
, "%u", (uint32_t)server
->port
);
16 memset(&hints
, 0, sizeof(hints
));
18 // hints.ai_family= AF_INET;
19 if (server
->type
== MEMCACHED_CONNECTION_UDP
)
21 hints
.ai_protocol
= IPPROTO_UDP
;
22 hints
.ai_socktype
= SOCK_DGRAM
;
26 hints
.ai_socktype
= SOCK_STREAM
;
27 hints
.ai_protocol
= IPPROTO_TCP
;
32 int e
= getaddrinfo(server
->hostname
, str_port
, &hints
, &ai
);
38 else if (e
== EAI_AGAIN
)
40 struct timespec dream
, rem
;
45 nanosleep(&dream
, &rem
);
51 WATCHPOINT_STRING(server
->hostname
);
52 WATCHPOINT_STRING(gai_strerror(e
));
53 return MEMCACHED_HOST_LOOKUP_FAILURE
;
57 if (server
->address_info
)
59 freeaddrinfo(server
->address_info
);
60 server
->address_info
= NULL
;
62 server
->address_info
= ai
;
64 return MEMCACHED_SUCCESS
;
67 static memcached_return_t
set_socket_options(memcached_server_st
*ptr
)
69 WATCHPOINT_ASSERT(ptr
->fd
!= -1);
71 if (ptr
->type
== MEMCACHED_CONNECTION_UDP
)
72 return MEMCACHED_SUCCESS
;
75 if (ptr
->root
->snd_timeout
)
78 struct timeval waittime
;
81 waittime
.tv_usec
= ptr
->root
->snd_timeout
;
83 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_SNDTIMEO
,
84 &waittime
, (socklen_t
)sizeof(struct timeval
));
85 WATCHPOINT_ASSERT(error
== 0);
87 return MEMCACHED_FAILURE
;
92 if (ptr
->root
->rcv_timeout
)
95 struct timeval waittime
;
98 waittime
.tv_usec
= ptr
->root
->rcv_timeout
;
100 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_RCVTIMEO
,
101 &waittime
, (socklen_t
)sizeof(struct timeval
));
102 WATCHPOINT_ASSERT(error
== 0);
104 return MEMCACHED_FAILURE
;
109 #if defined(__MACH__) && defined(__APPLE__) || defined(__FreeBSD__)
112 int error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_NOSIGPIPE
, (void *)&set
, sizeof(int));
114 // This is not considered a fatal error
117 WATCHPOINT_ERRNO(errno
);
118 perror("setsockopt(SO_NOSIGPIPE)");
123 if (ptr
->root
->flags
.no_block
)
126 struct linger linger
;
129 linger
.l_linger
= 0; /* By default on close() just drop the socket */
130 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_LINGER
,
131 &linger
, (socklen_t
)sizeof(struct linger
));
132 WATCHPOINT_ASSERT(error
== 0);
134 return MEMCACHED_FAILURE
;
137 if (ptr
->root
->flags
.tcp_nodelay
)
142 error
= setsockopt(ptr
->fd
, IPPROTO_TCP
, TCP_NODELAY
,
143 &flag
, (socklen_t
)sizeof(int));
144 WATCHPOINT_ASSERT(error
== 0);
146 return MEMCACHED_FAILURE
;
149 if (ptr
->root
->flags
.tcp_keepalive
)
154 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_KEEPALIVE
,
155 &flag
, (socklen_t
)sizeof(int));
156 WATCHPOINT_ASSERT(error
== 0);
158 return MEMCACHED_FAILURE
;
162 if (ptr
->root
->tcp_keepidle
> 0)
166 error
= setsockopt(ptr
->fd
, IPPROTO_TCP
, TCP_KEEPIDLE
,
167 &ptr
->root
->tcp_keepidle
, (socklen_t
)sizeof(int));
168 WATCHPOINT_ASSERT(error
== 0);
170 return MEMCACHED_FAILURE
;
174 if (ptr
->root
->send_size
> 0)
178 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_SNDBUF
,
179 &ptr
->root
->send_size
, (socklen_t
)sizeof(int));
180 WATCHPOINT_ASSERT(error
== 0);
182 return MEMCACHED_FAILURE
;
185 if (ptr
->root
->recv_size
> 0)
189 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_RCVBUF
,
190 &ptr
->root
->recv_size
, (socklen_t
)sizeof(int));
191 WATCHPOINT_ASSERT(error
== 0);
193 return MEMCACHED_FAILURE
;
196 /* libmemcached will always use nonblocking IO to avoid write deadlocks */
200 flags
= fcntl(ptr
->fd
, F_GETFL
, 0);
201 while (flags
== -1 && (errno
== EINTR
|| errno
== EAGAIN
));
203 unlikely (flags
== -1)
205 return MEMCACHED_CONNECTION_FAILURE
;
207 else if ((flags
& O_NONBLOCK
) == 0)
212 rval
= fcntl(ptr
->fd
, F_SETFL
, flags
| O_NONBLOCK
);
213 while (rval
== -1 && (errno
== EINTR
|| errno
== EAGAIN
));
215 unlikely (rval
== -1)
217 return MEMCACHED_CONNECTION_FAILURE
;
221 return MEMCACHED_SUCCESS
;
224 static memcached_return_t
unix_socket_connect(memcached_server_st
*ptr
)
226 struct sockaddr_un servAddr
;
230 if ((ptr
->fd
= socket(AF_UNIX
, SOCK_STREAM
, 0)) < 0)
232 ptr
->cached_errno
= errno
;
233 return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE
;
236 memset(&servAddr
, 0, sizeof (struct sockaddr_un
));
237 servAddr
.sun_family
= AF_UNIX
;
238 strcpy(servAddr
.sun_path
, ptr
->hostname
); /* Copy filename */
242 (struct sockaddr
*)&servAddr
,
243 sizeof(servAddr
)) < 0)
251 case EISCONN
: /* We were spinning waiting on connect */
254 WATCHPOINT_ERRNO(errno
);
255 ptr
->cached_errno
= errno
;
256 return MEMCACHED_ERRNO
;
261 WATCHPOINT_ASSERT(ptr
->fd
!= -1);
263 return MEMCACHED_SUCCESS
;
266 static memcached_return_t
network_connect(memcached_server_st
*ptr
)
270 struct addrinfo
*use
;
272 WATCHPOINT_ASSERT(ptr
->cursor_active
== 0);
274 if (! ptr
->options
.sockaddr_inited
||
275 (!(ptr
->root
->flags
.use_cache_lookups
)))
277 memcached_return_t rc
;
279 rc
= set_hostinfo(ptr
);
280 if (rc
!= MEMCACHED_SUCCESS
)
282 ptr
->options
.sockaddr_inited
= true;
285 use
= ptr
->address_info
;
286 /* Create the socket */
289 /* Memcache server does not support IPV6 in udp mode, so skip if not ipv4 */
290 if (ptr
->type
== MEMCACHED_CONNECTION_UDP
&& use
->ai_family
!= AF_INET
)
296 if ((ptr
->fd
= socket(use
->ai_family
,
298 use
->ai_protocol
)) < 0)
300 ptr
->cached_errno
= errno
;
301 WATCHPOINT_ERRNO(errno
);
302 return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE
;
305 (void)set_socket_options(ptr
);
307 /* connect to server */
308 if ((connect(ptr
->fd
, use
->ai_addr
, use
->ai_addrlen
) == -1))
310 ptr
->cached_errno
= errno
;
311 if (errno
== EINPROGRESS
|| /* nonblocking mode - first return, */
312 errno
== EALREADY
) /* nonblocking mode - subsequent returns */
314 struct pollfd fds
[1];
316 fds
[0].events
= POLLOUT
;
318 int timeout
= ptr
->root
->connect_timeout
;
319 if (ptr
->root
->flags
.no_block
== true)
325 int error
= poll(fds
, 1, timeout
);
334 return MEMCACHED_TIMEOUT
;
336 // A real error occurred and we need to completely bail
338 WATCHPOINT_ERRNO(errno
);
341 #ifdef TARGET_OS_LINUX
347 if (fds
[0].revents
& POLLERR
)
350 socklen_t len
= sizeof (err
);
351 (void)getsockopt(ptr
->fd
, SOL_SOCKET
, SO_ERROR
, &err
, &len
);
352 ptr
->cached_errno
= (err
== 0) ? errno
: err
;
355 (void)close(ptr
->fd
);
363 else if (errno
== EISCONN
) /* we are connected :-) */
367 else if (errno
!= EINTR
)
369 (void)close(ptr
->fd
);
375 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
376 if (ptr
->fd
!= -1 && ptr
->root
->sasl
.callbacks
!= NULL
)
378 memcached_return rc
= memcached_sasl_authenticate_connection(ptr
);
379 if (rc
!= MEMCACHED_SUCCESS
)
381 (void)close(ptr
->fd
);
392 return MEMCACHED_SUCCESS
;
400 WATCHPOINT_STRING("Never got a good file descriptor");
402 /* Failed to connect. schedule next retry */
403 if (ptr
->root
->retry_timeout
)
405 struct timeval next_time
;
407 if (gettimeofday(&next_time
, NULL
) == 0)
408 ptr
->next_retry
= next_time
.tv_sec
+ ptr
->root
->retry_timeout
;
411 if (ptr
->cached_errno
== 0)
412 return MEMCACHED_TIMEOUT
;
414 return MEMCACHED_ERRNO
; /* The last error should be from connect() */
417 return MEMCACHED_SUCCESS
; /* The last error should be from connect() */
420 void set_last_disconnected_host(memcached_server_write_instance_st ptr
)
423 memcached_st
*root
= (memcached_st
*)ptr
->root
;
426 WATCHPOINT_STRING(ptr
->hostname
);
427 WATCHPOINT_NUMBER(ptr
->port
);
428 WATCHPOINT_ERRNO(ptr
->cached_errno
);
430 if (root
->last_disconnected_server
)
431 memcached_server_free(root
->last_disconnected_server
);
432 root
->last_disconnected_server
= memcached_server_clone(NULL
, ptr
);
435 memcached_return_t
memcached_connect(memcached_server_write_instance_st ptr
)
437 memcached_return_t rc
= MEMCACHED_NO_SERVERS
;
438 LIBMEMCACHED_MEMCACHED_CONNECT_START();
440 /* both retry_timeout and server_failure_limit must be set in order to delay retrying a server on error. */
441 WATCHPOINT_ASSERT(ptr
->root
);
442 if (ptr
->root
->retry_timeout
&& ptr
->next_retry
)
444 struct timeval curr_time
;
446 gettimeofday(&curr_time
, NULL
);
448 // We should optimize this to remove the allocation if the server was
449 // the last server to die
450 if (ptr
->next_retry
> curr_time
.tv_sec
)
452 set_last_disconnected_host(ptr
);
454 return MEMCACHED_SERVER_MARKED_DEAD
;
458 // If we are over the counter failure, we just fail. Reject host only
459 // works if you have a set number of failures.
460 if (ptr
->root
->server_failure_limit
&& ptr
->server_failure_counter
>= ptr
->root
->server_failure_limit
)
462 set_last_disconnected_host(ptr
);
464 // @todo fix this by fixing behavior to no longer make use of
466 if (_is_auto_eject_host(ptr
->root
))
468 run_distribution((memcached_st
*)ptr
->root
);
471 return MEMCACHED_SERVER_MARKED_DEAD
;
474 /* We need to clean up the multi startup piece */
477 case MEMCACHED_CONNECTION_UNKNOWN
:
478 WATCHPOINT_ASSERT(0);
479 rc
= MEMCACHED_NOT_SUPPORTED
;
481 case MEMCACHED_CONNECTION_UDP
:
482 case MEMCACHED_CONNECTION_TCP
:
483 rc
= network_connect(ptr
);
485 case MEMCACHED_CONNECTION_UNIX_SOCKET
:
486 rc
= unix_socket_connect(ptr
);
488 case MEMCACHED_CONNECTION_MAX
:
490 WATCHPOINT_ASSERT(0);
493 if (rc
== MEMCACHED_SUCCESS
)
495 ptr
->server_failure_counter
= 0;
500 ptr
->server_failure_counter
++;
502 set_last_disconnected_host(ptr
);
505 LIBMEMCACHED_MEMCACHED_CONNECT_END();