4 #include <sys/socket.h>
5 #include <netinet/tcp.h>
8 This function is used to modify the behavior of running client.
10 We quit all connections so we can reset the sockets.
13 static void set_behavior_flag(memcached_st
*ptr
, memcached_flags temp_flag
, uint64_t data
)
16 ptr
->flags
|= temp_flag
;
18 ptr
->flags
&= ~temp_flag
;
21 memcached_return
memcached_behavior_set(memcached_st
*ptr
,
22 memcached_behavior flag
,
27 case MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK
:
28 ptr
->io_msg_watermark
= (int32_t)data
;
30 case MEMCACHED_BEHAVIOR_IO_BYTES_WATERMARK
:
31 ptr
->io_bytes_watermark
= (int32_t)data
;
33 case MEMCACHED_BEHAVIOR_SND_TIMEOUT
:
34 ptr
->snd_timeout
= (int32_t)data
;
36 case MEMCACHED_BEHAVIOR_RCV_TIMEOUT
:
37 ptr
->rcv_timeout
= (int32_t)data
;
39 case MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT
:
40 ptr
->server_failure_limit
= (uint32_t)data
;
42 case MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
:
44 set_behavior_flag(ptr
, MEM_VERIFY_KEY
, 0);
45 set_behavior_flag(ptr
, MEM_BINARY_PROTOCOL
, data
);
47 case MEMCACHED_BEHAVIOR_SUPPORT_CAS
:
48 set_behavior_flag(ptr
, MEM_SUPPORT_CAS
, data
);
50 case MEMCACHED_BEHAVIOR_NO_BLOCK
:
51 set_behavior_flag(ptr
, MEM_NO_BLOCK
, data
);
54 case MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
:
55 set_behavior_flag(ptr
, MEM_BUFFER_REQUESTS
, data
);
58 case MEMCACHED_BEHAVIOR_USE_UDP
:
59 if (ptr
->number_of_hosts
)
60 return MEMCACHED_FAILURE
;
61 set_behavior_flag(ptr
, MEM_USE_UDP
, data
);
63 set_behavior_flag(ptr
,MEM_NOREPLY
,data
);
65 case MEMCACHED_BEHAVIOR_TCP_NODELAY
:
66 set_behavior_flag(ptr
, MEM_TCP_NODELAY
, data
);
69 case MEMCACHED_BEHAVIOR_DISTRIBUTION
:
71 ptr
->distribution
= (memcached_server_distribution
)(data
);
72 if (ptr
->distribution
== MEMCACHED_DISTRIBUTION_RANDOM
)
76 run_distribution(ptr
);
79 case MEMCACHED_BEHAVIOR_KETAMA
:
83 ptr
->hash
= MEMCACHED_HASH_MD5
;
84 ptr
->distribution
= MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA
;
91 run_distribution(ptr
);
94 case MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
:
96 ptr
->hash
= MEMCACHED_HASH_MD5
;
97 ptr
->distribution
= MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA
;
98 set_behavior_flag(ptr
, MEM_KETAMA_WEIGHTED
, data
);
99 run_distribution(ptr
);
102 case MEMCACHED_BEHAVIOR_HASH
:
103 #ifndef HAVE_HSIEH_HASH
104 if ((memcached_hash
)(data
) == MEMCACHED_HASH_HSIEH
)
105 return MEMCACHED_FAILURE
;
107 ptr
->hash
= (memcached_hash
)(data
);
109 case MEMCACHED_BEHAVIOR_KETAMA_HASH
:
110 ptr
->hash_continuum
= (memcached_hash
)(data
);
111 run_distribution(ptr
);
113 case MEMCACHED_BEHAVIOR_CACHE_LOOKUPS
:
114 set_behavior_flag(ptr
, MEM_USE_CACHE_LOOKUPS
, data
);
117 case MEMCACHED_BEHAVIOR_VERIFY_KEY
:
118 if (ptr
->flags
& MEM_BINARY_PROTOCOL
)
120 set_behavior_flag(ptr
, MEM_VERIFY_KEY
, data
);
122 case MEMCACHED_BEHAVIOR_SORT_HOSTS
:
124 set_behavior_flag(ptr
, MEM_USE_SORT_HOSTS
, data
);
125 run_distribution(ptr
);
129 case MEMCACHED_BEHAVIOR_POLL_TIMEOUT
:
130 ptr
->poll_timeout
= (int32_t)data
;
132 case MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT
:
133 ptr
->connect_timeout
= (int32_t)data
;
135 case MEMCACHED_BEHAVIOR_RETRY_TIMEOUT
:
136 ptr
->retry_timeout
= (int32_t)data
;
138 case MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
:
139 ptr
->send_size
= (int32_t)data
;
142 case MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
:
143 ptr
->recv_size
= (int32_t)data
;
146 case MEMCACHED_BEHAVIOR_USER_DATA
:
147 return MEMCACHED_FAILURE
;
148 case MEMCACHED_BEHAVIOR_HASH_WITH_PREFIX_KEY
:
149 set_behavior_flag(ptr
, MEM_HASH_WITH_PREFIX_KEY
, data
);
151 case MEMCACHED_BEHAVIOR_NOREPLY
:
152 set_behavior_flag(ptr
, MEM_NOREPLY
, data
);
154 case MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS
:
155 set_behavior_flag(ptr
, MEM_AUTO_EJECT_HOSTS
, data
);
159 return MEMCACHED_SUCCESS
;
162 uint64_t memcached_behavior_get(memcached_st
*ptr
,
163 memcached_behavior flag
)
165 memcached_flags temp_flag
= 0;
169 case MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK
:
170 return ptr
->io_msg_watermark
;
171 case MEMCACHED_BEHAVIOR_IO_BYTES_WATERMARK
:
172 return ptr
->io_bytes_watermark
;
173 case MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
:
174 temp_flag
= MEM_BINARY_PROTOCOL
;
176 case MEMCACHED_BEHAVIOR_SUPPORT_CAS
:
177 temp_flag
= MEM_SUPPORT_CAS
;
179 case MEMCACHED_BEHAVIOR_CACHE_LOOKUPS
:
180 temp_flag
= MEM_USE_CACHE_LOOKUPS
;
182 case MEMCACHED_BEHAVIOR_NO_BLOCK
:
183 temp_flag
= MEM_NO_BLOCK
;
185 case MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
:
186 temp_flag
= MEM_BUFFER_REQUESTS
;
188 case MEMCACHED_BEHAVIOR_USE_UDP
:
189 temp_flag
= MEM_USE_UDP
;
191 case MEMCACHED_BEHAVIOR_TCP_NODELAY
:
192 temp_flag
= MEM_TCP_NODELAY
;
194 case MEMCACHED_BEHAVIOR_VERIFY_KEY
:
195 temp_flag
= MEM_VERIFY_KEY
;
197 case MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
:
198 temp_flag
= MEM_KETAMA_WEIGHTED
;
200 case MEMCACHED_BEHAVIOR_DISTRIBUTION
:
201 return ptr
->distribution
;
202 case MEMCACHED_BEHAVIOR_KETAMA
:
203 return (ptr
->distribution
== MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA
) ? 1 : 0;
204 case MEMCACHED_BEHAVIOR_HASH
:
206 case MEMCACHED_BEHAVIOR_KETAMA_HASH
:
207 return ptr
->hash_continuum
;
208 case MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT
:
209 return ptr
->server_failure_limit
;
210 case MEMCACHED_BEHAVIOR_SORT_HOSTS
:
211 temp_flag
= MEM_USE_SORT_HOSTS
;
213 case MEMCACHED_BEHAVIOR_POLL_TIMEOUT
:
215 return (uint64_t)ptr
->poll_timeout
;
217 case MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT
:
219 return (uint64_t)ptr
->connect_timeout
;
221 case MEMCACHED_BEHAVIOR_RETRY_TIMEOUT
:
223 return (uint64_t)ptr
->retry_timeout
;
225 case MEMCACHED_BEHAVIOR_SND_TIMEOUT
:
226 return (uint64_t)ptr
->snd_timeout
;
227 case MEMCACHED_BEHAVIOR_RCV_TIMEOUT
:
228 return (uint64_t)ptr
->rcv_timeout
;
229 case MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
:
232 socklen_t sock_length
= sizeof(int);
235 /* We just try the first host, and if it is down we return zero */
236 if ((memcached_connect(&ptr
->hosts
[0])) != MEMCACHED_SUCCESS
)
239 if (getsockopt(ptr
->hosts
[0].fd
, SOL_SOCKET
,
240 SO_SNDBUF
, &sock_size
, &sock_length
))
241 return 0; /* Zero means error */
245 case MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
:
248 socklen_t sock_length
= sizeof(int);
251 /* We just try the first host, and if it is down we return zero */
252 if ((memcached_connect(&ptr
->hosts
[0])) != MEMCACHED_SUCCESS
)
255 if (getsockopt(ptr
->hosts
[0].fd
, SOL_SOCKET
,
256 SO_RCVBUF
, &sock_size
, &sock_length
))
257 return 0; /* Zero means error */
261 case MEMCACHED_BEHAVIOR_USER_DATA
:
262 return MEMCACHED_FAILURE
;
263 case MEMCACHED_BEHAVIOR_HASH_WITH_PREFIX_KEY
:
264 temp_flag
= MEM_HASH_WITH_PREFIX_KEY
;
266 case MEMCACHED_BEHAVIOR_NOREPLY
:
267 temp_flag
= MEM_NOREPLY
;
269 case MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS
:
270 temp_flag
= MEM_AUTO_EJECT_HOSTS
;
274 WATCHPOINT_ASSERT(temp_flag
); /* Programming mistake if it gets this far */
275 if (ptr
->flags
& temp_flag
)