4 #include <sys/socket.h>
5 #include <netinet/tcp.h>
8 This function is used to modify the behabior 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
);
53 case MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
:
54 set_behavior_flag(ptr
, MEM_BUFFER_REQUESTS
, data
);
57 case MEMCACHED_BEHAVIOR_TCP_NODELAY
:
58 set_behavior_flag(ptr
, MEM_TCP_NODELAY
, data
);
61 case MEMCACHED_BEHAVIOR_DISTRIBUTION
:
63 ptr
->distribution
= (memcached_server_distribution
)(data
);
64 if (ptr
->distribution
== MEMCACHED_DISTRIBUTION_RANDOM
)
68 run_distribution(ptr
);
71 case MEMCACHED_BEHAVIOR_KETAMA
:
75 ptr
->hash
= MEMCACHED_HASH_MD5
;
76 ptr
->distribution
= MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA
;
83 run_distribution(ptr
);
86 case MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
:
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
);
94 case MEMCACHED_BEHAVIOR_HASH
:
95 ptr
->hash
= (memcached_hash
)(data
);
97 case MEMCACHED_BEHAVIOR_KETAMA_HASH
:
98 ptr
->hash_continuum
= (memcached_hash
)(data
);
99 run_distribution(ptr
);
101 case MEMCACHED_BEHAVIOR_CACHE_LOOKUPS
:
102 set_behavior_flag(ptr
, MEM_USE_CACHE_LOOKUPS
, data
);
105 case MEMCACHED_BEHAVIOR_VERIFY_KEY
:
106 if (ptr
->flags
& MEM_BINARY_PROTOCOL
)
108 set_behavior_flag(ptr
, MEM_VERIFY_KEY
, data
);
110 case MEMCACHED_BEHAVIOR_SORT_HOSTS
:
112 set_behavior_flag(ptr
, MEM_USE_SORT_HOSTS
, data
);
113 run_distribution(ptr
);
117 case MEMCACHED_BEHAVIOR_POLL_TIMEOUT
:
118 ptr
->poll_timeout
= (int32_t)data
;
120 case MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT
:
121 ptr
->connect_timeout
= (int32_t)data
;
123 case MEMCACHED_BEHAVIOR_RETRY_TIMEOUT
:
124 ptr
->retry_timeout
= (int32_t)data
;
126 case MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
:
127 ptr
->send_size
= (int32_t)data
;
130 case MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
:
131 ptr
->recv_size
= (int32_t)data
;
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
);
139 case MEMCACHED_BEHAVIOR_NOREPLY
:
140 set_behavior_flag(ptr
, MEM_NOREPLY
, data
);
144 return MEMCACHED_SUCCESS
;
147 uint64_t memcached_behavior_get(memcached_st
*ptr
,
148 memcached_behavior flag
)
150 memcached_flags temp_flag
= 0;
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
;
161 case MEMCACHED_BEHAVIOR_SUPPORT_CAS
:
162 temp_flag
= MEM_SUPPORT_CAS
;
164 case MEMCACHED_BEHAVIOR_CACHE_LOOKUPS
:
165 temp_flag
= MEM_USE_CACHE_LOOKUPS
;
167 case MEMCACHED_BEHAVIOR_NO_BLOCK
:
168 temp_flag
= MEM_NO_BLOCK
;
170 case MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
:
171 temp_flag
= MEM_BUFFER_REQUESTS
;
173 case MEMCACHED_BEHAVIOR_TCP_NODELAY
:
174 temp_flag
= MEM_TCP_NODELAY
;
176 case MEMCACHED_BEHAVIOR_VERIFY_KEY
:
177 temp_flag
= MEM_VERIFY_KEY
;
179 case MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
:
180 temp_flag
= MEM_KETAMA_WEIGHTED
;
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
:
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
;
195 case MEMCACHED_BEHAVIOR_POLL_TIMEOUT
:
197 return (uint64_t)ptr
->poll_timeout
;
199 case MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT
:
201 return (uint64_t)ptr
->connect_timeout
;
203 case MEMCACHED_BEHAVIOR_RETRY_TIMEOUT
:
205 return (uint64_t)ptr
->retry_timeout
;
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
:
214 socklen_t sock_length
= sizeof(int);
217 /* We just try the first host, and if it is down we return zero */
218 if ((memcached_connect(&ptr
->hosts
[0])) != MEMCACHED_SUCCESS
)
221 if (getsockopt(ptr
->hosts
[0].fd
, SOL_SOCKET
,
222 SO_SNDBUF
, &sock_size
, &sock_length
))
223 return 0; /* Zero means error */
227 case MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
:
230 socklen_t sock_length
= sizeof(int);
233 /* We just try the first host, and if it is down we return zero */
234 if ((memcached_connect(&ptr
->hosts
[0])) != MEMCACHED_SUCCESS
)
237 if (getsockopt(ptr
->hosts
[0].fd
, SOL_SOCKET
,
238 SO_RCVBUF
, &sock_size
, &sock_length
))
239 return 0; /* Zero means error */
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
;
248 case MEMCACHED_BEHAVIOR_NOREPLY
:
249 temp_flag
= MEM_NOREPLY
;
253 WATCHPOINT_ASSERT(temp_flag
); /* Programming mistake if it gets this far */
254 if (ptr
->flags
& temp_flag
)