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 if (ptr
->type
== MEMCACHED_CONNECTION_UDP
)
50 return MEMCACHED_SUCCESS
;
52 if (ptr
->root
->snd_timeout
)
55 struct timeval waittime
;
58 waittime
.tv_usec
= ptr
->root
->snd_timeout
;
60 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_SNDTIMEO
,
61 &waittime
, (socklen_t
)sizeof(struct timeval
));
62 WATCHPOINT_ASSERT(error
== 0);
65 if (ptr
->root
->rcv_timeout
)
68 struct timeval waittime
;
71 waittime
.tv_usec
= ptr
->root
->rcv_timeout
;
73 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_RCVTIMEO
,
74 &waittime
, (socklen_t
)sizeof(struct timeval
));
75 WATCHPOINT_ASSERT(error
== 0);
83 linger
.l_linger
= MEMCACHED_DEFAULT_TIMEOUT
;
84 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_LINGER
,
85 &linger
, (socklen_t
)sizeof(struct linger
));
86 WATCHPOINT_ASSERT(error
== 0);
89 if (ptr
->root
->flags
& MEM_TCP_NODELAY
)
94 error
= setsockopt(ptr
->fd
, IPPROTO_TCP
, TCP_NODELAY
,
95 &flag
, (socklen_t
)sizeof(int));
96 WATCHPOINT_ASSERT(error
== 0);
99 if (ptr
->root
->send_size
)
103 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_SNDBUF
,
104 &ptr
->root
->send_size
, (socklen_t
)sizeof(int));
105 WATCHPOINT_ASSERT(error
== 0);
108 if (ptr
->root
->recv_size
)
112 error
= setsockopt(ptr
->fd
, SOL_SOCKET
, SO_SNDBUF
,
113 &ptr
->root
->recv_size
, (socklen_t
)sizeof(int));
114 WATCHPOINT_ASSERT(error
== 0);
117 /* For the moment, not getting a nonblocking mode will not be fatal */
118 if (ptr
->root
->flags
& MEM_NO_BLOCK
)
122 flags
= fcntl(ptr
->fd
, F_GETFL
, 0);
123 unlikely (flags
!= -1)
125 (void)fcntl(ptr
->fd
, F_SETFL
, flags
| O_NONBLOCK
);
129 return MEMCACHED_SUCCESS
;
132 static memcached_return
unix_socket_connect(memcached_server_st
*ptr
)
134 struct sockaddr_un servAddr
;
139 if ((ptr
->fd
= socket(AF_UNIX
, SOCK_STREAM
, 0)) < 0)
141 ptr
->cached_errno
= errno
;
142 return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE
;
145 memset(&servAddr
, 0, sizeof (struct sockaddr_un
));
146 servAddr
.sun_family
= AF_UNIX
;
147 strcpy(servAddr
.sun_path
, ptr
->hostname
); /* Copy filename */
149 addrlen
= strlen(servAddr
.sun_path
) + sizeof(servAddr
.sun_family
);
153 (struct sockaddr
*)&servAddr
,
154 sizeof(servAddr
)) < 0)
161 case EISCONN
: /* We were spinning waiting on connect */
164 WATCHPOINT_ERRNO(errno
);
165 ptr
->cached_errno
= errno
;
166 return MEMCACHED_ERRNO
;
170 return MEMCACHED_SUCCESS
;
173 static memcached_return
network_connect(memcached_server_st
*ptr
)
177 struct addrinfo
*use
;
179 if (ptr
->root
->server_failure_limit
!= 0)
181 if (ptr
->server_failure_counter
>= ptr
->root
->server_failure_limit
)
183 memcached_server_remove(ptr
);
184 return MEMCACHED_FAILURE
;
187 /* Old connection junk still is in the structure */
188 WATCHPOINT_ASSERT(ptr
->cursor_active
== 0);
190 if (ptr
->sockaddr_inited
== MEMCACHED_NOT_ALLOCATED
||
191 (!(ptr
->root
->flags
& MEM_USE_CACHE_LOOKUPS
)))
195 rc
= set_hostinfo(ptr
);
196 if (rc
!= MEMCACHED_SUCCESS
)
198 ptr
->sockaddr_inited
= MEMCACHED_ALLOCATED
;
201 use
= ptr
->address_info
;
202 /* Create the socket */
205 if ((ptr
->fd
= socket(use
->ai_family
,
207 use
->ai_protocol
)) < 0)
209 ptr
->cached_errno
= errno
;
210 WATCHPOINT_ERRNO(errno
);
211 return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE
;
214 (void)set_socket_options(ptr
);
216 /* connect to server */
220 use
->ai_addrlen
) < 0)
223 /* We are spinning waiting on connect */
227 struct pollfd fds
[1];
230 memset(&fds
, 0, sizeof(struct pollfd
));
232 fds
[0].events
= POLLOUT
| POLLERR
;
233 error
= poll(fds
, 1, ptr
->root
->connect_timeout
);
239 else if (error
!= 1 || fds
[0].revents
& POLLERR
)
241 ptr
->cached_errno
= errno
;
242 WATCHPOINT_ERRNO(ptr
->cached_errno
);
243 WATCHPOINT_NUMBER(ptr
->root
->connect_timeout
);
246 if (ptr
->address_info
)
248 freeaddrinfo(ptr
->address_info
);
249 ptr
->address_info
= NULL
;
252 if (ptr
->root
->retry_timeout
)
254 struct timeval next_time
;
256 gettimeofday(&next_time
, NULL
);
257 ptr
->next_retry
= next_time
.tv_sec
+ ptr
->root
->retry_timeout
;
259 ptr
->server_failure_counter
+= 1;
260 return MEMCACHED_ERRNO
;
265 /* We are spinning waiting on connect */
268 case EISCONN
: /* We were spinning waiting on connect */
272 ptr
->cached_errno
= errno
;
275 if (ptr
->root
->retry_timeout
)
277 struct timeval next_time
;
279 gettimeofday(&next_time
, NULL
);
280 ptr
->next_retry
= next_time
.tv_sec
+ ptr
->root
->retry_timeout
;
286 WATCHPOINT_ASSERT(ptr
->cursor_active
== 0);
287 ptr
->server_failure_counter
= 0;
288 return MEMCACHED_SUCCESS
;
295 ptr
->server_failure_counter
+= 1;
296 return MEMCACHED_ERRNO
; /* The last error should be from connect() */
299 ptr
->server_failure_counter
= 0;
300 return MEMCACHED_SUCCESS
; /* The last error should be from connect() */
304 memcached_return
memcached_connect(memcached_server_st
*ptr
)
306 memcached_return rc
= MEMCACHED_NO_SERVERS
;
307 LIBMEMCACHED_MEMCACHED_CONNECT_START();
309 if (ptr
->root
->retry_timeout
)
311 struct timeval next_time
;
313 gettimeofday(&next_time
, NULL
);
314 if (next_time
.tv_sec
< ptr
->next_retry
)
315 return MEMCACHED_TIMEOUT
;
317 /* We need to clean up the multi startup piece */
320 case MEMCACHED_CONNECTION_UNKNOWN
:
321 WATCHPOINT_ASSERT(0);
322 rc
= MEMCACHED_NOT_SUPPORTED
;
324 case MEMCACHED_CONNECTION_UDP
:
325 case MEMCACHED_CONNECTION_TCP
:
326 rc
= network_connect(ptr
);
328 case MEMCACHED_CONNECTION_UNIX_SOCKET
:
329 rc
= unix_socket_connect(ptr
);
332 WATCHPOINT_ASSERT(0);
335 LIBMEMCACHED_MEMCACHED_CONNECT_END();