f23415507ced1525fa99f3bea346e88ae2db258e
[awesomized/libmemcached] / libmemcached / memcached_behavior.c
1 #include "common.h"
2 #include <time.h>
3 #include <sys/types.h>
4 #include <sys/socket.h>
5 #include <netinet/tcp.h>
6
7 /*
8 This function is used to modify the behabior of running client.
9
10 We quit all connections so we can reset the sockets.
11 */
12
13 static void set_behavior_flag(memcached_st *ptr, memcached_flags temp_flag, uint64_t data)
14 {
15 if (data)
16 ptr->flags|= temp_flag;
17 else
18 ptr->flags&= ~temp_flag;
19 }
20
21 memcached_return memcached_behavior_set(memcached_st *ptr,
22 memcached_behavior flag,
23 uint64_t data)
24 {
25 switch (flag)
26 {
27 case MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK:
28 ptr->io_msg_watermark= (int32_t)data;
29 break;
30 case MEMCACHED_BEHAVIOR_IO_BYTES_WATERMARK:
31 ptr->io_bytes_watermark= (int32_t)data;
32 break;
33 case MEMCACHED_BEHAVIOR_SND_TIMEOUT:
34 ptr->snd_timeout= (int32_t)data;
35 break;
36 case MEMCACHED_BEHAVIOR_RCV_TIMEOUT:
37 ptr->rcv_timeout= (int32_t)data;
38 break;
39 case MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT:
40 ptr->server_failure_limit= (uint32_t)data;
41 break;
42 case MEMCACHED_BEHAVIOR_BINARY_PROTOCOL:
43 if (data)
44 set_behavior_flag(ptr, MEM_VERIFY_KEY, 0);
45 set_behavior_flag(ptr, MEM_BINARY_PROTOCOL, data);
46 break;
47 case MEMCACHED_BEHAVIOR_SUPPORT_CAS:
48 set_behavior_flag(ptr, MEM_SUPPORT_CAS, data);
49 break;
50 case MEMCACHED_BEHAVIOR_NO_BLOCK:
51 set_behavior_flag(ptr, MEM_NO_BLOCK, data);
52 memcached_quit(ptr);
53 case MEMCACHED_BEHAVIOR_BUFFER_REQUESTS:
54 set_behavior_flag(ptr, MEM_BUFFER_REQUESTS, data);
55 memcached_quit(ptr);
56 break;
57 case MEMCACHED_BEHAVIOR_TCP_NODELAY:
58 set_behavior_flag(ptr, MEM_TCP_NODELAY, data);
59 memcached_quit(ptr);
60 break;
61 case MEMCACHED_BEHAVIOR_DISTRIBUTION:
62 {
63 ptr->distribution= (memcached_server_distribution)(data);
64 if (ptr->distribution == MEMCACHED_DISTRIBUTION_RANDOM)
65 {
66 srandom(time(NULL));
67 }
68 run_distribution(ptr);
69 break;
70 }
71 case MEMCACHED_BEHAVIOR_KETAMA:
72 {
73 if (data)
74 {
75 ptr->hash= MEMCACHED_HASH_MD5;
76 ptr->distribution= MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA;
77 }
78 else
79 {
80 ptr->hash= 0;
81 ptr->distribution= 0;
82 }
83 run_distribution(ptr);
84 break;
85 }
86 case MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED:
87 {
88 ptr->hash= MEMCACHED_HASH_MD5;
89 ptr->distribution= MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA;
90 set_behavior_flag(ptr, MEM_KETAMA_WEIGHTED, data);
91 run_distribution(ptr);
92 break;
93 }
94 case MEMCACHED_BEHAVIOR_HASH:
95 ptr->hash= (memcached_hash)(data);
96 break;
97 case MEMCACHED_BEHAVIOR_KETAMA_HASH:
98 ptr->hash_continuum= (memcached_hash)(data);
99 run_distribution(ptr);
100 break;
101 case MEMCACHED_BEHAVIOR_CACHE_LOOKUPS:
102 set_behavior_flag(ptr, MEM_USE_CACHE_LOOKUPS, data);
103 memcached_quit(ptr);
104 break;
105 case MEMCACHED_BEHAVIOR_VERIFY_KEY:
106 if (ptr->flags & MEM_BINARY_PROTOCOL)
107 break;
108 set_behavior_flag(ptr, MEM_VERIFY_KEY, data);
109 break;
110 case MEMCACHED_BEHAVIOR_SORT_HOSTS:
111 {
112 set_behavior_flag(ptr, MEM_USE_SORT_HOSTS, data);
113 run_distribution(ptr);
114
115 break;
116 }
117 case MEMCACHED_BEHAVIOR_POLL_TIMEOUT:
118 ptr->poll_timeout= (int32_t)data;
119 break;
120 case MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT:
121 ptr->connect_timeout= (int32_t)data;
122 break;
123 case MEMCACHED_BEHAVIOR_RETRY_TIMEOUT:
124 ptr->retry_timeout= (int32_t)data;
125 break;
126 case MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE:
127 ptr->send_size= (int32_t)data;
128 memcached_quit(ptr);
129 break;
130 case MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE:
131 ptr->recv_size= (int32_t)data;
132 memcached_quit(ptr);
133 break;
134 case MEMCACHED_BEHAVIOR_USER_DATA:
135 return MEMCACHED_FAILURE;
136 case MEMCACHED_BEHAVIOR_HASH_WITH_PREFIX_KEY:
137 set_behavior_flag(ptr, MEM_HASH_WITH_PREFIX_KEY, data);
138 break;
139 case MEMCACHED_BEHAVIOR_NOREPLY:
140 set_behavior_flag(ptr, MEM_NOREPLY, data);
141 break;
142 }
143
144 return MEMCACHED_SUCCESS;
145 }
146
147 uint64_t memcached_behavior_get(memcached_st *ptr,
148 memcached_behavior flag)
149 {
150 memcached_flags temp_flag= 0;
151
152 switch (flag)
153 {
154 case MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK:
155 return ptr->io_msg_watermark;
156 case MEMCACHED_BEHAVIOR_IO_BYTES_WATERMARK:
157 return ptr->io_bytes_watermark;
158 case MEMCACHED_BEHAVIOR_BINARY_PROTOCOL:
159 temp_flag= MEM_BINARY_PROTOCOL;
160 break;
161 case MEMCACHED_BEHAVIOR_SUPPORT_CAS:
162 temp_flag= MEM_SUPPORT_CAS;
163 break;
164 case MEMCACHED_BEHAVIOR_CACHE_LOOKUPS:
165 temp_flag= MEM_USE_CACHE_LOOKUPS;
166 break;
167 case MEMCACHED_BEHAVIOR_NO_BLOCK:
168 temp_flag= MEM_NO_BLOCK;
169 break;
170 case MEMCACHED_BEHAVIOR_BUFFER_REQUESTS:
171 temp_flag= MEM_BUFFER_REQUESTS;
172 break;
173 case MEMCACHED_BEHAVIOR_TCP_NODELAY:
174 temp_flag= MEM_TCP_NODELAY;
175 break;
176 case MEMCACHED_BEHAVIOR_VERIFY_KEY:
177 temp_flag= MEM_VERIFY_KEY;
178 break;
179 case MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED:
180 temp_flag= MEM_KETAMA_WEIGHTED;
181 break;
182 case MEMCACHED_BEHAVIOR_DISTRIBUTION:
183 return ptr->distribution;
184 case MEMCACHED_BEHAVIOR_KETAMA:
185 return (ptr->distribution == MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA) ? 1 : 0;
186 case MEMCACHED_BEHAVIOR_HASH:
187 return ptr->hash;
188 case MEMCACHED_BEHAVIOR_KETAMA_HASH:
189 return ptr->hash_continuum;
190 case MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT:
191 return ptr->server_failure_limit;
192 case MEMCACHED_BEHAVIOR_SORT_HOSTS:
193 temp_flag= MEM_USE_SORT_HOSTS;
194 break;
195 case MEMCACHED_BEHAVIOR_POLL_TIMEOUT:
196 {
197 return (uint64_t)ptr->poll_timeout;
198 }
199 case MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT:
200 {
201 return (uint64_t)ptr->connect_timeout;
202 }
203 case MEMCACHED_BEHAVIOR_RETRY_TIMEOUT:
204 {
205 return (uint64_t)ptr->retry_timeout;
206 }
207 case MEMCACHED_BEHAVIOR_SND_TIMEOUT:
208 return (uint64_t)ptr->snd_timeout;
209 case MEMCACHED_BEHAVIOR_RCV_TIMEOUT:
210 return (uint64_t)ptr->rcv_timeout;
211 case MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE:
212 {
213 int sock_size;
214 socklen_t sock_length= sizeof(int);
215
216 /* REFACTOR */
217 /* We just try the first host, and if it is down we return zero */
218 if ((memcached_connect(&ptr->hosts[0])) != MEMCACHED_SUCCESS)
219 return 0;
220
221 if (getsockopt(ptr->hosts[0].fd, SOL_SOCKET,
222 SO_SNDBUF, &sock_size, &sock_length))
223 return 0; /* Zero means error */
224
225 return sock_size;
226 }
227 case MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE:
228 {
229 int sock_size;
230 socklen_t sock_length= sizeof(int);
231
232 /* REFACTOR */
233 /* We just try the first host, and if it is down we return zero */
234 if ((memcached_connect(&ptr->hosts[0])) != MEMCACHED_SUCCESS)
235 return 0;
236
237 if (getsockopt(ptr->hosts[0].fd, SOL_SOCKET,
238 SO_RCVBUF, &sock_size, &sock_length))
239 return 0; /* Zero means error */
240
241 return sock_size;
242 }
243 case MEMCACHED_BEHAVIOR_USER_DATA:
244 return MEMCACHED_FAILURE;
245 case MEMCACHED_BEHAVIOR_HASH_WITH_PREFIX_KEY:
246 temp_flag= MEM_HASH_WITH_PREFIX_KEY;
247 break;
248 case MEMCACHED_BEHAVIOR_NOREPLY:
249 temp_flag= MEM_NOREPLY;
250 break;
251 }
252
253 WATCHPOINT_ASSERT(temp_flag); /* Programming mistake if it gets this far */
254 if (ptr->flags & temp_flag)
255 return 1;
256 else
257 return 0;
258 }