6 static memcached_return
set_hostinfo(memcached_server_st
*server
)
11 char str_port
[NI_MAXSERV
];
13 sprintf(str_port
, "%u", server
->port
);
15 memset(&hints
, 0, sizeof(hints
));
17 // hints.ai_family= AF_INET;
18 if (server
->type
== MEMCACHED_CONNECTION_UDP
)
20 hints
.ai_protocol
= IPPROTO_UDP
;
21 hints
.ai_socktype
= SOCK_DGRAM
;
25 hints
.ai_socktype
= SOCK_STREAM
;
26 hints
.ai_protocol
= IPPROTO_TCP
;
29 e
= getaddrinfo(server
->hostname
, str_port
, &hints
, &ai
);
32 WATCHPOINT_STRING(server
->hostname
);
33 WATCHPOINT_STRING(gai_strerror(e
));
34 return MEMCACHED_HOST_LOOKUP_FAILURE
;
37 if (server
->address_info
)
39 freeaddrinfo(server
->address_info
);
40 server
->address_info
= NULL
;
42 server
->address_info
= ai
;
44 return MEMCACHED_SUCCESS
;
47 static memcached_return
set_socket_options(memcached_server_st
*ptr
)
49 WATCHPOINT_ASSERT(ptr
->fd
!= -1);
51 if (ptr
->type
== MEMCACHED_CONNECTION_UDP
)
52 return MEMCACHED_SUCCESS
;
55 if (ptr
->root
->snd_timeout
)
58 struct timeval waittime
;
61 waittime
.tv_usec
= ptr
->root
->snd_timeout
;
63 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_SNDTIMEO
,
64 &waittime
, (socklen_t
)sizeof(struct timeval
));
65 WATCHPOINT_ASSERT(error
== 0);
70 if (ptr
->root
->rcv_timeout
)
73 struct timeval waittime
;
76 waittime
.tv_usec
= ptr
->root
->rcv_timeout
;
78 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_RCVTIMEO
,
79 &waittime
, (socklen_t
)sizeof(struct timeval
));
80 WATCHPOINT_ASSERT(error
== 0);
84 if (ptr
->root
->flags
& MEM_NO_BLOCK
)
90 linger
.l_linger
= 0; /* By default on close() just drop the socket */
91 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_LINGER
,
92 &linger
, (socklen_t
)sizeof(struct linger
));
93 WATCHPOINT_ASSERT(error
== 0);
96 if (ptr
->root
->flags
& MEM_TCP_NODELAY
)
101 error
= setsockopt(ptr
->fd
, IPPROTO_TCP
, TCP_NODELAY
,
102 &flag
, (socklen_t
)sizeof(int));
103 WATCHPOINT_ASSERT(error
== 0);
106 if (ptr
->root
->send_size
)
110 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_SNDBUF
,
111 &ptr
->root
->send_size
, (socklen_t
)sizeof(int));
112 WATCHPOINT_ASSERT(error
== 0);
115 if (ptr
->root
->recv_size
)
119 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_RCVBUF
,
120 &ptr
->root
->recv_size
, (socklen_t
)sizeof(int));
121 WATCHPOINT_ASSERT(error
== 0);
124 /* For the moment, not getting a nonblocking mode will not be fatal */
125 if ((ptr
->root
->flags
& MEM_NO_BLOCK
) || ptr
->root
->connect_timeout
)
129 flags
= fcntl(ptr
->fd
, F_GETFL
, 0);
130 unlikely (flags
!= -1)
132 (void)fcntl(ptr
->fd
, F_SETFL
, flags
| O_NONBLOCK
);
136 return MEMCACHED_SUCCESS
;
139 static memcached_return
unix_socket_connect(memcached_server_st
*ptr
)
141 struct sockaddr_un servAddr
;
146 if ((ptr
->fd
= socket(AF_UNIX
, SOCK_STREAM
, 0)) < 0)
148 ptr
->cached_errno
= errno
;
149 return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE
;
152 memset(&servAddr
, 0, sizeof (struct sockaddr_un
));
153 servAddr
.sun_family
= AF_UNIX
;
154 strcpy(servAddr
.sun_path
, ptr
->hostname
); /* Copy filename */
156 addrlen
= (socklen_t
) (strlen(servAddr
.sun_path
) + sizeof(servAddr
.sun_family
));
160 (struct sockaddr
*)&servAddr
,
161 sizeof(servAddr
)) < 0)
169 case EISCONN
: /* We were spinning waiting on connect */
172 WATCHPOINT_ERRNO(errno
);
173 ptr
->cached_errno
= errno
;
174 return MEMCACHED_ERRNO
;
179 WATCHPOINT_ASSERT(ptr
->fd
!= -1);
180 return MEMCACHED_SUCCESS
;
183 static memcached_return
network_connect(memcached_server_st
*ptr
)
187 struct addrinfo
*use
;
189 if (!ptr
->sockaddr_inited
||
190 (!(ptr
->root
->flags
& MEM_USE_CACHE_LOOKUPS
)))
194 rc
= set_hostinfo(ptr
);
195 if (rc
!= MEMCACHED_SUCCESS
)
197 ptr
->sockaddr_inited
= true;
200 use
= ptr
->address_info
;
201 /* Create the socket */
204 /* Memcache server does not support IPV6 in udp mode, so skip if not ipv4 */
205 if (ptr
->type
== MEMCACHED_CONNECTION_UDP
&& use
->ai_family
!= AF_INET
)
211 if ((ptr
->fd
= socket(use
->ai_family
,
213 use
->ai_protocol
)) < 0)
215 ptr
->cached_errno
= errno
;
216 WATCHPOINT_ERRNO(errno
);
217 return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE
;
220 (void)set_socket_options(ptr
);
223 if (ptr
->root
->connect_timeout
)
225 flags
= fcntl(ptr
->fd
, F_GETFL
, 0);
226 if (flags
!= -1 && !(flags
& O_NONBLOCK
))
227 (void)fcntl(ptr
->fd
, F_SETFL
, flags
| O_NONBLOCK
);
230 /* connect to server */
231 while (ptr
->fd
!= -1 &&
232 connect(ptr
->fd
, use
->ai_addr
, use
->ai_addrlen
) < 0)
234 ptr
->cached_errno
= errno
;
235 if (errno
== EINPROGRESS
|| /* nonblocking mode - first return, */
236 errno
== EALREADY
) /* nonblocking mode - subsequent returns */
238 struct pollfd fds
[1];
240 fds
[0].events
= POLLOUT
;
241 int error
= poll(fds
, 1, ptr
->root
->connect_timeout
);
243 if (error
!= 1 || fds
[0].revents
& POLLERR
)
245 if (fds
[0].revents
& POLLERR
)
248 socklen_t len
= sizeof (err
);
249 (void)getsockopt(ptr
->fd
, SOL_SOCKET
, SO_ERROR
, &err
, &len
);
250 ptr
->cached_errno
= (err
== 0) ? errno
: err
;
253 (void)close(ptr
->fd
);
257 else if (errno
== EISCONN
) /* we are connected :-) */
261 else if (errno
!= EINTR
)
263 (void)close(ptr
->fd
);
272 if (ptr
->root
->connect_timeout
&& (ptr
->root
->flags
& MEM_NO_BLOCK
) == 0)
273 (void)fcntl(ptr
->fd
, F_SETFL
, flags
& ~O_NONBLOCK
);
275 WATCHPOINT_ASSERT(ptr
->cursor_active
== 0);
276 ptr
->server_failure_counter
= 0;
277 return MEMCACHED_SUCCESS
;
285 /* Failed to connect. schedule next retry */
286 if (ptr
->root
->retry_timeout
)
288 struct timeval next_time
;
290 if (gettimeofday(&next_time
, NULL
) == 0)
291 ptr
->next_retry
= next_time
.tv_sec
+ ptr
->root
->retry_timeout
;
293 ptr
->server_failure_counter
+= 1;
294 if (ptr
->cached_errno
== 0)
295 return MEMCACHED_TIMEOUT
;
296 return MEMCACHED_ERRNO
; /* The last error should be from connect() */
299 ptr
->server_failure_counter
= 0;
300 return MEMCACHED_SUCCESS
; /* The last error should be from connect() */
304 memcached_return
memcached_connect(memcached_server_st
*ptr
)
306 memcached_return rc
= MEMCACHED_NO_SERVERS
;
307 LIBMEMCACHED_MEMCACHED_CONNECT_START();
309 /* both retry_timeout and server_failure_limit must be set in order to delay retrying a server on error. */
310 WATCHPOINT_ASSERT(ptr
->root
);
311 if (ptr
->root
->retry_timeout
&& ptr
->root
->server_failure_limit
)
313 struct timeval next_time
;
315 gettimeofday(&next_time
, NULL
);
317 /* if we've had too many consecutive errors on this server, mark it dead. */
318 if (ptr
->server_failure_counter
> ptr
->root
->server_failure_limit
)
320 ptr
->next_retry
= next_time
.tv_sec
+ ptr
->root
->retry_timeout
;
321 ptr
->server_failure_counter
= 0;
324 if (next_time
.tv_sec
< ptr
->next_retry
)
326 if (memcached_behavior_get(ptr
->root
, MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS
))
327 run_distribution(ptr
->root
);
329 return MEMCACHED_SERVER_MARKED_DEAD
;
333 /* We need to clean up the multi startup piece */
336 case MEMCACHED_CONNECTION_UNKNOWN
:
337 WATCHPOINT_ASSERT(0);
338 rc
= MEMCACHED_NOT_SUPPORTED
;
340 case MEMCACHED_CONNECTION_UDP
:
341 case MEMCACHED_CONNECTION_TCP
:
342 rc
= network_connect(ptr
);
344 case MEMCACHED_CONNECTION_UNIX_SOCKET
:
345 rc
= unix_socket_connect(ptr
);
348 WATCHPOINT_ASSERT(0);
351 LIBMEMCACHED_MEMCACHED_CONNECT_END();