Fix connect with timeouts
[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)
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 case EINPROGRESS:
164 case EALREADY:
165 case EINTR:
166 goto test_connect;
167 case EISCONN: /* We were spinning waiting on connect */
168 break;
169 default:
170 WATCHPOINT_ERRNO(errno);
171 ptr->cached_errno= errno;
172 return MEMCACHED_ERRNO;
173 }
174 }
175 }
176
177 WATCHPOINT_ASSERT(ptr->fd != -1);
178 return MEMCACHED_SUCCESS;
179 }
180
181 static memcached_return network_connect(memcached_server_st *ptr)
182 {
183 if (ptr->fd == -1)
184 {
185 struct addrinfo *use;
186
187 if (ptr->root->server_failure_limit != 0)
188 {
189 if (ptr->server_failure_counter >= ptr->root->server_failure_limit)
190 {
191 memcached_server_remove(ptr);
192 return MEMCACHED_FAILURE;
193 }
194 }
195
196 if (ptr->sockaddr_inited ||
197 (!(ptr->root->flags & MEM_USE_CACHE_LOOKUPS)))
198 {
199 memcached_return rc;
200
201 rc= set_hostinfo(ptr);
202 if (rc != MEMCACHED_SUCCESS)
203 return rc;
204 ptr->sockaddr_inited= true;
205 }
206
207 use= ptr->address_info;
208 /* Create the socket */
209 while (use != NULL)
210 {
211 if ((ptr->fd= socket(use->ai_family,
212 use->ai_socktype,
213 use->ai_protocol)) < 0)
214 {
215 ptr->cached_errno= errno;
216 WATCHPOINT_ERRNO(errno);
217 return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE;
218 }
219
220 (void)set_socket_options(ptr);
221
222 int flags;
223 if (ptr->root->connect_timeout)
224 {
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);
228 }
229
230 /* connect to server */
231 while (ptr->fd != -1 &&
232 connect(ptr->fd, use->ai_addr, use->ai_addrlen) < 0)
233 {
234 ptr->cached_errno= errno;
235 if (errno == EINPROGRESS || /* nonblocking mode - first return, */
236 errno == EALREADY) /* nonblocking mode - subsequent returns */
237 {
238 struct pollfd fds[1] = { [0].fd = ptr->fd, [0].events = POLLOUT };
239 int error= poll(fds, 1, ptr->root->connect_timeout);
240
241 if (error != 1 || fds[0].revents & POLLERR)
242 {
243 if (fds[0].revents & POLLERR)
244 {
245 int err;
246 int len = sizeof (err);
247 (void)getsockopt(ptr->fd, SOL_SOCKET, SO_ERROR, &err, &len);
248 ptr->cached_errno= errno;
249 }
250
251 (void)close(ptr->fd);
252 ptr->fd= -1;
253 }
254 }
255 else if (errno == EISCONN) /* we are connected :-) */
256 {
257 break;
258 }
259 else if (errno != EINTR)
260 {
261 (void)close(ptr->fd);
262 ptr->fd= -1;
263 break;
264 }
265 }
266
267 if (ptr->fd != -1)
268 {
269 /* restore flags */
270 if (ptr->root->connect_timeout && (flags & O_NONBLOCK) == 0)
271 (void)fcntl(ptr->fd, F_SETFL, flags & ~O_NONBLOCK);
272
273 WATCHPOINT_ASSERT(ptr->cursor_active == 0);
274 ptr->server_failure_counter= 0;
275 return MEMCACHED_SUCCESS;
276 }
277 use = use->ai_next;
278 }
279 }
280
281 if (ptr->fd == -1)
282 {
283 /* Failed to connect. schedule next retry */
284 if (ptr->root->retry_timeout)
285 {
286 struct timeval next_time;
287
288 if (gettimeofday(&next_time, NULL) == 0)
289 ptr->next_retry= next_time.tv_sec + ptr->root->retry_timeout;
290 }
291 ptr->server_failure_counter+= 1;
292 return MEMCACHED_ERRNO; /* The last error should be from connect() */
293 }
294
295 ptr->server_failure_counter= 0;
296 return MEMCACHED_SUCCESS; /* The last error should be from connect() */
297 }
298
299
300 memcached_return memcached_connect(memcached_server_st *ptr)
301 {
302 memcached_return rc= MEMCACHED_NO_SERVERS;
303 LIBMEMCACHED_MEMCACHED_CONNECT_START();
304
305 if (ptr->root->retry_timeout)
306 {
307 struct timeval next_time;
308
309 gettimeofday(&next_time, NULL);
310 if (next_time.tv_sec < ptr->next_retry)
311 return MEMCACHED_TIMEOUT;
312 }
313 /* We need to clean up the multi startup piece */
314 switch (ptr->type)
315 {
316 case MEMCACHED_CONNECTION_UNKNOWN:
317 WATCHPOINT_ASSERT(0);
318 rc= MEMCACHED_NOT_SUPPORTED;
319 break;
320 case MEMCACHED_CONNECTION_UDP:
321 case MEMCACHED_CONNECTION_TCP:
322 rc= network_connect(ptr);
323 break;
324 case MEMCACHED_CONNECTION_UNIX_SOCKET:
325 rc= unix_socket_connect(ptr);
326 break;
327 default:
328 WATCHPOINT_ASSERT(0);
329 }
330
331 LIBMEMCACHED_MEMCACHED_CONNECT_END();
332
333 return rc;
334 }