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 /* libmemcached will always use nonblocking IO to avoid write deadlocks */
128 flags
= fcntl(ptr
->fd
, F_GETFL
, 0);
129 while (flags
== -1 && (errno
== EINTR
|| errno
== EAGAIN
));
131 unlikely (flags
== -1)
132 return MEMCACHED_CONNECTION_FAILURE
;
133 else if ((flags
& O_NONBLOCK
) == 0)
138 rval
= fcntl(ptr
->fd
, F_SETFL
, flags
| O_NONBLOCK
);
139 while (rval
== -1 && (errno
== EINTR
|| errno
== EAGAIN
));
141 unlikely (rval
== -1)
142 return MEMCACHED_CONNECTION_FAILURE
;
145 return MEMCACHED_SUCCESS
;
148 static memcached_return
unix_socket_connect(memcached_server_st
*ptr
)
150 struct sockaddr_un servAddr
;
155 if ((ptr
->fd
= socket(AF_UNIX
, SOCK_STREAM
, 0)) < 0)
157 ptr
->cached_errno
= errno
;
158 return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE
;
161 memset(&servAddr
, 0, sizeof (struct sockaddr_un
));
162 servAddr
.sun_family
= AF_UNIX
;
163 strcpy(servAddr
.sun_path
, ptr
->hostname
); /* Copy filename */
165 addrlen
= (socklen_t
) (strlen(servAddr
.sun_path
) + sizeof(servAddr
.sun_family
));
169 (struct sockaddr
*)&servAddr
,
170 sizeof(servAddr
)) < 0)
178 case EISCONN
: /* We were spinning waiting on connect */
181 WATCHPOINT_ERRNO(errno
);
182 ptr
->cached_errno
= errno
;
183 return MEMCACHED_ERRNO
;
188 WATCHPOINT_ASSERT(ptr
->fd
!= -1);
189 return MEMCACHED_SUCCESS
;
192 static memcached_return
network_connect(memcached_server_st
*ptr
)
196 struct addrinfo
*use
;
198 if (!ptr
->sockaddr_inited
||
199 (!(ptr
->root
->flags
& MEM_USE_CACHE_LOOKUPS
)))
203 rc
= set_hostinfo(ptr
);
204 if (rc
!= MEMCACHED_SUCCESS
)
206 ptr
->sockaddr_inited
= true;
209 use
= ptr
->address_info
;
210 /* Create the socket */
213 /* Memcache server does not support IPV6 in udp mode, so skip if not ipv4 */
214 if (ptr
->type
== MEMCACHED_CONNECTION_UDP
&& use
->ai_family
!= AF_INET
)
220 if ((ptr
->fd
= socket(use
->ai_family
,
222 use
->ai_protocol
)) < 0)
224 ptr
->cached_errno
= errno
;
225 WATCHPOINT_ERRNO(errno
);
226 return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE
;
229 (void)set_socket_options(ptr
);
231 /* connect to server */
232 while (ptr
->fd
!= -1 &&
233 connect(ptr
->fd
, use
->ai_addr
, use
->ai_addrlen
) < 0)
235 ptr
->cached_errno
= errno
;
236 if (errno
== EINPROGRESS
|| /* nonblocking mode - first return, */
237 errno
== EALREADY
) /* nonblocking mode - subsequent returns */
239 struct pollfd fds
[1];
241 fds
[0].events
= POLLOUT
;
242 int error
= poll(fds
, 1, ptr
->root
->connect_timeout
);
244 if (error
!= 1 || fds
[0].revents
& POLLERR
)
246 if (fds
[0].revents
& POLLERR
)
249 socklen_t len
= sizeof (err
);
250 (void)getsockopt(ptr
->fd
, SOL_SOCKET
, SO_ERROR
, &err
, &len
);
251 ptr
->cached_errno
= (err
== 0) ? errno
: err
;
254 (void)close(ptr
->fd
);
258 else if (errno
== EISCONN
) /* we are connected :-) */
262 else if (errno
!= EINTR
)
264 (void)close(ptr
->fd
);
272 WATCHPOINT_ASSERT(ptr
->cursor_active
== 0);
273 ptr
->server_failure_counter
= 0;
274 return MEMCACHED_SUCCESS
;
282 /* Failed to connect. schedule next retry */
283 if (ptr
->root
->retry_timeout
)
285 struct timeval next_time
;
287 if (gettimeofday(&next_time
, NULL
) == 0)
288 ptr
->next_retry
= next_time
.tv_sec
+ ptr
->root
->retry_timeout
;
290 ptr
->server_failure_counter
++;
291 if (ptr
->cached_errno
== 0)
292 return MEMCACHED_TIMEOUT
;
294 return MEMCACHED_ERRNO
; /* The last error should be from connect() */
297 ptr
->server_failure_counter
= 0;
298 return MEMCACHED_SUCCESS
; /* The last error should be from connect() */
302 memcached_return
memcached_connect(memcached_server_st
*ptr
)
304 memcached_return rc
= MEMCACHED_NO_SERVERS
;
305 LIBMEMCACHED_MEMCACHED_CONNECT_START();
307 /* both retry_timeout and server_failure_limit must be set in order to delay retrying a server on error. */
308 WATCHPOINT_ASSERT(ptr
->root
);
309 if (ptr
->root
->retry_timeout
&& ptr
->root
->server_failure_limit
)
311 struct timeval curr_time
;
313 gettimeofday(&curr_time
, NULL
);
315 /* if we've had too many consecutive errors on this server, mark it dead. */
316 if (ptr
->server_failure_counter
>= ptr
->root
->server_failure_limit
)
318 ptr
->next_retry
= curr_time
.tv_sec
+ ptr
->root
->retry_timeout
;
319 ptr
->server_failure_counter
= 0;
322 if (curr_time
.tv_sec
< ptr
->next_retry
)
324 if (memcached_behavior_get(ptr
->root
, MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS
))
325 run_distribution(ptr
->root
);
327 ptr
->root
->last_disconnected_server
= ptr
;
328 return MEMCACHED_SERVER_MARKED_DEAD
;
332 /* We need to clean up the multi startup piece */
335 case MEMCACHED_CONNECTION_UNKNOWN
:
336 WATCHPOINT_ASSERT(0);
337 rc
= MEMCACHED_NOT_SUPPORTED
;
339 case MEMCACHED_CONNECTION_UDP
:
340 case MEMCACHED_CONNECTION_TCP
:
341 rc
= network_connect(ptr
);
343 case MEMCACHED_CONNECTION_UNIX_SOCKET
:
344 rc
= unix_socket_connect(ptr
);
347 WATCHPOINT_ASSERT(0);
350 unlikely ( rc
!= MEMCACHED_SUCCESS
) ptr
->root
->last_disconnected_server
= ptr
;
352 LIBMEMCACHED_MEMCACHED_CONNECT_END();