5 static memcached_return
set_hostinfo(memcached_server_st
*server
)
10 char str_port
[NI_MAXSERV
];
12 sprintf(str_port
, "%u", server
->port
);
14 memset(&hints
, 0, sizeof(hints
));
16 // hints.ai_family= AF_INET;
17 if (server
->type
== MEMCACHED_CONNECTION_UDP
)
19 hints
.ai_protocol
= IPPROTO_UDP
;
20 hints
.ai_socktype
= SOCK_DGRAM
;
24 hints
.ai_socktype
= SOCK_STREAM
;
25 hints
.ai_protocol
= IPPROTO_TCP
;
28 e
= getaddrinfo(server
->hostname
, str_port
, &hints
, &ai
);
31 WATCHPOINT_STRING(server
->hostname
);
32 WATCHPOINT_STRING(gai_strerror(e
));
33 return MEMCACHED_HOST_LOOKUP_FAILURE
;
36 if (server
->address_info
)
38 freeaddrinfo(server
->address_info
);
39 server
->address_info
= NULL
;
41 server
->address_info
= ai
;
43 return MEMCACHED_SUCCESS
;
46 static memcached_return
set_socket_options(memcached_server_st
*ptr
)
48 if (ptr
->type
== MEMCACHED_CONNECTION_UDP
)
49 return MEMCACHED_SUCCESS
;
51 if (ptr
->root
->snd_timeout
)
54 struct timeval waittime
;
57 waittime
.tv_usec
= ptr
->root
->snd_timeout
;
59 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_SNDTIMEO
,
60 &waittime
, (socklen_t
)sizeof(struct timeval
));
61 WATCHPOINT_ASSERT(error
== 0);
64 if (ptr
->root
->rcv_timeout
)
67 struct timeval waittime
;
70 waittime
.tv_usec
= ptr
->root
->rcv_timeout
;
72 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_RCVTIMEO
,
73 &waittime
, (socklen_t
)sizeof(struct timeval
));
74 WATCHPOINT_ASSERT(error
== 0);
82 linger
.l_linger
= MEMCACHED_DEFAULT_TIMEOUT
;
83 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_LINGER
,
84 &linger
, (socklen_t
)sizeof(struct linger
));
85 WATCHPOINT_ASSERT(error
== 0);
88 if (ptr
->root
->flags
& MEM_TCP_NODELAY
)
93 error
= setsockopt(ptr
->fd
, IPPROTO_TCP
, TCP_NODELAY
,
94 &flag
, (socklen_t
)sizeof(int));
95 WATCHPOINT_ASSERT(error
== 0);
98 if (ptr
->root
->send_size
)
102 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_SNDBUF
,
103 &ptr
->root
->send_size
, (socklen_t
)sizeof(int));
104 WATCHPOINT_ASSERT(error
== 0);
107 if (ptr
->root
->recv_size
)
111 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_SNDBUF
,
112 &ptr
->root
->recv_size
, (socklen_t
)sizeof(int));
113 WATCHPOINT_ASSERT(error
== 0);
116 /* For the moment, not getting a nonblocking mode will not be fatal */
117 if (ptr
->root
->flags
& MEM_NO_BLOCK
)
121 flags
= fcntl(ptr
->fd
, F_GETFL
, 0);
122 unlikely (flags
!= -1)
124 (void)fcntl(ptr
->fd
, F_SETFL
, flags
| O_NONBLOCK
);
128 return MEMCACHED_SUCCESS
;
131 static memcached_return
unix_socket_connect(memcached_server_st
*ptr
)
133 struct sockaddr_un servAddr
;
138 if ((ptr
->fd
= socket(AF_UNIX
, SOCK_STREAM
, 0)) < 0)
140 ptr
->cached_errno
= errno
;
141 return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE
;
144 memset(&servAddr
, 0, sizeof (struct sockaddr_un
));
145 servAddr
.sun_family
= AF_UNIX
;
146 strcpy(servAddr
.sun_path
, ptr
->hostname
); /* Copy filename */
148 addrlen
= strlen(servAddr
.sun_path
) + sizeof(servAddr
.sun_family
);
152 (struct sockaddr
*)&servAddr
,
153 sizeof(servAddr
)) < 0)
160 case EISCONN
: /* We were spinning waiting on connect */
163 WATCHPOINT_ERRNO(errno
);
164 ptr
->cached_errno
= errno
;
165 return MEMCACHED_ERRNO
;
169 return MEMCACHED_SUCCESS
;
172 static memcached_return
network_connect(memcached_server_st
*ptr
)
176 struct addrinfo
*use
;
178 if (ptr
->root
->server_failure_limit
!= 0)
180 if (ptr
->server_failure_counter
>= ptr
->root
->server_failure_limit
)
182 memcached_server_remove(ptr
);
183 return MEMCACHED_FAILURE
;
186 /* Old connection junk still is in the structure */
187 WATCHPOINT_ASSERT(ptr
->cursor_active
== 0);
189 if (ptr
->sockaddr_inited
== MEMCACHED_NOT_ALLOCATED
||
190 (!(ptr
->root
->flags
& MEM_USE_CACHE_LOOKUPS
)))
194 rc
= set_hostinfo(ptr
);
195 if (rc
!= MEMCACHED_SUCCESS
)
197 ptr
->sockaddr_inited
= MEMCACHED_ALLOCATED
;
200 use
= ptr
->address_info
;
201 /* Create the socket */
204 if ((ptr
->fd
= socket(use
->ai_family
,
206 use
->ai_protocol
)) < 0)
208 ptr
->cached_errno
= errno
;
209 WATCHPOINT_ERRNO(errno
);
210 return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE
;
213 (void)set_socket_options(ptr
);
215 /* connect to server */
219 use
->ai_addrlen
) < 0)
222 /* We are spinning waiting on connect */
226 struct pollfd fds
[1];
229 memset(&fds
, 0, sizeof(struct pollfd
));
231 fds
[0].events
= POLLOUT
| POLLERR
;
232 error
= poll(fds
, 1, ptr
->root
->connect_timeout
);
238 else if (error
!= 1 || fds
[0].revents
& POLLERR
)
240 ptr
->cached_errno
= errno
;
241 WATCHPOINT_ERRNO(ptr
->cached_errno
);
242 WATCHPOINT_NUMBER(ptr
->root
->connect_timeout
);
245 if (ptr
->address_info
)
247 freeaddrinfo(ptr
->address_info
);
248 ptr
->address_info
= NULL
;
251 if (ptr
->root
->retry_timeout
)
253 struct timeval next_time
;
255 gettimeofday(&next_time
, NULL
);
256 ptr
->next_retry
= next_time
.tv_sec
+ ptr
->root
->retry_timeout
;
258 ptr
->server_failure_counter
+= 1;
259 return MEMCACHED_ERRNO
;
264 /* We are spinning waiting on connect */
267 case EISCONN
: /* We were spinning waiting on connect */
271 ptr
->cached_errno
= errno
;
274 if (ptr
->root
->retry_timeout
)
276 struct timeval next_time
;
278 gettimeofday(&next_time
, NULL
);
279 ptr
->next_retry
= next_time
.tv_sec
+ ptr
->root
->retry_timeout
;
285 WATCHPOINT_ASSERT(ptr
->cursor_active
== 0);
286 ptr
->server_failure_counter
= 0;
287 return MEMCACHED_SUCCESS
;
294 ptr
->server_failure_counter
+= 1;
295 return MEMCACHED_ERRNO
; /* The last error should be from connect() */
298 ptr
->server_failure_counter
= 0;
299 return MEMCACHED_SUCCESS
; /* The last error should be from connect() */
303 memcached_return
memcached_connect(memcached_server_st
*ptr
)
305 memcached_return rc
= MEMCACHED_NO_SERVERS
;
306 LIBMEMCACHED_MEMCACHED_CONNECT_START();
308 if (ptr
->root
->retry_timeout
)
310 struct timeval next_time
;
312 gettimeofday(&next_time
, NULL
);
313 if (next_time
.tv_sec
< ptr
->next_retry
)
314 return MEMCACHED_TIMEOUT
;
316 /* We need to clean up the multi startup piece */
319 case MEMCACHED_CONNECTION_UNKNOWN
:
320 WATCHPOINT_ASSERT(0);
321 rc
= MEMCACHED_NOT_SUPPORTED
;
323 case MEMCACHED_CONNECTION_UDP
:
324 case MEMCACHED_CONNECTION_TCP
:
325 rc
= network_connect(ptr
);
327 case MEMCACHED_CONNECTION_UNIX_SOCKET
:
328 rc
= unix_socket_connect(ptr
);
331 WATCHPOINT_ASSERT(0);
334 LIBMEMCACHED_MEMCACHED_CONNECT_END();