Added option to enable TCP_KEEPALIVE on created sockets.
[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->io_key_prefetch= 0;
67 self->cached_errno= 0;
68 self->poll_timeout= MEMCACHED_DEFAULT_TIMEOUT;
69 self->connect_timeout= MEMCACHED_DEFAULT_TIMEOUT;
70 self->retry_timeout= 0;
71 self->continuum_count= 0;
72
73 self->send_size= -1;
74 self->recv_size= -1;
75
76 self->user_data= NULL;
77 self->next_distribution_rebuild= 0;
78 self->prefix_key_length= 0;
79 self->number_of_replicas= 0;
80 hash_ptr= hashkit_create(&self->distribution_hashkit);
81 if (! hash_ptr)
82 return false;
83 self->continuum= NULL;
84
85 self->allocators= memcached_allocators_return_default();
86
87 self->on_clone= NULL;
88 self->on_cleanup= NULL;
89 self->get_key_failure= NULL;
90 self->delete_trigger= NULL;
91 self->callbacks= NULL;
92
93 return true;
94 }
95
96 memcached_st *memcached_create(memcached_st *ptr)
97 {
98 if (ptr == NULL)
99 {
100 ptr= (memcached_st *)malloc(sizeof(memcached_st));
101
102 if (! ptr)
103 {
104 return NULL; /* MEMCACHED_MEMORY_ALLOCATION_FAILURE */
105 }
106
107 ptr->options.is_allocated= true;
108 }
109 else
110 {
111 ptr->options.is_allocated= false;
112 }
113
114 #if 0
115 memcached_set_purging(ptr, false);
116 memcached_set_processing_input(ptr, false);
117 #endif
118
119 if (! _memcached_init(ptr))
120 {
121 memcached_free(ptr);
122 return NULL;
123 }
124
125 if (! memcached_result_create(ptr, &ptr->result))
126 {
127 memcached_free(ptr);
128 return NULL;
129 }
130
131 WATCHPOINT_ASSERT_INITIALIZED(&ptr->result);
132
133 return ptr;
134 }
135
136 void memcached_servers_reset(memcached_st *ptr)
137 {
138 memcached_server_list_free(memcached_server_list(ptr));
139
140 memcached_server_list_set(ptr, NULL);
141 ptr->number_of_hosts= 0;
142 ptr->last_disconnected_server= NULL;
143 ptr->server_failure_limit= 0;
144 }
145
146 void memcached_free(memcached_st *ptr)
147 {
148 /* If we have anything open, lets close it now */
149 memcached_quit(ptr);
150 memcached_server_list_free(memcached_server_list(ptr));
151 memcached_result_free(&ptr->result);
152
153 if (ptr->on_cleanup)
154 ptr->on_cleanup(ptr);
155
156 if (ptr->continuum)
157 libmemcached_free(ptr, ptr->continuum);
158
159 if (memcached_is_allocated(ptr))
160 {
161 libmemcached_free(ptr, ptr);
162 }
163 }
164
165 /*
166 clone is the destination, while source is the structure to clone.
167 If source is NULL the call is the same as if a memcached_create() was
168 called.
169 */
170 memcached_st *memcached_clone(memcached_st *clone, const memcached_st *source)
171 {
172 memcached_return_t rc= MEMCACHED_SUCCESS;
173 memcached_st *new_clone;
174
175 if (source == NULL)
176 return memcached_create(clone);
177
178 if (clone && memcached_is_allocated(clone))
179 {
180 return NULL;
181 }
182
183 new_clone= memcached_create(clone);
184
185 if (new_clone == NULL)
186 return NULL;
187
188 new_clone->flags= source->flags;
189 new_clone->send_size= source->send_size;
190 new_clone->recv_size= source->recv_size;
191 new_clone->poll_timeout= source->poll_timeout;
192 new_clone->connect_timeout= source->connect_timeout;
193 new_clone->retry_timeout= source->retry_timeout;
194 new_clone->distribution= source->distribution;
195
196 hashkit_st *hash_ptr;
197
198 hash_ptr= hashkit_clone(&new_clone->hashkit, &source->hashkit);
199 if (! hash_ptr)
200 {
201 memcached_free(new_clone);
202 return NULL;
203 }
204
205 hash_ptr= hashkit_clone(&new_clone->distribution_hashkit, &source->distribution_hashkit);
206 if (! hash_ptr)
207 {
208 memcached_free(new_clone);
209 return NULL;
210 }
211
212 new_clone->user_data= source->user_data;
213
214 new_clone->snd_timeout= source->snd_timeout;
215 new_clone->rcv_timeout= source->rcv_timeout;
216
217 new_clone->on_clone= source->on_clone;
218 new_clone->on_cleanup= source->on_cleanup;
219
220 new_clone->allocators= source->allocators;
221
222 new_clone->get_key_failure= source->get_key_failure;
223 new_clone->delete_trigger= source->delete_trigger;
224 new_clone->server_failure_limit= source->server_failure_limit;
225 new_clone->io_msg_watermark= source->io_msg_watermark;
226 new_clone->io_bytes_watermark= source->io_bytes_watermark;
227 new_clone->io_key_prefetch= source->io_key_prefetch;
228 new_clone->number_of_replicas= source->number_of_replicas;
229
230 if (memcached_server_count(source))
231 rc= memcached_push(new_clone, source);
232
233 if (rc != MEMCACHED_SUCCESS)
234 {
235 memcached_free(new_clone);
236
237 return NULL;
238 }
239
240
241 if (source->prefix_key_length)
242 {
243 strcpy(new_clone->prefix_key, source->prefix_key);
244 new_clone->prefix_key_length= source->prefix_key_length;
245 }
246
247 rc= run_distribution(new_clone);
248
249 if (rc != MEMCACHED_SUCCESS)
250 {
251 memcached_free(new_clone);
252
253 return NULL;
254 }
255
256 if (source->on_clone)
257 source->on_clone(new_clone, source);
258
259 return new_clone;
260 }
261
262 void *memcached_get_user_data(const memcached_st *ptr)
263 {
264 return ptr->user_data;
265 }
266
267 void *memcached_set_user_data(memcached_st *ptr, void *data)
268 {
269 void *ret= ptr->user_data;
270 ptr->user_data= data;
271
272 return ret;
273 }
274
275 memcached_return_t memcached_push(memcached_st *destination, const memcached_st *source)
276 {
277 return memcached_server_push(destination, source->servers);
278 }
279
280 inline memcached_server_write_instance_st memcached_server_instance_fetch(memcached_st *ptr, uint32_t server_key)
281 {
282 return &ptr->servers[server_key];
283 }
284
285 inline memcached_server_instance_st memcached_server_instance_by_position(const memcached_st *ptr, uint32_t server_key)
286 {
287 return &ptr->servers[server_key];
288 }