6 static memcached_return_t
set_hostinfo(memcached_server_st
*server
)
11 char str_port
[NI_MAXSERV
];
13 snprintf(str_port
, NI_MAXSERV
, "%u", (uint32_t)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_t
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);
67 return MEMCACHED_FAILURE
;
72 if (ptr
->root
->rcv_timeout
)
75 struct timeval waittime
;
78 waittime
.tv_usec
= ptr
->root
->rcv_timeout
;
80 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_RCVTIMEO
,
81 &waittime
, (socklen_t
)sizeof(struct timeval
));
82 WATCHPOINT_ASSERT(error
== 0);
84 return MEMCACHED_FAILURE
;
88 if (ptr
->root
->flags
.no_block
)
94 linger
.l_linger
= 0; /* By default on close() just drop the socket */
95 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_LINGER
,
96 &linger
, (socklen_t
)sizeof(struct linger
));
97 WATCHPOINT_ASSERT(error
== 0);
99 return MEMCACHED_FAILURE
;
102 if (ptr
->root
->flags
.tcp_nodelay
)
107 error
= setsockopt(ptr
->fd
, IPPROTO_TCP
, TCP_NODELAY
,
108 &flag
, (socklen_t
)sizeof(int));
109 WATCHPOINT_ASSERT(error
== 0);
111 return MEMCACHED_FAILURE
;
114 if (ptr
->root
->send_size
)
118 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_SNDBUF
,
119 &ptr
->root
->send_size
, (socklen_t
)sizeof(int));
120 WATCHPOINT_ASSERT(error
== 0);
122 return MEMCACHED_FAILURE
;
125 if (ptr
->root
->recv_size
)
129 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_RCVBUF
,
130 &ptr
->root
->recv_size
, (socklen_t
)sizeof(int));
131 WATCHPOINT_ASSERT(error
== 0);
133 return MEMCACHED_FAILURE
;
136 /* libmemcached will always use nonblocking IO to avoid write deadlocks */
140 flags
= fcntl(ptr
->fd
, F_GETFL
, 0);
141 while (flags
== -1 && (errno
== EINTR
|| errno
== EAGAIN
));
143 unlikely (flags
== -1)
145 return MEMCACHED_CONNECTION_FAILURE
;
147 else if ((flags
& O_NONBLOCK
) == 0)
152 rval
= fcntl(ptr
->fd
, F_SETFL
, flags
| O_NONBLOCK
);
153 while (rval
== -1 && (errno
== EINTR
|| errno
== EAGAIN
));
155 unlikely (rval
== -1)
157 return MEMCACHED_CONNECTION_FAILURE
;
161 return MEMCACHED_SUCCESS
;
164 static memcached_return_t
unix_socket_connect(memcached_server_st
*ptr
)
166 struct sockaddr_un servAddr
;
170 if ((ptr
->fd
= socket(AF_UNIX
, SOCK_STREAM
, 0)) < 0)
172 ptr
->cached_errno
= errno
;
173 return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE
;
176 memset(&servAddr
, 0, sizeof (struct sockaddr_un
));
177 servAddr
.sun_family
= AF_UNIX
;
178 strcpy(servAddr
.sun_path
, ptr
->hostname
); /* Copy filename */
182 (struct sockaddr
*)&servAddr
,
183 sizeof(servAddr
)) < 0)
191 case EISCONN
: /* We were spinning waiting on connect */
194 WATCHPOINT_ERRNO(errno
);
195 ptr
->cached_errno
= errno
;
196 return MEMCACHED_ERRNO
;
201 WATCHPOINT_ASSERT(ptr
->fd
!= -1);
202 return MEMCACHED_SUCCESS
;
205 static memcached_return_t
network_connect(memcached_server_st
*ptr
)
209 struct addrinfo
*use
;
211 if (!ptr
->sockaddr_inited
||
212 (!(ptr
->root
->flags
.use_cache_lookups
)))
214 memcached_return_t rc
;
216 rc
= set_hostinfo(ptr
);
217 if (rc
!= MEMCACHED_SUCCESS
)
219 ptr
->sockaddr_inited
= true;
222 use
= ptr
->address_info
;
223 /* Create the socket */
226 /* Memcache server does not support IPV6 in udp mode, so skip if not ipv4 */
227 if (ptr
->type
== MEMCACHED_CONNECTION_UDP
&& use
->ai_family
!= AF_INET
)
233 if ((ptr
->fd
= socket(use
->ai_family
,
235 use
->ai_protocol
)) < 0)
237 ptr
->cached_errno
= errno
;
238 WATCHPOINT_ERRNO(errno
);
239 return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE
;
242 (void)set_socket_options(ptr
);
244 /* connect to server */
245 while (ptr
->fd
!= -1 &&
246 connect(ptr
->fd
, use
->ai_addr
, use
->ai_addrlen
) < 0)
248 ptr
->cached_errno
= errno
;
249 if (errno
== EINPROGRESS
|| /* nonblocking mode - first return, */
250 errno
== EALREADY
) /* nonblocking mode - subsequent returns */
252 struct pollfd fds
[1];
254 fds
[0].events
= POLLOUT
;
255 int error
= poll(fds
, 1, ptr
->root
->connect_timeout
);
257 if (error
!= 1 || fds
[0].revents
& POLLERR
)
259 if (fds
[0].revents
& POLLERR
)
262 socklen_t len
= sizeof (err
);
263 (void)getsockopt(ptr
->fd
, SOL_SOCKET
, SO_ERROR
, &err
, &len
);
264 ptr
->cached_errno
= (err
== 0) ? errno
: err
;
267 (void)close(ptr
->fd
);
271 else if (errno
== EISCONN
) /* we are connected :-) */
275 else if (errno
!= EINTR
)
277 (void)close(ptr
->fd
);
285 WATCHPOINT_ASSERT(ptr
->cursor_active
== 0);
286 ptr
->server_failure_counter
= 0;
287 return MEMCACHED_SUCCESS
;
295 /* Failed to connect. schedule next retry */
296 if (ptr
->root
->retry_timeout
)
298 struct timeval next_time
;
300 if (gettimeofday(&next_time
, NULL
) == 0)
301 ptr
->next_retry
= next_time
.tv_sec
+ ptr
->root
->retry_timeout
;
303 ptr
->server_failure_counter
++;
304 if (ptr
->cached_errno
== 0)
305 return MEMCACHED_TIMEOUT
;
307 return MEMCACHED_ERRNO
; /* The last error should be from connect() */
310 ptr
->server_failure_counter
= 0;
311 return MEMCACHED_SUCCESS
; /* The last error should be from connect() */
315 memcached_return_t
memcached_connect(memcached_server_st
*ptr
)
317 memcached_return_t rc
= MEMCACHED_NO_SERVERS
;
318 LIBMEMCACHED_MEMCACHED_CONNECT_START();
320 /* both retry_timeout and server_failure_limit must be set in order to delay retrying a server on error. */
321 WATCHPOINT_ASSERT(ptr
->root
);
322 if (ptr
->root
->retry_timeout
&& ptr
->root
->server_failure_limit
)
324 struct timeval curr_time
;
326 gettimeofday(&curr_time
, NULL
);
328 /* if we've had too many consecutive errors on this server, mark it dead. */
329 if (ptr
->server_failure_counter
>= ptr
->root
->server_failure_limit
)
331 ptr
->next_retry
= curr_time
.tv_sec
+ ptr
->root
->retry_timeout
;
332 ptr
->server_failure_counter
= 0;
335 if (curr_time
.tv_sec
< ptr
->next_retry
)
337 if (memcached_behavior_get(ptr
->root
, MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS
))
339 run_distribution(ptr
->root
);
342 ptr
->root
->last_disconnected_server
= ptr
;
343 return MEMCACHED_SERVER_MARKED_DEAD
;
347 /* We need to clean up the multi startup piece */
350 case MEMCACHED_CONNECTION_UNKNOWN
:
351 WATCHPOINT_ASSERT(0);
352 rc
= MEMCACHED_NOT_SUPPORTED
;
354 case MEMCACHED_CONNECTION_UDP
:
355 case MEMCACHED_CONNECTION_TCP
:
356 rc
= network_connect(ptr
);
358 case MEMCACHED_CONNECTION_UNIX_SOCKET
:
359 rc
= unix_socket_connect(ptr
);
361 case MEMCACHED_CONNECTION_MAX
:
363 WATCHPOINT_ASSERT(0);
366 unlikely ( rc
!= MEMCACHED_SUCCESS
) ptr
->root
->last_disconnected_server
= ptr
;
368 LIBMEMCACHED_MEMCACHED_CONNECT_END();