Implemented the TCP_KEEPIDLE tcp option. This is only available on Linux.
[awesomized/libmemcached] / libmemcached / memcached.c
1 /* LibMemcached
2 * Copyright (C) 2006-2010 Brian Aker
3 * All rights reserved.
4 *
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
7 *
8 * Summary:
9 *
10 */
11
12 #include "common.h"
13
14 static const memcached_st global_copy= {
15 .state= {
16 .is_purging= false,
17 .is_processing_input= false,
18 },
19 .flags= {
20 .auto_eject_hosts= false,
21 .binary_protocol= false,
22 .buffer_requests= false,
23 .cork= false,
24 .hash_with_prefix_key= false,
25 .ketama_weighted= false,
26 .no_block= false,
27 .no_reply= false,
28 .randomize_replica_read= false,
29 .reuse_memory= false,
30 .support_cas= false,
31 .tcp_nodelay= false,
32 .use_cache_lookups= false,
33 .use_sort_hosts= false,
34 .use_udp= false,
35 .verify_key= false,
36 .tcp_keepalive= false
37 }
38 };
39
40 static inline bool _memcached_init(memcached_st *self)
41 {
42 self->state= global_copy.state;
43 self->flags= global_copy.flags;
44
45 self->distribution= MEMCACHED_DISTRIBUTION_MODULA;
46
47 hashkit_st *hash_ptr;
48 hash_ptr= hashkit_create(&self->hashkit);
49 if (! hash_ptr)
50 return false;
51
52 self->continuum_points_counter= 0;
53
54 self->number_of_hosts= 0;
55 self->servers= NULL;
56 self->last_disconnected_server= NULL;
57
58 self->snd_timeout= 0;
59 self->rcv_timeout= 0;
60 self->server_failure_limit= 0;
61
62 /* TODO, Document why we picked these defaults */
63 self->io_msg_watermark= 500;
64 self->io_bytes_watermark= 65 * 1024;
65
66 self->tcp_keepidle= 0;
67
68 self->io_key_prefetch= 0;
69 self->cached_errno= 0;
70 self->poll_timeout= MEMCACHED_DEFAULT_TIMEOUT;
71 self->connect_timeout= MEMCACHED_DEFAULT_TIMEOUT;
72 self->retry_timeout= 0;
73 self->continuum_count= 0;
74
75 self->send_size= -1;
76 self->recv_size= -1;
77
78 self->user_data= NULL;
79 self->next_distribution_rebuild= 0;
80 self->prefix_key_length= 0;
81 self->number_of_replicas= 0;
82 hash_ptr= hashkit_create(&self->distribution_hashkit);
83 if (! hash_ptr)
84 return false;
85 self->continuum= NULL;
86
87 self->allocators= memcached_allocators_return_default();
88
89 self->on_clone= NULL;
90 self->on_cleanup= NULL;
91 self->get_key_failure= NULL;
92 self->delete_trigger= NULL;
93 self->callbacks= NULL;
94
95 return true;
96 }
97
98 memcached_st *memcached_create(memcached_st *ptr)
99 {
100 if (ptr == NULL)
101 {
102 ptr= (memcached_st *)malloc(sizeof(memcached_st));
103
104 if (! ptr)
105 {
106 return NULL; /* MEMCACHED_MEMORY_ALLOCATION_FAILURE */
107 }
108
109 ptr->options.is_allocated= true;
110 }
111 else
112 {
113 ptr->options.is_allocated= false;
114 }
115
116 #if 0
117 memcached_set_purging(ptr, false);
118 memcached_set_processing_input(ptr, false);
119 #endif
120
121 if (! _memcached_init(ptr))
122 {
123 memcached_free(ptr);
124 return NULL;
125 }
126
127 if (! memcached_result_create(ptr, &ptr->result))
128 {
129 memcached_free(ptr);
130 return NULL;
131 }
132
133 WATCHPOINT_ASSERT_INITIALIZED(&ptr->result);
134
135 return ptr;
136 }
137
138 void memcached_servers_reset(memcached_st *ptr)
139 {
140 memcached_server_list_free(memcached_server_list(ptr));
141
142 memcached_server_list_set(ptr, NULL);
143 ptr->number_of_hosts= 0;
144 ptr->last_disconnected_server= NULL;
145 ptr->server_failure_limit= 0;
146 }
147
148 void memcached_free(memcached_st *ptr)
149 {
150 /* If we have anything open, lets close it now */
151 memcached_quit(ptr);
152 memcached_server_list_free(memcached_server_list(ptr));
153 memcached_result_free(&ptr->result);
154
155 if (ptr->on_cleanup)
156 ptr->on_cleanup(ptr);
157
158 if (ptr->continuum)
159 libmemcached_free(ptr, ptr->continuum);
160
161 if (memcached_is_allocated(ptr))
162 {
163 libmemcached_free(ptr, ptr);
164 }
165 }
166
167 /*
168 clone is the destination, while source is the structure to clone.
169 If source is NULL the call is the same as if a memcached_create() was
170 called.
171 */
172 memcached_st *memcached_clone(memcached_st *clone, const memcached_st *source)
173 {
174 memcached_return_t rc= MEMCACHED_SUCCESS;
175 memcached_st *new_clone;
176
177 if (source == NULL)
178 return memcached_create(clone);
179
180 if (clone && memcached_is_allocated(clone))
181 {
182 return NULL;
183 }
184
185 new_clone= memcached_create(clone);
186
187 if (new_clone == NULL)
188 return NULL;
189
190 new_clone->flags= source->flags;
191 new_clone->send_size= source->send_size;
192 new_clone->recv_size= source->recv_size;
193 new_clone->poll_timeout= source->poll_timeout;
194 new_clone->connect_timeout= source->connect_timeout;
195 new_clone->retry_timeout= source->retry_timeout;
196 new_clone->distribution= source->distribution;
197
198 hashkit_st *hash_ptr;
199
200 hash_ptr= hashkit_clone(&new_clone->hashkit, &source->hashkit);
201 if (! hash_ptr)
202 {
203 memcached_free(new_clone);
204 return NULL;
205 }
206
207 hash_ptr= hashkit_clone(&new_clone->distribution_hashkit, &source->distribution_hashkit);
208 if (! hash_ptr)
209 {
210 memcached_free(new_clone);
211 return NULL;
212 }
213
214 new_clone->user_data= source->user_data;
215
216 new_clone->snd_timeout= source->snd_timeout;
217 new_clone->rcv_timeout= source->rcv_timeout;
218
219 new_clone->on_clone= source->on_clone;
220 new_clone->on_cleanup= source->on_cleanup;
221
222 new_clone->allocators= source->allocators;
223
224 new_clone->get_key_failure= source->get_key_failure;
225 new_clone->delete_trigger= source->delete_trigger;
226 new_clone->server_failure_limit= source->server_failure_limit;
227 new_clone->io_msg_watermark= source->io_msg_watermark;
228 new_clone->io_bytes_watermark= source->io_bytes_watermark;
229 new_clone->io_key_prefetch= source->io_key_prefetch;
230 new_clone->number_of_replicas= source->number_of_replicas;
231 new_clone->tcp_keepidle= source->tcp_keepidle;
232
233 if (memcached_server_count(source))
234 rc= memcached_push(new_clone, source);
235
236 if (rc != MEMCACHED_SUCCESS)
237 {
238 memcached_free(new_clone);
239
240 return NULL;
241 }
242
243
244 if (source->prefix_key_length)
245 {
246 strcpy(new_clone->prefix_key, source->prefix_key);
247 new_clone->prefix_key_length= source->prefix_key_length;
248 }
249
250 rc= run_distribution(new_clone);
251
252 if (rc != MEMCACHED_SUCCESS)
253 {
254 memcached_free(new_clone);
255
256 return NULL;
257 }
258
259 if (source->on_clone)
260 source->on_clone(new_clone, source);
261
262 return new_clone;
263 }
264
265 void *memcached_get_user_data(const memcached_st *ptr)
266 {
267 return ptr->user_data;
268 }
269
270 void *memcached_set_user_data(memcached_st *ptr, void *data)
271 {
272 void *ret= ptr->user_data;
273 ptr->user_data= data;
274
275 return ret;
276 }
277
278 memcached_return_t memcached_push(memcached_st *destination, const memcached_st *source)
279 {
280 return memcached_server_push(destination, source->servers);
281 }
282
283 inline memcached_server_write_instance_st memcached_server_instance_fetch(memcached_st *ptr, uint32_t server_key)
284 {
285 return &ptr->servers[server_key];
286 }
287
288 inline memcached_server_instance_st memcached_server_instance_by_position(const memcached_st *ptr, uint32_t server_key)
289 {
290 return &ptr->servers[server_key];
291 }