2 * Copyright (C) 2006-2010 Brian Aker
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
8 * Summary: Server IO, Not public!
16 static memcached_return_t
connect_poll(memcached_server_st
*ptr
)
20 fds
[0].events
= POLLOUT
;
22 int timeout
= ptr
->root
->connect_timeout
;
23 if (ptr
->root
->flags
.no_block
== true)
29 while (--loop_max
) // Should only loop on cases of ERESTART or EINTR
31 error
= poll(fds
, 1, timeout
);
38 socklen_t len
= sizeof (err
);
39 (void)getsockopt(ptr
->fd
, SOL_SOCKET
, SO_ERROR
, &err
, &len
);
41 // We check the value to see what happened wth the socket.
44 return MEMCACHED_SUCCESS
;
48 ptr
->cached_errno
= errno
;
50 return MEMCACHED_ERRNO
;
54 return MEMCACHED_TIMEOUT
;
55 default: // A real error occurred and we need to completely bail
56 WATCHPOINT_ERRNO(get_socket_errno());
57 switch (get_socket_errno())
59 #ifdef TARGET_OS_LINUX
65 if (fds
[0].revents
& POLLERR
)
68 socklen_t len
= sizeof (err
);
69 (void)getsockopt(ptr
->fd
, SOL_SOCKET
, SO_ERROR
, &err
, &len
);
70 ptr
->cached_errno
= (err
== 0) ? get_socket_errno() : err
;
74 ptr
->cached_errno
= get_socket_errno();
77 (void)closesocket(ptr
->fd
);
78 ptr
->fd
= INVALID_SOCKET
;
80 return MEMCACHED_ERRNO
;
85 // This should only be possible from ERESTART or EINTR;
86 ptr
->cached_errno
= get_socket_errno();
88 return MEMCACHED_ERRNO
;
91 static memcached_return_t
set_hostinfo(memcached_server_st
*server
)
94 struct addrinfo hints
;
95 char str_port
[NI_MAXSERV
];
98 snprintf(str_port
, NI_MAXSERV
, "%u", (uint32_t)server
->port
);
100 memset(&hints
, 0, sizeof(hints
));
102 // hints.ai_family= AF_INET;
103 if (server
->type
== MEMCACHED_CONNECTION_UDP
)
105 hints
.ai_protocol
= IPPROTO_UDP
;
106 hints
.ai_socktype
= SOCK_DGRAM
;
110 hints
.ai_socktype
= SOCK_STREAM
;
111 hints
.ai_protocol
= IPPROTO_TCP
;
116 int e
= getaddrinfo(server
->hostname
, str_port
, &hints
, &ai
);
122 else if (e
== EAI_AGAIN
)
125 struct timespec dream
, rem
;
130 nanosleep(&dream
, &rem
);
136 WATCHPOINT_STRING(server
->hostname
);
137 WATCHPOINT_STRING(gai_strerror(e
));
138 return MEMCACHED_HOST_LOOKUP_FAILURE
;
142 if (server
->address_info
)
144 freeaddrinfo(server
->address_info
);
145 server
->address_info
= NULL
;
147 server
->address_info
= ai
;
149 return MEMCACHED_SUCCESS
;
152 static inline memcached_return_t
set_socket_nonblocking(memcached_server_st
*ptr
)
156 if (ioctlsocket(ptr
->fd
, FIONBIO
, &arg
) == SOCKET_ERROR
)
158 ptr
->cached_errno
= get_socket_errno();
159 return MEMCACHED_CONNECTION_FAILURE
;
165 flags
= fcntl(ptr
->fd
, F_GETFL
, 0);
166 while (flags
== -1 && (errno
== EINTR
|| errno
== EAGAIN
));
168 unlikely (flags
== -1)
170 ptr
->cached_errno
= errno
;
171 return MEMCACHED_CONNECTION_FAILURE
;
173 else if ((flags
& O_NONBLOCK
) == 0)
178 rval
= fcntl(ptr
->fd
, F_SETFL
, flags
| O_NONBLOCK
);
179 while (rval
== -1 && (errno
== EINTR
|| errno
== EAGAIN
));
181 unlikely (rval
== -1)
183 ptr
->cached_errno
= errno
;
184 return MEMCACHED_CONNECTION_FAILURE
;
188 return MEMCACHED_SUCCESS
;
191 static memcached_return_t
set_socket_options(memcached_server_st
*ptr
)
193 WATCHPOINT_ASSERT(ptr
->fd
!= -1);
195 if (ptr
->type
== MEMCACHED_CONNECTION_UDP
)
196 return MEMCACHED_SUCCESS
;
199 if (ptr
->root
->snd_timeout
)
202 struct timeval waittime
;
205 waittime
.tv_usec
= ptr
->root
->snd_timeout
;
207 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_SNDTIMEO
,
208 &waittime
, (socklen_t
)sizeof(struct timeval
));
209 WATCHPOINT_ASSERT(error
== 0);
211 return MEMCACHED_FAILURE
;
216 if (ptr
->root
->rcv_timeout
)
219 struct timeval waittime
;
222 waittime
.tv_usec
= ptr
->root
->rcv_timeout
;
224 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_RCVTIMEO
,
225 &waittime
, (socklen_t
)sizeof(struct timeval
));
226 WATCHPOINT_ASSERT(error
== 0);
228 return MEMCACHED_FAILURE
;
233 #if defined(__MACH__) && defined(__APPLE__) || defined(__FreeBSD__)
236 int error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_NOSIGPIPE
, (void *)&set
, sizeof(int));
238 // This is not considered a fatal error
241 WATCHPOINT_ERRNO(get_socket_errno());
242 perror("setsockopt(SO_NOSIGPIPE)");
247 if (ptr
->root
->flags
.no_block
)
250 struct linger linger
;
253 linger
.l_linger
= 0; /* By default on close() just drop the socket */
254 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_LINGER
,
255 &linger
, (socklen_t
)sizeof(struct linger
));
256 WATCHPOINT_ASSERT(error
== 0);
258 return MEMCACHED_FAILURE
;
261 if (ptr
->root
->flags
.tcp_nodelay
)
266 error
= setsockopt(ptr
->fd
, IPPROTO_TCP
, TCP_NODELAY
,
267 &flag
, (socklen_t
)sizeof(int));
268 WATCHPOINT_ASSERT(error
== 0);
270 return MEMCACHED_FAILURE
;
273 if (ptr
->root
->flags
.tcp_keepalive
)
278 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_KEEPALIVE
,
279 &flag
, (socklen_t
)sizeof(int));
280 WATCHPOINT_ASSERT(error
== 0);
282 return MEMCACHED_FAILURE
;
286 if (ptr
->root
->tcp_keepidle
> 0)
290 error
= setsockopt(ptr
->fd
, IPPROTO_TCP
, TCP_KEEPIDLE
,
291 &ptr
->root
->tcp_keepidle
, (socklen_t
)sizeof(int));
292 WATCHPOINT_ASSERT(error
== 0);
294 return MEMCACHED_FAILURE
;
298 if (ptr
->root
->send_size
> 0)
302 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_SNDBUF
,
303 &ptr
->root
->send_size
, (socklen_t
)sizeof(int));
304 WATCHPOINT_ASSERT(error
== 0);
306 return MEMCACHED_FAILURE
;
309 if (ptr
->root
->recv_size
> 0)
313 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_RCVBUF
,
314 &ptr
->root
->recv_size
, (socklen_t
)sizeof(int));
315 WATCHPOINT_ASSERT(error
== 0);
317 return MEMCACHED_FAILURE
;
321 /* libmemcached will always use nonblocking IO to avoid write deadlocks */
322 return set_socket_nonblocking(ptr
);
325 static memcached_return_t
unix_socket_connect(memcached_server_st
*ptr
)
328 struct sockaddr_un servAddr
;
330 WATCHPOINT_ASSERT(ptr
->fd
== -1);
332 if ((ptr
->fd
= socket(AF_UNIX
, SOCK_STREAM
, 0)) < 0)
334 ptr
->cached_errno
= errno
;
335 return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE
;
338 memset(&servAddr
, 0, sizeof (struct sockaddr_un
));
339 servAddr
.sun_family
= AF_UNIX
;
340 strcpy(servAddr
.sun_path
, ptr
->hostname
); /* Copy filename */
344 (struct sockaddr
*)&servAddr
,
345 sizeof(servAddr
)) < 0)
353 case EISCONN
: /* We were spinning waiting on connect */
356 WATCHPOINT_ERRNO(errno
);
357 ptr
->cached_errno
= errno
;
358 return MEMCACHED_ERRNO
;
362 WATCHPOINT_ASSERT(ptr
->fd
!= -1);
364 return MEMCACHED_SUCCESS
;
367 return MEMCACHED_NOT_SUPPORTED
;
371 static memcached_return_t
network_connect(memcached_server_st
*ptr
)
373 bool timeout_error_occured
= false;
376 WATCHPOINT_ASSERT(ptr
->fd
== INVALID_SOCKET
);
377 WATCHPOINT_ASSERT(ptr
->cursor_active
== 0);
379 if (! ptr
->options
.sockaddr_inited
|| (!(ptr
->root
->flags
.use_cache_lookups
)))
381 memcached_return_t rc
;
383 rc
= set_hostinfo(ptr
);
384 if (rc
!= MEMCACHED_SUCCESS
)
386 ptr
->options
.sockaddr_inited
= true;
389 struct addrinfo
*use
= ptr
->address_info
;
390 /* Create the socket */
393 /* Memcache server does not support IPV6 in udp mode, so skip if not ipv4 */
394 if (ptr
->type
== MEMCACHED_CONNECTION_UDP
&& use
->ai_family
!= AF_INET
)
400 if ((ptr
->fd
= socket(use
->ai_family
,
402 use
->ai_protocol
)) < 0)
404 ptr
->cached_errno
= get_socket_errno();
405 WATCHPOINT_ERRNO(get_socket_errno());
406 return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE
;
409 (void)set_socket_options(ptr
);
411 /* connect to server */
412 if ((connect(ptr
->fd
, use
->ai_addr
, use
->ai_addrlen
) != SOCKET_ERROR
))
417 /* An error occurred */
418 ptr
->cached_errno
= get_socket_errno();
419 if (ptr
->cached_errno
== EWOULDBLOCK
||
420 ptr
->cached_errno
== EINPROGRESS
|| /* nonblocking mode - first return, */
421 ptr
->cached_errno
== EALREADY
) /* nonblocking mode - subsequent returns */
423 memcached_return_t rc
;
424 rc
= connect_poll(ptr
);
426 if (rc
== MEMCACHED_TIMEOUT
)
427 timeout_error_occured
= true;
429 if (rc
== MEMCACHED_SUCCESS
)
432 else if (get_socket_errno() == EISCONN
) /* we are connected :-) */
436 else if (get_socket_errno() == EINTR
) // Special case, we retry ai_addr
438 (void)closesocket(ptr
->fd
);
439 ptr
->fd
= INVALID_SOCKET
;
443 (void)closesocket(ptr
->fd
);
444 ptr
->fd
= INVALID_SOCKET
;
448 if (ptr
->fd
== INVALID_SOCKET
)
450 WATCHPOINT_STRING("Never got a good file descriptor");
452 /* Failed to connect. schedule next retry */
453 if (ptr
->root
->retry_timeout
)
455 struct timeval next_time
;
457 if (gettimeofday(&next_time
, NULL
) == 0)
458 ptr
->next_retry
= next_time
.tv_sec
+ ptr
->root
->retry_timeout
;
461 if (timeout_error_occured
)
462 return MEMCACHED_TIMEOUT
;
464 return MEMCACHED_ERRNO
; /* The last error should be from connect() */
467 return MEMCACHED_SUCCESS
; /* The last error should be from connect() */
470 void set_last_disconnected_host(memcached_server_write_instance_st ptr
)
473 memcached_st
*root
= (memcached_st
*)ptr
->root
;
476 WATCHPOINT_STRING(ptr
->hostname
);
477 WATCHPOINT_NUMBER(ptr
->port
);
478 WATCHPOINT_ERRNO(ptr
->cached_errno
);
480 if (root
->last_disconnected_server
)
481 memcached_server_free(root
->last_disconnected_server
);
482 root
->last_disconnected_server
= memcached_server_clone(NULL
, ptr
);
485 memcached_return_t
memcached_connect(memcached_server_write_instance_st ptr
)
487 memcached_return_t rc
= MEMCACHED_NO_SERVERS
;
489 if (ptr
->fd
!= INVALID_SOCKET
)
490 return MEMCACHED_SUCCESS
;
492 LIBMEMCACHED_MEMCACHED_CONNECT_START();
494 /* both retry_timeout and server_failure_limit must be set in order to delay retrying a server on error. */
495 WATCHPOINT_ASSERT(ptr
->root
);
496 if (ptr
->root
->retry_timeout
&& ptr
->next_retry
)
498 struct timeval curr_time
;
500 gettimeofday(&curr_time
, NULL
);
502 // We should optimize this to remove the allocation if the server was
503 // the last server to die
504 if (ptr
->next_retry
> curr_time
.tv_sec
)
506 set_last_disconnected_host(ptr
);
508 return MEMCACHED_SERVER_MARKED_DEAD
;
512 // If we are over the counter failure, we just fail. Reject host only
513 // works if you have a set number of failures.
514 if (ptr
->root
->server_failure_limit
&& ptr
->server_failure_counter
>= ptr
->root
->server_failure_limit
)
516 set_last_disconnected_host(ptr
);
518 // @todo fix this by fixing behavior to no longer make use of
520 if (_is_auto_eject_host(ptr
->root
))
522 run_distribution((memcached_st
*)ptr
->root
);
525 return MEMCACHED_SERVER_MARKED_DEAD
;
528 /* We need to clean up the multi startup piece */
531 case MEMCACHED_CONNECTION_UNKNOWN
:
532 WATCHPOINT_ASSERT(0);
533 rc
= MEMCACHED_NOT_SUPPORTED
;
535 case MEMCACHED_CONNECTION_UDP
:
536 case MEMCACHED_CONNECTION_TCP
:
537 rc
= network_connect(ptr
);
538 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
539 if (ptr
->fd
!= INVALID_SOCKET
&& ptr
->root
->sasl
.callbacks
)
541 rc
= memcached_sasl_authenticate_connection(ptr
);
542 if (rc
!= MEMCACHED_SUCCESS
)
544 (void)closesocket(ptr
->fd
);
545 ptr
->fd
= INVALID_SOCKET
;
550 case MEMCACHED_CONNECTION_UNIX_SOCKET
:
551 rc
= unix_socket_connect(ptr
);
553 case MEMCACHED_CONNECTION_MAX
:
555 WATCHPOINT_ASSERT(0);
558 if (rc
== MEMCACHED_SUCCESS
)
560 ptr
->server_failure_counter
= 0;
565 ptr
->server_failure_counter
++;
567 set_last_disconnected_host(ptr
);
570 LIBMEMCACHED_MEMCACHED_CONNECT_END();