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
;
83 WATCHPOINT_ASSERT(0); // Programming error
86 // This should only be possible from ERESTART or EINTR;
87 ptr
->cached_errno
= get_socket_errno();
89 return MEMCACHED_ERRNO
;
92 static memcached_return_t
set_hostinfo(memcached_server_st
*server
)
95 struct addrinfo hints
;
96 char str_port
[NI_MAXSERV
];
99 snprintf(str_port
, NI_MAXSERV
, "%u", (uint32_t)server
->port
);
101 memset(&hints
, 0, sizeof(hints
));
103 // hints.ai_family= AF_INET;
104 if (server
->type
== MEMCACHED_CONNECTION_UDP
)
106 hints
.ai_protocol
= IPPROTO_UDP
;
107 hints
.ai_socktype
= SOCK_DGRAM
;
111 hints
.ai_socktype
= SOCK_STREAM
;
112 hints
.ai_protocol
= IPPROTO_TCP
;
117 int e
= getaddrinfo(server
->hostname
, str_port
, &hints
, &ai
);
123 else if (e
== EAI_AGAIN
)
126 struct timespec dream
, rem
;
131 nanosleep(&dream
, &rem
);
137 WATCHPOINT_STRING(server
->hostname
);
138 WATCHPOINT_STRING(gai_strerror(e
));
139 return MEMCACHED_HOST_LOOKUP_FAILURE
;
143 if (server
->address_info
)
145 freeaddrinfo(server
->address_info
);
146 server
->address_info
= NULL
;
148 server
->address_info
= ai
;
150 return MEMCACHED_SUCCESS
;
153 static inline memcached_return_t
set_socket_nonblocking(memcached_server_st
*ptr
)
157 if (ioctlsocket(ptr
->fd
, FIONBIO
, &arg
) == SOCKET_ERROR
)
159 ptr
->cached_errno
= get_socket_errno();
160 return MEMCACHED_CONNECTION_FAILURE
;
166 flags
= fcntl(ptr
->fd
, F_GETFL
, 0);
167 while (flags
== -1 && (errno
== EINTR
|| errno
== EAGAIN
));
169 unlikely (flags
== -1)
171 ptr
->cached_errno
= errno
;
172 return MEMCACHED_CONNECTION_FAILURE
;
174 else if ((flags
& O_NONBLOCK
) == 0)
179 rval
= fcntl(ptr
->fd
, F_SETFL
, flags
| O_NONBLOCK
);
180 while (rval
== -1 && (errno
== EINTR
|| errno
== EAGAIN
));
182 unlikely (rval
== -1)
184 ptr
->cached_errno
= errno
;
185 return MEMCACHED_CONNECTION_FAILURE
;
189 return MEMCACHED_SUCCESS
;
192 static memcached_return_t
set_socket_options(memcached_server_st
*ptr
)
194 WATCHPOINT_ASSERT(ptr
->fd
!= -1);
196 if (ptr
->type
== MEMCACHED_CONNECTION_UDP
)
197 return MEMCACHED_SUCCESS
;
200 if (ptr
->root
->snd_timeout
)
203 struct timeval waittime
;
206 waittime
.tv_usec
= ptr
->root
->snd_timeout
;
208 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_SNDTIMEO
,
209 &waittime
, (socklen_t
)sizeof(struct timeval
));
210 WATCHPOINT_ASSERT(error
== 0);
212 return MEMCACHED_FAILURE
;
217 if (ptr
->root
->rcv_timeout
)
220 struct timeval waittime
;
223 waittime
.tv_usec
= ptr
->root
->rcv_timeout
;
225 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_RCVTIMEO
,
226 &waittime
, (socklen_t
)sizeof(struct timeval
));
227 WATCHPOINT_ASSERT(error
== 0);
229 return MEMCACHED_FAILURE
;
234 #if defined(__MACH__) && defined(__APPLE__) || defined(__FreeBSD__)
237 int error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_NOSIGPIPE
, (void *)&set
, sizeof(int));
239 // This is not considered a fatal error
242 WATCHPOINT_ERRNO(get_socket_errno());
243 perror("setsockopt(SO_NOSIGPIPE)");
248 if (ptr
->root
->flags
.no_block
)
251 struct linger linger
;
254 linger
.l_linger
= 0; /* By default on close() just drop the socket */
255 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_LINGER
,
256 &linger
, (socklen_t
)sizeof(struct linger
));
257 WATCHPOINT_ASSERT(error
== 0);
259 return MEMCACHED_FAILURE
;
262 if (ptr
->root
->flags
.tcp_nodelay
)
267 error
= setsockopt(ptr
->fd
, IPPROTO_TCP
, TCP_NODELAY
,
268 &flag
, (socklen_t
)sizeof(int));
269 WATCHPOINT_ASSERT(error
== 0);
271 return MEMCACHED_FAILURE
;
274 if (ptr
->root
->flags
.tcp_keepalive
)
279 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_KEEPALIVE
,
280 &flag
, (socklen_t
)sizeof(int));
281 WATCHPOINT_ASSERT(error
== 0);
283 return MEMCACHED_FAILURE
;
287 if (ptr
->root
->tcp_keepidle
> 0)
291 error
= setsockopt(ptr
->fd
, IPPROTO_TCP
, TCP_KEEPIDLE
,
292 &ptr
->root
->tcp_keepidle
, (socklen_t
)sizeof(int));
293 WATCHPOINT_ASSERT(error
== 0);
295 return MEMCACHED_FAILURE
;
299 if (ptr
->root
->send_size
> 0)
303 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_SNDBUF
,
304 &ptr
->root
->send_size
, (socklen_t
)sizeof(int));
305 WATCHPOINT_ASSERT(error
== 0);
307 return MEMCACHED_FAILURE
;
310 if (ptr
->root
->recv_size
> 0)
314 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_RCVBUF
,
315 &ptr
->root
->recv_size
, (socklen_t
)sizeof(int));
316 WATCHPOINT_ASSERT(error
== 0);
318 return MEMCACHED_FAILURE
;
322 /* libmemcached will always use nonblocking IO to avoid write deadlocks */
323 return set_socket_nonblocking(ptr
);
326 static memcached_return_t
unix_socket_connect(memcached_server_st
*ptr
)
329 struct sockaddr_un servAddr
;
331 WATCHPOINT_ASSERT(ptr
->fd
== -1);
333 if ((ptr
->fd
= socket(AF_UNIX
, SOCK_STREAM
, 0)) < 0)
335 ptr
->cached_errno
= errno
;
336 return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE
;
339 memset(&servAddr
, 0, sizeof (struct sockaddr_un
));
340 servAddr
.sun_family
= AF_UNIX
;
341 strcpy(servAddr
.sun_path
, ptr
->hostname
); /* Copy filename */
345 (struct sockaddr
*)&servAddr
,
346 sizeof(servAddr
)) < 0)
354 case EISCONN
: /* We were spinning waiting on connect */
357 WATCHPOINT_ERRNO(errno
);
358 ptr
->cached_errno
= errno
;
359 return MEMCACHED_ERRNO
;
363 WATCHPOINT_ASSERT(ptr
->fd
!= -1);
365 return MEMCACHED_SUCCESS
;
368 return MEMCACHED_NOT_SUPPORTED
;
372 static memcached_return_t
network_connect(memcached_server_st
*ptr
)
374 bool timeout_error_occured
= false;
377 WATCHPOINT_ASSERT(ptr
->fd
== INVALID_SOCKET
);
378 WATCHPOINT_ASSERT(ptr
->cursor_active
== 0);
380 if (! ptr
->options
.sockaddr_inited
|| (!(ptr
->root
->flags
.use_cache_lookups
)))
382 memcached_return_t rc
;
384 rc
= set_hostinfo(ptr
);
385 if (rc
!= MEMCACHED_SUCCESS
)
387 ptr
->options
.sockaddr_inited
= true;
390 struct addrinfo
*use
= ptr
->address_info
;
391 /* Create the socket */
394 /* Memcache server does not support IPV6 in udp mode, so skip if not ipv4 */
395 if (ptr
->type
== MEMCACHED_CONNECTION_UDP
&& use
->ai_family
!= AF_INET
)
401 if ((ptr
->fd
= socket(use
->ai_family
,
403 use
->ai_protocol
)) < 0)
405 ptr
->cached_errno
= get_socket_errno();
406 WATCHPOINT_ERRNO(get_socket_errno());
407 return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE
;
410 (void)set_socket_options(ptr
);
412 /* connect to server */
413 if ((connect(ptr
->fd
, use
->ai_addr
, use
->ai_addrlen
) != SOCKET_ERROR
))
418 /* An error occurred */
419 ptr
->cached_errno
= get_socket_errno();
420 if (ptr
->cached_errno
== EWOULDBLOCK
||
421 ptr
->cached_errno
== EINPROGRESS
|| /* nonblocking mode - first return, */
422 ptr
->cached_errno
== EALREADY
) /* nonblocking mode - subsequent returns */
424 memcached_return_t rc
;
425 rc
= connect_poll(ptr
);
427 if (rc
== MEMCACHED_TIMEOUT
)
428 timeout_error_occured
= true;
430 if (rc
== MEMCACHED_SUCCESS
)
433 else if (get_socket_errno() == EISCONN
) /* we are connected :-) */
437 else if (get_socket_errno() == EINTR
) // Special case, we retry ai_addr
439 (void)closesocket(ptr
->fd
);
440 ptr
->fd
= INVALID_SOCKET
;
444 (void)closesocket(ptr
->fd
);
445 ptr
->fd
= INVALID_SOCKET
;
449 if (ptr
->fd
== INVALID_SOCKET
)
451 WATCHPOINT_STRING("Never got a good file descriptor");
453 /* Failed to connect. schedule next retry */
454 if (ptr
->root
->retry_timeout
)
456 struct timeval next_time
;
458 if (gettimeofday(&next_time
, NULL
) == 0)
459 ptr
->next_retry
= next_time
.tv_sec
+ ptr
->root
->retry_timeout
;
462 if (timeout_error_occured
)
463 return MEMCACHED_TIMEOUT
;
465 return MEMCACHED_ERRNO
; /* The last error should be from connect() */
468 return MEMCACHED_SUCCESS
; /* The last error should be from connect() */
471 void set_last_disconnected_host(memcached_server_write_instance_st ptr
)
474 memcached_st
*root
= (memcached_st
*)ptr
->root
;
477 WATCHPOINT_STRING(ptr
->hostname
);
478 WATCHPOINT_NUMBER(ptr
->port
);
479 WATCHPOINT_ERRNO(ptr
->cached_errno
);
481 if (root
->last_disconnected_server
)
482 memcached_server_free(root
->last_disconnected_server
);
483 root
->last_disconnected_server
= memcached_server_clone(NULL
, ptr
);
486 memcached_return_t
memcached_connect(memcached_server_write_instance_st ptr
)
488 memcached_return_t rc
= MEMCACHED_NO_SERVERS
;
490 if (ptr
->fd
!= INVALID_SOCKET
)
491 return MEMCACHED_SUCCESS
;
493 LIBMEMCACHED_MEMCACHED_CONNECT_START();
495 /* both retry_timeout and server_failure_limit must be set in order to delay retrying a server on error. */
496 WATCHPOINT_ASSERT(ptr
->root
);
497 if (ptr
->root
->retry_timeout
&& ptr
->next_retry
)
499 struct timeval curr_time
;
501 gettimeofday(&curr_time
, NULL
);
503 // We should optimize this to remove the allocation if the server was
504 // the last server to die
505 if (ptr
->next_retry
> curr_time
.tv_sec
)
507 set_last_disconnected_host(ptr
);
509 return MEMCACHED_SERVER_MARKED_DEAD
;
513 // If we are over the counter failure, we just fail. Reject host only
514 // works if you have a set number of failures.
515 if (ptr
->root
->server_failure_limit
&& ptr
->server_failure_counter
>= ptr
->root
->server_failure_limit
)
517 set_last_disconnected_host(ptr
);
519 // @todo fix this by fixing behavior to no longer make use of
521 if (_is_auto_eject_host(ptr
->root
))
523 run_distribution((memcached_st
*)ptr
->root
);
526 return MEMCACHED_SERVER_MARKED_DEAD
;
529 /* We need to clean up the multi startup piece */
532 case MEMCACHED_CONNECTION_UNKNOWN
:
533 WATCHPOINT_ASSERT(0);
534 rc
= MEMCACHED_NOT_SUPPORTED
;
536 case MEMCACHED_CONNECTION_UDP
:
537 case MEMCACHED_CONNECTION_TCP
:
538 rc
= network_connect(ptr
);
539 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
540 if (ptr
->fd
!= INVALID_SOCKET
&& ptr
->root
->sasl
&& ptr
->root
->sasl
->callbacks
)
542 rc
= memcached_sasl_authenticate_connection(ptr
);
543 if (rc
!= MEMCACHED_SUCCESS
)
545 (void)closesocket(ptr
->fd
);
546 ptr
->fd
= INVALID_SOCKET
;
551 case MEMCACHED_CONNECTION_UNIX_SOCKET
:
552 rc
= unix_socket_connect(ptr
);
554 case MEMCACHED_CONNECTION_MAX
:
556 WATCHPOINT_ASSERT(0);
559 if (rc
== MEMCACHED_SUCCESS
)
561 ptr
->server_failure_counter
= 0;
566 ptr
->server_failure_counter
++;
568 set_last_disconnected_host(ptr
);
571 LIBMEMCACHED_MEMCACHED_CONNECT_END();