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!
18 static memcached_return_t
connect_poll(memcached_server_st
*ptr
)
22 fds
[0].events
= POLLOUT
;
24 int timeout
= ptr
->root
->connect_timeout
;
25 if (ptr
->root
->flags
.no_block
== true)
31 while (--loop_max
) // Should only loop on cases of ERESTART or EINTR
33 error
= poll(fds
, 1, timeout
);
40 socklen_t len
= sizeof (err
);
41 (void)getsockopt(ptr
->fd
, SOL_SOCKET
, SO_ERROR
, &err
, &len
);
43 // We check the value to see what happened wth the socket.
46 return MEMCACHED_SUCCESS
;
50 ptr
->cached_errno
= errno
;
52 return MEMCACHED_ERRNO
;
56 return MEMCACHED_TIMEOUT
;
57 default: // A real error occurred and we need to completely bail
58 WATCHPOINT_ERRNO(errno
);
61 #ifdef TARGET_OS_LINUX
67 if (fds
[0].revents
& POLLERR
)
70 socklen_t len
= sizeof (err
);
71 (void)getsockopt(ptr
->fd
, SOL_SOCKET
, SO_ERROR
, &err
, &len
);
72 ptr
->cached_errno
= (err
== 0) ? errno
: err
;
76 ptr
->cached_errno
= errno
;
82 return MEMCACHED_ERRNO
;
85 WATCHPOINT_ASSERT(0); // Programming error
88 // This should only be possible from ERESTART or EINTR;
89 ptr
->cached_errno
= errno
;
91 return MEMCACHED_ERRNO
;
94 static memcached_return_t
set_hostinfo(memcached_server_st
*server
)
97 struct addrinfo hints
;
98 char str_port
[NI_MAXSERV
];
101 snprintf(str_port
, NI_MAXSERV
, "%u", (uint32_t)server
->port
);
103 memset(&hints
, 0, sizeof(hints
));
105 // hints.ai_family= AF_INET;
106 if (server
->type
== MEMCACHED_CONNECTION_UDP
)
108 hints
.ai_protocol
= IPPROTO_UDP
;
109 hints
.ai_socktype
= SOCK_DGRAM
;
113 hints
.ai_socktype
= SOCK_STREAM
;
114 hints
.ai_protocol
= IPPROTO_TCP
;
119 int e
= getaddrinfo(server
->hostname
, str_port
, &hints
, &ai
);
125 else if (e
== EAI_AGAIN
)
127 struct timespec dream
, rem
;
132 nanosleep(&dream
, &rem
);
138 WATCHPOINT_STRING(server
->hostname
);
139 WATCHPOINT_STRING(gai_strerror(e
));
140 return MEMCACHED_HOST_LOOKUP_FAILURE
;
144 if (server
->address_info
)
146 freeaddrinfo(server
->address_info
);
147 server
->address_info
= NULL
;
149 server
->address_info
= ai
;
151 return MEMCACHED_SUCCESS
;
154 static memcached_return_t
set_socket_options(memcached_server_st
*ptr
)
156 WATCHPOINT_ASSERT(ptr
->fd
!= -1);
158 if (ptr
->type
== MEMCACHED_CONNECTION_UDP
)
159 return MEMCACHED_SUCCESS
;
162 if (ptr
->root
->snd_timeout
)
165 struct timeval waittime
;
168 waittime
.tv_usec
= ptr
->root
->snd_timeout
;
170 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_SNDTIMEO
,
171 &waittime
, (socklen_t
)sizeof(struct timeval
));
172 WATCHPOINT_ASSERT(error
== 0);
174 return MEMCACHED_FAILURE
;
179 if (ptr
->root
->rcv_timeout
)
182 struct timeval waittime
;
185 waittime
.tv_usec
= ptr
->root
->rcv_timeout
;
187 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_RCVTIMEO
,
188 &waittime
, (socklen_t
)sizeof(struct timeval
));
189 WATCHPOINT_ASSERT(error
== 0);
191 return MEMCACHED_FAILURE
;
196 #if defined(__MACH__) && defined(__APPLE__) || defined(__FreeBSD__)
199 int error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_NOSIGPIPE
, (void *)&set
, sizeof(int));
201 // This is not considered a fatal error
204 WATCHPOINT_ERRNO(errno
);
205 perror("setsockopt(SO_NOSIGPIPE)");
210 if (ptr
->root
->flags
.no_block
)
213 struct linger linger
;
216 linger
.l_linger
= 0; /* By default on close() just drop the socket */
217 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_LINGER
,
218 &linger
, (socklen_t
)sizeof(struct linger
));
219 WATCHPOINT_ASSERT(error
== 0);
221 return MEMCACHED_FAILURE
;
224 if (ptr
->root
->flags
.tcp_nodelay
)
229 error
= setsockopt(ptr
->fd
, IPPROTO_TCP
, TCP_NODELAY
,
230 &flag
, (socklen_t
)sizeof(int));
231 WATCHPOINT_ASSERT(error
== 0);
233 return MEMCACHED_FAILURE
;
236 if (ptr
->root
->flags
.tcp_keepalive
)
241 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_KEEPALIVE
,
242 &flag
, (socklen_t
)sizeof(int));
243 WATCHPOINT_ASSERT(error
== 0);
245 return MEMCACHED_FAILURE
;
249 if (ptr
->root
->tcp_keepidle
> 0)
253 error
= setsockopt(ptr
->fd
, IPPROTO_TCP
, TCP_KEEPIDLE
,
254 &ptr
->root
->tcp_keepidle
, (socklen_t
)sizeof(int));
255 WATCHPOINT_ASSERT(error
== 0);
257 return MEMCACHED_FAILURE
;
261 if (ptr
->root
->send_size
> 0)
265 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_SNDBUF
,
266 &ptr
->root
->send_size
, (socklen_t
)sizeof(int));
267 WATCHPOINT_ASSERT(error
== 0);
269 return MEMCACHED_FAILURE
;
272 if (ptr
->root
->recv_size
> 0)
276 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_RCVBUF
,
277 &ptr
->root
->recv_size
, (socklen_t
)sizeof(int));
278 WATCHPOINT_ASSERT(error
== 0);
280 return MEMCACHED_FAILURE
;
283 /* libmemcached will always use nonblocking IO to avoid write deadlocks */
287 flags
= fcntl(ptr
->fd
, F_GETFL
, 0);
288 while (flags
== -1 && (errno
== EINTR
|| errno
== EAGAIN
));
290 unlikely (flags
== -1)
292 return MEMCACHED_CONNECTION_FAILURE
;
294 else if ((flags
& O_NONBLOCK
) == 0)
299 rval
= fcntl(ptr
->fd
, F_SETFL
, flags
| O_NONBLOCK
);
300 while (rval
== -1 && (errno
== EINTR
|| errno
== EAGAIN
));
302 unlikely (rval
== -1)
304 return MEMCACHED_CONNECTION_FAILURE
;
308 return MEMCACHED_SUCCESS
;
311 static memcached_return_t
unix_socket_connect(memcached_server_st
*ptr
)
313 struct sockaddr_un servAddr
;
317 if ((ptr
->fd
= socket(AF_UNIX
, SOCK_STREAM
, 0)) < 0)
319 ptr
->cached_errno
= errno
;
320 return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE
;
323 memset(&servAddr
, 0, sizeof (struct sockaddr_un
));
324 servAddr
.sun_family
= AF_UNIX
;
325 strcpy(servAddr
.sun_path
, ptr
->hostname
); /* Copy filename */
329 (struct sockaddr
*)&servAddr
,
330 sizeof(servAddr
)) < 0)
338 case EISCONN
: /* We were spinning waiting on connect */
341 WATCHPOINT_ERRNO(errno
);
342 ptr
->cached_errno
= errno
;
343 return MEMCACHED_ERRNO
;
348 WATCHPOINT_ASSERT(ptr
->fd
!= -1);
350 return MEMCACHED_SUCCESS
;
353 static memcached_return_t
network_connect(memcached_server_st
*ptr
)
355 bool timeout_error_occured
= false;
359 struct addrinfo
*use
;
361 WATCHPOINT_ASSERT(ptr
->cursor_active
== 0);
363 if (! ptr
->options
.sockaddr_inited
||
364 (!(ptr
->root
->flags
.use_cache_lookups
)))
366 memcached_return_t rc
;
368 rc
= set_hostinfo(ptr
);
369 if (rc
!= MEMCACHED_SUCCESS
)
371 ptr
->options
.sockaddr_inited
= true;
374 use
= ptr
->address_info
;
375 /* Create the socket */
378 /* Memcache server does not support IPV6 in udp mode, so skip if not ipv4 */
379 if (ptr
->type
== MEMCACHED_CONNECTION_UDP
&& use
->ai_family
!= AF_INET
)
385 if ((ptr
->fd
= socket(use
->ai_family
,
387 use
->ai_protocol
)) < 0)
389 ptr
->cached_errno
= errno
;
390 WATCHPOINT_ERRNO(errno
);
391 return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE
;
394 (void)set_socket_options(ptr
);
396 /* connect to server */
397 if ((connect(ptr
->fd
, use
->ai_addr
, use
->ai_addrlen
) > -1))
402 /* An error occurred */
403 ptr
->cached_errno
= errno
;
404 if (errno
== EINPROGRESS
|| /* nonblocking mode - first return, */
405 errno
== EALREADY
) /* nonblocking mode - subsequent returns */
407 memcached_return_t rc
;
408 rc
= connect_poll(ptr
);
410 if (rc
== MEMCACHED_TIMEOUT
)
411 timeout_error_occured
= true;
413 if (rc
== MEMCACHED_SUCCESS
)
416 else if (errno
== EISCONN
) /* we are connected :-) */
420 else if (errno
== EINTR
) // Special case, we retry ai_addr
422 (void)close(ptr
->fd
);
427 (void)close(ptr
->fd
);
435 WATCHPOINT_STRING("Never got a good file descriptor");
437 /* Failed to connect. schedule next retry */
438 if (ptr
->root
->retry_timeout
)
440 struct timeval next_time
;
442 if (gettimeofday(&next_time
, NULL
) == 0)
443 ptr
->next_retry
= next_time
.tv_sec
+ ptr
->root
->retry_timeout
;
446 if (timeout_error_occured
)
447 return MEMCACHED_TIMEOUT
;
449 return MEMCACHED_ERRNO
; /* The last error should be from connect() */
452 return MEMCACHED_SUCCESS
; /* The last error should be from connect() */
455 void set_last_disconnected_host(memcached_server_write_instance_st ptr
)
458 memcached_st
*root
= (memcached_st
*)ptr
->root
;
461 WATCHPOINT_STRING(ptr
->hostname
);
462 WATCHPOINT_NUMBER(ptr
->port
);
463 WATCHPOINT_ERRNO(ptr
->cached_errno
);
465 if (root
->last_disconnected_server
)
466 memcached_server_free(root
->last_disconnected_server
);
467 root
->last_disconnected_server
= memcached_server_clone(NULL
, ptr
);
470 memcached_return_t
memcached_connect(memcached_server_write_instance_st ptr
)
472 memcached_return_t rc
= MEMCACHED_NO_SERVERS
;
473 LIBMEMCACHED_MEMCACHED_CONNECT_START();
475 /* both retry_timeout and server_failure_limit must be set in order to delay retrying a server on error. */
476 WATCHPOINT_ASSERT(ptr
->root
);
477 if (ptr
->root
->retry_timeout
&& ptr
->next_retry
)
479 struct timeval curr_time
;
481 gettimeofday(&curr_time
, NULL
);
483 // We should optimize this to remove the allocation if the server was
484 // the last server to die
485 if (ptr
->next_retry
> curr_time
.tv_sec
)
487 set_last_disconnected_host(ptr
);
489 return MEMCACHED_SERVER_MARKED_DEAD
;
493 // If we are over the counter failure, we just fail. Reject host only
494 // works if you have a set number of failures.
495 if (ptr
->root
->server_failure_limit
&& ptr
->server_failure_counter
>= ptr
->root
->server_failure_limit
)
497 set_last_disconnected_host(ptr
);
499 // @todo fix this by fixing behavior to no longer make use of
501 if (_is_auto_eject_host(ptr
->root
))
503 run_distribution((memcached_st
*)ptr
->root
);
506 return MEMCACHED_SERVER_MARKED_DEAD
;
509 /* We need to clean up the multi startup piece */
512 case MEMCACHED_CONNECTION_UNKNOWN
:
513 WATCHPOINT_ASSERT(0);
514 rc
= MEMCACHED_NOT_SUPPORTED
;
516 case MEMCACHED_CONNECTION_UDP
:
517 case MEMCACHED_CONNECTION_TCP
:
518 rc
= network_connect(ptr
);
519 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
520 if (ptr
->fd
!= -1 && ptr
->root
->sasl
&& ptr
->root
->sasl
->callbacks
)
522 rc
= memcached_sasl_authenticate_connection(ptr
);
523 if (rc
!= MEMCACHED_SUCCESS
)
525 (void)close(ptr
->fd
);
531 case MEMCACHED_CONNECTION_UNIX_SOCKET
:
532 rc
= unix_socket_connect(ptr
);
534 case MEMCACHED_CONNECTION_MAX
:
536 WATCHPOINT_ASSERT(0);
539 if (rc
== MEMCACHED_SUCCESS
)
541 ptr
->server_failure_counter
= 0;
546 ptr
->server_failure_counter
++;
548 set_last_disconnected_host(ptr
);
551 LIBMEMCACHED_MEMCACHED_CONNECT_END();