Add support for AUTO_EJECT_HOST
[awesomized/libmemcached] / libmemcached / memcached_connect.c
1 #include "common.h"
2 #include <netdb.h>
3 #include <poll.h>
4 #include <sys/time.h>
5
6 static memcached_return set_hostinfo(memcached_server_st *server)
7 {
8 struct addrinfo *ai;
9 struct addrinfo hints;
10 int e;
11 char str_port[NI_MAXSERV];
12
13 sprintf(str_port, "%u", server->port);
14
15 memset(&hints, 0, sizeof(hints));
16
17 // hints.ai_family= AF_INET;
18 if (server->type == MEMCACHED_CONNECTION_UDP)
19 {
20 hints.ai_protocol= IPPROTO_UDP;
21 hints.ai_socktype= SOCK_DGRAM;
22 }
23 else
24 {
25 hints.ai_socktype= SOCK_STREAM;
26 hints.ai_protocol= IPPROTO_TCP;
27 }
28
29 e= getaddrinfo(server->hostname, str_port, &hints, &ai);
30 if (e != 0)
31 {
32 WATCHPOINT_STRING(server->hostname);
33 WATCHPOINT_STRING(gai_strerror(e));
34 return MEMCACHED_HOST_LOOKUP_FAILURE;
35 }
36
37 if (server->address_info)
38 {
39 freeaddrinfo(server->address_info);
40 server->address_info= NULL;
41 }
42 server->address_info= ai;
43
44 return MEMCACHED_SUCCESS;
45 }
46
47 static memcached_return set_socket_options(memcached_server_st *ptr)
48 {
49 WATCHPOINT_ASSERT(ptr->fd != -1);
50
51 if (ptr->type == MEMCACHED_CONNECTION_UDP)
52 return MEMCACHED_SUCCESS;
53
54 #ifdef HAVE_SNDTIMEO
55 if (ptr->root->snd_timeout)
56 {
57 int error;
58 struct timeval waittime;
59
60 waittime.tv_sec= 0;
61 waittime.tv_usec= ptr->root->snd_timeout;
62
63 error= setsockopt(ptr->fd, SOL_SOCKET, SO_SNDTIMEO,
64 &waittime, (socklen_t)sizeof(struct timeval));
65 WATCHPOINT_ASSERT(error == 0);
66 }
67 #endif
68
69 #ifdef HAVE_RCVTIMEO
70 if (ptr->root->rcv_timeout)
71 {
72 int error;
73 struct timeval waittime;
74
75 waittime.tv_sec= 0;
76 waittime.tv_usec= ptr->root->rcv_timeout;
77
78 error= setsockopt(ptr->fd, SOL_SOCKET, SO_RCVTIMEO,
79 &waittime, (socklen_t)sizeof(struct timeval));
80 WATCHPOINT_ASSERT(error == 0);
81 }
82 #endif
83
84 {
85 int error;
86 struct linger linger;
87
88 linger.l_onoff= 1;
89 linger.l_linger= MEMCACHED_DEFAULT_TIMEOUT;
90 error= setsockopt(ptr->fd, SOL_SOCKET, SO_LINGER,
91 &linger, (socklen_t)sizeof(struct linger));
92 WATCHPOINT_ASSERT(error == 0);
93 }
94
95 if (ptr->root->flags & MEM_TCP_NODELAY)
96 {
97 int flag= 1;
98 int error;
99
100 error= setsockopt(ptr->fd, IPPROTO_TCP, TCP_NODELAY,
101 &flag, (socklen_t)sizeof(int));
102 WATCHPOINT_ASSERT(error == 0);
103 }
104
105 if (ptr->root->send_size)
106 {
107 int error;
108
109 error= setsockopt(ptr->fd, SOL_SOCKET, SO_SNDBUF,
110 &ptr->root->send_size, (socklen_t)sizeof(int));
111 WATCHPOINT_ASSERT(error == 0);
112 }
113
114 if (ptr->root->recv_size)
115 {
116 int error;
117
118 error= setsockopt(ptr->fd, SOL_SOCKET, SO_RCVBUF,
119 &ptr->root->recv_size, (socklen_t)sizeof(int));
120 WATCHPOINT_ASSERT(error == 0);
121 }
122
123 /* For the moment, not getting a nonblocking mode will not be fatal */
124 if ((ptr->root->flags & MEM_NO_BLOCK) || ptr->root->connect_timeout)
125 {
126 int flags;
127
128 flags= fcntl(ptr->fd, F_GETFL, 0);
129 unlikely (flags != -1)
130 {
131 (void)fcntl(ptr->fd, F_SETFL, flags | O_NONBLOCK);
132 }
133 }
134
135 return MEMCACHED_SUCCESS;
136 }
137
138 static memcached_return unix_socket_connect(memcached_server_st *ptr)
139 {
140 struct sockaddr_un servAddr;
141 socklen_t addrlen;
142
143 if (ptr->fd == -1)
144 {
145 if ((ptr->fd= socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
146 {
147 ptr->cached_errno= errno;
148 return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE;
149 }
150
151 memset(&servAddr, 0, sizeof (struct sockaddr_un));
152 servAddr.sun_family= AF_UNIX;
153 strcpy(servAddr.sun_path, ptr->hostname); /* Copy filename */
154
155 addrlen= strlen(servAddr.sun_path) + sizeof(servAddr.sun_family);
156
157 test_connect:
158 if (connect(ptr->fd,
159 (struct sockaddr *)&servAddr,
160 sizeof(servAddr)) < 0)
161 {
162 switch (errno)
163 {
164 case EINPROGRESS:
165 case EALREADY:
166 case EINTR:
167 goto test_connect;
168 case EISCONN: /* We were spinning waiting on connect */
169 break;
170 default:
171 WATCHPOINT_ERRNO(errno);
172 ptr->cached_errno= errno;
173 return MEMCACHED_ERRNO;
174 }
175 }
176 }
177
178 WATCHPOINT_ASSERT(ptr->fd != -1);
179 return MEMCACHED_SUCCESS;
180 }
181
182 static memcached_return network_connect(memcached_server_st *ptr)
183 {
184 if (ptr->fd == -1)
185 {
186 struct addrinfo *use;
187
188 if (!ptr->sockaddr_inited ||
189 (!(ptr->root->flags & MEM_USE_CACHE_LOOKUPS)))
190 {
191 memcached_return rc;
192
193 rc= set_hostinfo(ptr);
194 if (rc != MEMCACHED_SUCCESS)
195 return rc;
196 ptr->sockaddr_inited= true;
197 }
198
199 use= ptr->address_info;
200 /* Create the socket */
201 while (use != NULL)
202 {
203 /* Memcache server does not support IPV6 in udp mode, so skip if not ipv4 */
204 if (ptr->type == MEMCACHED_CONNECTION_UDP && use->ai_family != AF_INET)
205 {
206 use= use->ai_next;
207 continue;
208 }
209
210 if ((ptr->fd= socket(use->ai_family,
211 use->ai_socktype,
212 use->ai_protocol)) < 0)
213 {
214 ptr->cached_errno= errno;
215 WATCHPOINT_ERRNO(errno);
216 return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE;
217 }
218
219 (void)set_socket_options(ptr);
220
221 int flags;
222 if (ptr->root->connect_timeout)
223 {
224 flags= fcntl(ptr->fd, F_GETFL, 0);
225 if (flags != -1 && !(flags & O_NONBLOCK))
226 (void)fcntl(ptr->fd, F_SETFL, flags | O_NONBLOCK);
227 }
228
229 /* connect to server */
230 while (ptr->fd != -1 &&
231 connect(ptr->fd, use->ai_addr, use->ai_addrlen) < 0)
232 {
233 ptr->cached_errno= errno;
234 if (errno == EINPROGRESS || /* nonblocking mode - first return, */
235 errno == EALREADY) /* nonblocking mode - subsequent returns */
236 {
237 struct pollfd fds[1] = { [0].fd = ptr->fd, [0].events = POLLOUT };
238 int error= poll(fds, 1, ptr->root->connect_timeout);
239
240 if (error != 1 || fds[0].revents & POLLERR)
241 {
242 if (fds[0].revents & POLLERR)
243 {
244 int err;
245 int len = sizeof (err);
246 (void)getsockopt(ptr->fd, SOL_SOCKET, SO_ERROR, &err, &len);
247 ptr->cached_errno= (err == 0) ? errno : err;
248 }
249
250 (void)close(ptr->fd);
251 ptr->fd= -1;
252 }
253 }
254 else if (errno == EISCONN) /* we are connected :-) */
255 {
256 break;
257 }
258 else if (errno != EINTR)
259 {
260 (void)close(ptr->fd);
261 ptr->fd= -1;
262 break;
263 }
264 }
265
266 if (ptr->fd != -1)
267 {
268 /* restore flags */
269 if (ptr->root->connect_timeout && (ptr->root->flags & MEM_NO_BLOCK) == 0)
270 (void)fcntl(ptr->fd, F_SETFL, flags & ~O_NONBLOCK);
271
272 WATCHPOINT_ASSERT(ptr->cursor_active == 0);
273 ptr->server_failure_counter= 0;
274 return MEMCACHED_SUCCESS;
275 }
276 use = use->ai_next;
277 }
278 }
279
280 if (ptr->fd == -1)
281 {
282 /* Failed to connect. schedule next retry */
283 if (ptr->root->retry_timeout)
284 {
285 struct timeval next_time;
286
287 if (gettimeofday(&next_time, NULL) == 0)
288 ptr->next_retry= next_time.tv_sec + ptr->root->retry_timeout;
289 }
290 ptr->server_failure_counter+= 1;
291 if (ptr->cached_errno == 0)
292 return MEMCACHED_TIMEOUT;
293 return MEMCACHED_ERRNO; /* The last error should be from connect() */
294 }
295
296 ptr->server_failure_counter= 0;
297 return MEMCACHED_SUCCESS; /* The last error should be from connect() */
298 }
299
300
301 memcached_return memcached_connect(memcached_server_st *ptr)
302 {
303 memcached_return rc= MEMCACHED_NO_SERVERS;
304 LIBMEMCACHED_MEMCACHED_CONNECT_START();
305
306 /* both retry_timeout and server_failure_limit must be set in order to delay retrying a server on error. */
307 if (ptr->root->retry_timeout && ptr->root->server_failure_limit)
308 {
309 struct timeval next_time;
310
311 gettimeofday(&next_time, NULL);
312
313 /* if we've had too many consecutive errors on this server, mark it dead. */
314 if (ptr->server_failure_counter > ptr->root->server_failure_limit)
315 {
316 ptr->next_retry= next_time.tv_sec + ptr->root->retry_timeout;
317 ptr->server_failure_counter= 0;
318 }
319
320 if (next_time.tv_sec < ptr->next_retry)
321 {
322 if (memcached_behavior_get(ptr->root, MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS))
323 run_distribution(ptr->root);
324
325 return MEMCACHED_SERVER_MARKED_DEAD;
326 }
327 }
328
329 /* We need to clean up the multi startup piece */
330 switch (ptr->type)
331 {
332 case MEMCACHED_CONNECTION_UNKNOWN:
333 WATCHPOINT_ASSERT(0);
334 rc= MEMCACHED_NOT_SUPPORTED;
335 break;
336 case MEMCACHED_CONNECTION_UDP:
337 case MEMCACHED_CONNECTION_TCP:
338 rc= network_connect(ptr);
339 break;
340 case MEMCACHED_CONNECTION_UNIX_SOCKET:
341 rc= unix_socket_connect(ptr);
342 break;
343 default:
344 WATCHPOINT_ASSERT(0);
345 }
346
347 LIBMEMCACHED_MEMCACHED_CONNECT_END();
348
349 return rc;
350 }