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
;
25 while (--loop_max
) // Should only loop on cases of ERESTART or EINTR
27 error
= poll(fds
, 1, ptr
->root
->connect_timeout
);
34 socklen_t len
= sizeof (err
);
35 (void)getsockopt(ptr
->fd
, SOL_SOCKET
, SO_ERROR
, &err
, &len
);
37 // We check the value to see what happened wth the socket.
40 return MEMCACHED_SUCCESS
;
44 ptr
->cached_errno
= errno
;
46 return MEMCACHED_ERRNO
;
50 return MEMCACHED_TIMEOUT
;
51 default: // A real error occurred and we need to completely bail
52 WATCHPOINT_ERRNO(get_socket_errno());
53 switch (get_socket_errno())
55 #ifdef TARGET_OS_LINUX
61 if (fds
[0].revents
& POLLERR
)
64 socklen_t len
= sizeof (err
);
65 (void)getsockopt(ptr
->fd
, SOL_SOCKET
, SO_ERROR
, &err
, &len
);
66 ptr
->cached_errno
= (err
== 0) ? get_socket_errno() : err
;
70 ptr
->cached_errno
= get_socket_errno();
73 (void)closesocket(ptr
->fd
);
74 ptr
->fd
= INVALID_SOCKET
;
76 return MEMCACHED_ERRNO
;
81 // This should only be possible from ERESTART or EINTR;
82 ptr
->cached_errno
= get_socket_errno();
84 return MEMCACHED_ERRNO
;
87 static memcached_return_t
set_hostinfo(memcached_server_st
*server
)
90 struct addrinfo hints
;
91 char str_port
[NI_MAXSERV
];
94 int length
= snprintf(str_port
, NI_MAXSERV
, "%u", (uint32_t)server
->port
);
95 if (length
>= NI_MAXSERV
|| length
< 0)
96 return MEMCACHED_FAILURE
;
98 memset(&hints
, 0, sizeof(hints
));
100 // hints.ai_family= AF_INET;
101 if (server
->type
== MEMCACHED_CONNECTION_UDP
)
103 hints
.ai_protocol
= IPPROTO_UDP
;
104 hints
.ai_socktype
= SOCK_DGRAM
;
108 hints
.ai_socktype
= SOCK_STREAM
;
109 hints
.ai_protocol
= IPPROTO_TCP
;
114 int e
= getaddrinfo(server
->hostname
, str_port
, &hints
, &ai
);
120 else if (e
== EAI_AGAIN
)
123 struct timespec dream
, rem
;
128 nanosleep(&dream
, &rem
);
134 WATCHPOINT_STRING(server
->hostname
);
135 WATCHPOINT_STRING(gai_strerror(e
));
136 return MEMCACHED_HOST_LOOKUP_FAILURE
;
140 if (server
->address_info
)
142 freeaddrinfo(server
->address_info
);
143 server
->address_info
= NULL
;
145 server
->address_info
= ai
;
147 return MEMCACHED_SUCCESS
;
150 static inline memcached_return_t
set_socket_nonblocking(memcached_server_st
*ptr
)
154 if (ioctlsocket(ptr
->fd
, FIONBIO
, &arg
) == SOCKET_ERROR
)
156 ptr
->cached_errno
= get_socket_errno();
157 return MEMCACHED_CONNECTION_FAILURE
;
163 flags
= fcntl(ptr
->fd
, F_GETFL
, 0);
164 while (flags
== -1 && (errno
== EINTR
|| errno
== EAGAIN
));
166 unlikely (flags
== -1)
168 ptr
->cached_errno
= errno
;
169 return MEMCACHED_CONNECTION_FAILURE
;
171 else if ((flags
& O_NONBLOCK
) == 0)
176 rval
= fcntl(ptr
->fd
, F_SETFL
, flags
| O_NONBLOCK
);
177 while (rval
== -1 && (errno
== EINTR
|| errno
== EAGAIN
));
179 unlikely (rval
== -1)
181 ptr
->cached_errno
= errno
;
182 return MEMCACHED_CONNECTION_FAILURE
;
186 return MEMCACHED_SUCCESS
;
189 static memcached_return_t
set_socket_options(memcached_server_st
*ptr
)
191 WATCHPOINT_ASSERT(ptr
->fd
!= -1);
193 if (ptr
->type
== MEMCACHED_CONNECTION_UDP
)
194 return MEMCACHED_SUCCESS
;
197 if (ptr
->root
->snd_timeout
)
200 struct timeval waittime
;
203 waittime
.tv_usec
= ptr
->root
->snd_timeout
;
205 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_SNDTIMEO
,
206 &waittime
, (socklen_t
)sizeof(struct timeval
));
207 WATCHPOINT_ASSERT(error
== 0);
209 return MEMCACHED_FAILURE
;
214 if (ptr
->root
->rcv_timeout
)
217 struct timeval waittime
;
220 waittime
.tv_usec
= ptr
->root
->rcv_timeout
;
222 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_RCVTIMEO
,
223 &waittime
, (socklen_t
)sizeof(struct timeval
));
224 WATCHPOINT_ASSERT(error
== 0);
226 return MEMCACHED_FAILURE
;
231 #if defined(__MACH__) && defined(__APPLE__) || defined(__FreeBSD__)
234 int error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_NOSIGPIPE
, (void *)&set
, sizeof(int));
236 // This is not considered a fatal error
239 WATCHPOINT_ERRNO(get_socket_errno());
240 perror("setsockopt(SO_NOSIGPIPE)");
245 if (ptr
->root
->flags
.no_block
)
248 struct linger linger
;
251 linger
.l_linger
= 0; /* By default on close() just drop the socket */
252 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_LINGER
,
253 &linger
, (socklen_t
)sizeof(struct linger
));
254 WATCHPOINT_ASSERT(error
== 0);
256 return MEMCACHED_FAILURE
;
259 if (ptr
->root
->flags
.tcp_nodelay
)
264 error
= setsockopt(ptr
->fd
, IPPROTO_TCP
, TCP_NODELAY
,
265 &flag
, (socklen_t
)sizeof(int));
266 WATCHPOINT_ASSERT(error
== 0);
268 return MEMCACHED_FAILURE
;
271 if (ptr
->root
->flags
.tcp_keepalive
)
276 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_KEEPALIVE
,
277 &flag
, (socklen_t
)sizeof(int));
278 WATCHPOINT_ASSERT(error
== 0);
280 return MEMCACHED_FAILURE
;
284 if (ptr
->root
->tcp_keepidle
> 0)
288 error
= setsockopt(ptr
->fd
, IPPROTO_TCP
, TCP_KEEPIDLE
,
289 &ptr
->root
->tcp_keepidle
, (socklen_t
)sizeof(int));
290 WATCHPOINT_ASSERT(error
== 0);
292 return MEMCACHED_FAILURE
;
296 if (ptr
->root
->send_size
> 0)
300 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_SNDBUF
,
301 &ptr
->root
->send_size
, (socklen_t
)sizeof(int));
302 WATCHPOINT_ASSERT(error
== 0);
304 return MEMCACHED_FAILURE
;
307 if (ptr
->root
->recv_size
> 0)
311 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_RCVBUF
,
312 &ptr
->root
->recv_size
, (socklen_t
)sizeof(int));
313 WATCHPOINT_ASSERT(error
== 0);
315 return MEMCACHED_FAILURE
;
319 /* libmemcached will always use nonblocking IO to avoid write deadlocks */
320 return set_socket_nonblocking(ptr
);
323 static memcached_return_t
unix_socket_connect(memcached_server_st
*ptr
)
326 struct sockaddr_un servAddr
;
328 WATCHPOINT_ASSERT(ptr
->fd
== -1);
330 if ((ptr
->fd
= socket(AF_UNIX
, SOCK_STREAM
, 0)) < 0)
332 ptr
->cached_errno
= errno
;
333 return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE
;
336 memset(&servAddr
, 0, sizeof (struct sockaddr_un
));
337 servAddr
.sun_family
= AF_UNIX
;
338 strcpy(servAddr
.sun_path
, ptr
->hostname
); /* Copy filename */
342 (struct sockaddr
*)&servAddr
,
343 sizeof(servAddr
)) < 0)
351 case EISCONN
: /* We were spinning waiting on connect */
354 WATCHPOINT_ERRNO(errno
);
355 ptr
->cached_errno
= errno
;
356 return MEMCACHED_ERRNO
;
360 WATCHPOINT_ASSERT(ptr
->fd
!= -1);
362 return MEMCACHED_SUCCESS
;
365 return MEMCACHED_NOT_SUPPORTED
;
369 static memcached_return_t
network_connect(memcached_server_st
*ptr
)
371 bool timeout_error_occured
= false;
374 WATCHPOINT_ASSERT(ptr
->fd
== INVALID_SOCKET
);
375 WATCHPOINT_ASSERT(ptr
->cursor_active
== 0);
377 if (! ptr
->options
.sockaddr_inited
|| (!(ptr
->root
->flags
.use_cache_lookups
)))
379 memcached_return_t rc
;
381 rc
= set_hostinfo(ptr
);
382 if (rc
!= MEMCACHED_SUCCESS
)
384 ptr
->options
.sockaddr_inited
= true;
387 struct addrinfo
*use
= ptr
->address_info
;
388 /* Create the socket */
391 /* Memcache server does not support IPV6 in udp mode, so skip if not ipv4 */
392 if (ptr
->type
== MEMCACHED_CONNECTION_UDP
&& use
->ai_family
!= AF_INET
)
398 if ((ptr
->fd
= socket(use
->ai_family
,
400 use
->ai_protocol
)) < 0)
402 ptr
->cached_errno
= get_socket_errno();
403 WATCHPOINT_ERRNO(get_socket_errno());
404 return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE
;
407 (void)set_socket_options(ptr
);
409 /* connect to server */
410 if ((connect(ptr
->fd
, use
->ai_addr
, use
->ai_addrlen
) != SOCKET_ERROR
))
415 /* An error occurred */
416 ptr
->cached_errno
= get_socket_errno();
417 if (ptr
->cached_errno
== EWOULDBLOCK
||
418 ptr
->cached_errno
== EINPROGRESS
|| /* nonblocking mode - first return, */
419 ptr
->cached_errno
== EALREADY
) /* nonblocking mode - subsequent returns */
421 memcached_return_t rc
;
422 rc
= connect_poll(ptr
);
424 if (rc
== MEMCACHED_TIMEOUT
)
425 timeout_error_occured
= true;
427 if (rc
== MEMCACHED_SUCCESS
)
430 else if (get_socket_errno() == EISCONN
) /* we are connected :-) */
434 else if (get_socket_errno() == EINTR
) // Special case, we retry ai_addr
436 (void)closesocket(ptr
->fd
);
437 ptr
->fd
= INVALID_SOCKET
;
441 (void)closesocket(ptr
->fd
);
442 ptr
->fd
= INVALID_SOCKET
;
446 if (ptr
->fd
== INVALID_SOCKET
)
448 WATCHPOINT_STRING("Never got a good file descriptor");
450 /* Failed to connect. schedule next retry */
451 if (ptr
->root
->retry_timeout
)
453 struct timeval next_time
;
455 if (gettimeofday(&next_time
, NULL
) == 0)
456 ptr
->next_retry
= next_time
.tv_sec
+ ptr
->root
->retry_timeout
;
459 if (timeout_error_occured
)
460 return MEMCACHED_TIMEOUT
;
462 return MEMCACHED_ERRNO
; /* The last error should be from connect() */
465 return MEMCACHED_SUCCESS
; /* The last error should be from connect() */
468 void set_last_disconnected_host(memcached_server_write_instance_st ptr
)
471 memcached_st
*root
= (memcached_st
*)ptr
->root
;
474 WATCHPOINT_STRING(ptr
->hostname
);
475 WATCHPOINT_NUMBER(ptr
->port
);
476 WATCHPOINT_ERRNO(ptr
->cached_errno
);
478 if (root
->last_disconnected_server
)
479 memcached_server_free(root
->last_disconnected_server
);
480 root
->last_disconnected_server
= memcached_server_clone(NULL
, ptr
);
483 memcached_return_t
memcached_connect(memcached_server_write_instance_st ptr
)
485 memcached_return_t rc
= MEMCACHED_NO_SERVERS
;
487 if (ptr
->fd
!= INVALID_SOCKET
)
488 return MEMCACHED_SUCCESS
;
490 LIBMEMCACHED_MEMCACHED_CONNECT_START();
492 /* both retry_timeout and server_failure_limit must be set in order to delay retrying a server on error. */
493 WATCHPOINT_ASSERT(ptr
->root
);
494 if (ptr
->root
->retry_timeout
&& ptr
->next_retry
)
496 struct timeval curr_time
;
498 gettimeofday(&curr_time
, NULL
);
500 // We should optimize this to remove the allocation if the server was
501 // the last server to die
502 if (ptr
->next_retry
> curr_time
.tv_sec
)
504 set_last_disconnected_host(ptr
);
506 return MEMCACHED_SERVER_MARKED_DEAD
;
510 // If we are over the counter failure, we just fail. Reject host only
511 // works if you have a set number of failures.
512 if (ptr
->root
->server_failure_limit
&& ptr
->server_failure_counter
>= ptr
->root
->server_failure_limit
)
514 set_last_disconnected_host(ptr
);
516 // @todo fix this by fixing behavior to no longer make use of
518 if (_is_auto_eject_host(ptr
->root
))
520 run_distribution((memcached_st
*)ptr
->root
);
523 return MEMCACHED_SERVER_MARKED_DEAD
;
526 /* We need to clean up the multi startup piece */
529 case MEMCACHED_CONNECTION_UNKNOWN
:
530 WATCHPOINT_ASSERT(0);
531 rc
= MEMCACHED_NOT_SUPPORTED
;
533 case MEMCACHED_CONNECTION_UDP
:
534 case MEMCACHED_CONNECTION_TCP
:
535 rc
= network_connect(ptr
);
536 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
537 if (ptr
->fd
!= INVALID_SOCKET
&& ptr
->root
->sasl
.callbacks
)
539 rc
= memcached_sasl_authenticate_connection(ptr
);
540 if (rc
!= MEMCACHED_SUCCESS
)
542 (void)closesocket(ptr
->fd
);
543 ptr
->fd
= INVALID_SOCKET
;
548 case MEMCACHED_CONNECTION_UNIX_SOCKET
:
549 rc
= unix_socket_connect(ptr
);
551 case MEMCACHED_CONNECTION_MAX
:
553 WATCHPOINT_ASSERT(0);
556 if (rc
== MEMCACHED_SUCCESS
)
558 ptr
->server_failure_counter
= 0;
563 ptr
->server_failure_counter
++;
565 set_last_disconnected_host(ptr
);
568 LIBMEMCACHED_MEMCACHED_CONNECT_END();