1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 * Copyright (C) 2010 Brian Aker All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * * Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following disclaimer
17 * in the documentation and/or other materials provided with the
20 * * The names of its contributors may not be used to endorse or
21 * promote products derived from this software without specific prior
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 #include <libmemcached/common.h>
40 #include <libmemcached/memcached_util.h>
42 #include <libmemcached/error.hpp>
49 struct memcached_pool_st
51 pthread_mutex_t mutex
;
54 memcached_st
**server_pool
;
57 uint32_t current_size
;
59 struct timespec _timeout
;
61 memcached_pool_st(memcached_st
*master_arg
, size_t max_arg
) :
69 pthread_mutex_init(&mutex
, NULL
);
70 pthread_cond_init(&cond
, NULL
);
75 const struct timespec
& timeout() const
80 bool release(memcached_st
*, memcached_return_t
& rc
);
82 memcached_st
*fetch(memcached_return_t
& rc
);
83 memcached_st
*fetch(const struct timespec
&, memcached_return_t
& rc
);
85 bool init(uint32_t initial
);
89 for (int x
= 0; x
<= firstfree
; ++x
)
91 memcached_free(server_pool
[x
]);
92 server_pool
[x
] = NULL
;
95 pthread_mutex_destroy(&mutex
);
96 pthread_cond_destroy(&cond
);
97 delete [] server_pool
;
100 memcached_free(master
);
104 void increment_version()
106 ++master
->configure
.version
;
109 bool compare_version(const memcached_st
*arg
) const
111 return (arg
->configure
.version
== version());
114 int32_t version() const
116 return master
->configure
.version
;
122 * Grow the connection pool by creating a connection structure and clone the
123 * original memcached handle.
125 static bool grow_pool(memcached_pool_st
* pool
)
130 if (not (obj
= memcached_clone(NULL
, pool
->master
)))
135 pool
->server_pool
[++pool
->firstfree
]= obj
;
136 pool
->current_size
++;
137 obj
->configure
.version
= pool
->version();
142 bool memcached_pool_st::init(uint32_t initial
)
144 server_pool
= new (std::nothrow
) memcached_st
*[size
];
149 Try to create the initial size of the pool. An allocation failure at
150 this time is not fatal..
152 for (unsigned int x
= 0; x
< initial
; ++x
)
154 if (grow_pool(this) == false)
164 static inline memcached_pool_st
*_pool_create(memcached_st
* master
, uint32_t initial
, uint32_t max
)
166 if (initial
== 0 or max
== 0 or (initial
> max
))
171 memcached_pool_st
*object
= new (std::nothrow
) memcached_pool_st(master
, max
);
178 Try to create the initial size of the pool. An allocation failure at
179 this time is not fatal..
181 if (not object
->init(initial
))
190 memcached_pool_st
*memcached_pool_create(memcached_st
* master
, uint32_t initial
, uint32_t max
)
192 return _pool_create(master
, initial
, max
);
195 memcached_pool_st
* memcached_pool(const char *option_string
, size_t option_string_length
)
197 memcached_st
*memc
= memcached(option_string
, option_string_length
);
204 memcached_pool_st
*self
= memcached_pool_create(memc
, memc
->configure
.initial_pool_size
, memc
->configure
.max_pool_size
);
207 memcached_free(memc
);
211 self
->_owns_master
= true;
216 memcached_st
* memcached_pool_destroy(memcached_pool_st
* pool
)
223 // Legacy that we return the original structure
224 memcached_st
*ret
= NULL
;
225 if (pool
->_owns_master
)
237 memcached_st
* memcached_pool_st::fetch(memcached_return_t
& rc
)
239 static struct timespec relative_time
= { 0, 0 };
240 return fetch(relative_time
, rc
);
243 memcached_st
* memcached_pool_st::fetch(const struct timespec
& relative_time
, memcached_return_t
& rc
)
245 rc
= MEMCACHED_SUCCESS
;
247 if (pthread_mutex_lock(&mutex
))
249 rc
= MEMCACHED_IN_PROGRESS
;
253 memcached_st
*ret
= NULL
;
258 ret
= server_pool
[firstfree
--];
260 else if (current_size
== size
)
262 if (relative_time
.tv_sec
== 0 and relative_time
.tv_nsec
== 0)
264 pthread_mutex_unlock(&mutex
);
265 rc
= MEMCACHED_NOTFOUND
;
270 struct timespec time_to_wait
= {0, 0};
271 time_to_wait
.tv_sec
= time(NULL
) +relative_time
.tv_sec
;
272 time_to_wait
.tv_nsec
= relative_time
.tv_nsec
;
275 if ((thread_ret
= pthread_cond_timedwait(&cond
, &mutex
, &time_to_wait
)) != 0)
277 pthread_mutex_unlock(&mutex
);
279 if (thread_ret
== ETIMEDOUT
)
281 rc
= MEMCACHED_TIMEOUT
;
292 else if (grow_pool(this) == false)
294 (void)pthread_mutex_unlock(&mutex
);
297 } while (ret
== NULL
);
299 pthread_mutex_unlock(&mutex
);
304 bool memcached_pool_st::release(memcached_st
*released
, memcached_return_t
& rc
)
306 rc
= MEMCACHED_SUCCESS
;
307 if (released
== NULL
)
309 rc
= MEMCACHED_INVALID_ARGUMENTS
;
313 if (pthread_mutex_lock(&mutex
))
315 rc
= MEMCACHED_IN_PROGRESS
;
320 Someone updated the behavior on the object, so we clone a new memcached_st with the new settings. If we fail to clone, we keep the old one around.
322 if (compare_version(released
) == false)
325 if ((memc
= memcached_clone(NULL
, master
)))
327 memcached_free(released
);
332 server_pool
[++firstfree
]= released
;
334 if (firstfree
== 0 and current_size
== size
)
336 /* we might have people waiting for a connection.. wake them up :-) */
337 pthread_cond_broadcast(&cond
);
340 (void)pthread_mutex_unlock(&mutex
);
345 memcached_st
* memcached_pool_fetch(memcached_pool_st
* pool
, struct timespec
* relative_time
, memcached_return_t
* rc
)
352 memcached_return_t unused
;
358 if (relative_time
== NULL
)
360 return pool
->fetch(*rc
);
363 return pool
->fetch(*relative_time
, *rc
);
366 memcached_st
* memcached_pool_pop(memcached_pool_st
* pool
,
368 memcached_return_t
*rc
)
375 memcached_return_t unused
;
384 memc
= pool
->fetch(pool
->timeout(), *rc
);
388 memc
= pool
->fetch(*rc
);
394 memcached_return_t
memcached_pool_release(memcached_pool_st
* pool
, memcached_st
*released
)
398 return MEMCACHED_INVALID_ARGUMENTS
;
401 memcached_return_t rc
;
403 (void) pool
->release(released
, rc
);
408 memcached_return_t
memcached_pool_push(memcached_pool_st
* pool
, memcached_st
*released
)
410 return memcached_pool_release(pool
, released
);
414 memcached_return_t
memcached_pool_behavior_set(memcached_pool_st
*pool
,
415 memcached_behavior_t flag
,
420 return MEMCACHED_INVALID_ARGUMENTS
;
423 if (pthread_mutex_lock(&pool
->mutex
))
425 return MEMCACHED_IN_PROGRESS
;
428 /* update the master */
429 memcached_return_t rc
= memcached_behavior_set(pool
->master
, flag
, data
);
430 if (memcached_failed(rc
))
432 (void)pthread_mutex_unlock(&pool
->mutex
);
436 pool
->increment_version();
437 /* update the clones */
438 for (int xx
= 0; xx
<= pool
->firstfree
; ++xx
)
440 if (memcached_success(memcached_behavior_set(pool
->server_pool
[xx
], flag
, data
)))
442 pool
->server_pool
[xx
]->configure
.version
= pool
->version();
447 if ((memc
= memcached_clone(NULL
, pool
->master
)))
449 memcached_free(pool
->server_pool
[xx
]);
450 pool
->server_pool
[xx
]= memc
;
451 /* I'm not sure what to do in this case.. this would happen
452 if we fail to push the server list inside the client..
453 I should add a testcase for this, but I believe the following
454 would work, except that you would add a hole in the pool list..
455 in theory you could end up with an empty pool....
461 (void)pthread_mutex_unlock(&pool
->mutex
);
466 memcached_return_t
memcached_pool_behavior_get(memcached_pool_st
*pool
,
467 memcached_behavior_t flag
,
472 return MEMCACHED_INVALID_ARGUMENTS
;
475 if (pthread_mutex_lock(&pool
->mutex
))
477 return MEMCACHED_IN_PROGRESS
;
480 *value
= memcached_behavior_get(pool
->master
, flag
);
482 (void)pthread_mutex_unlock(&pool
->mutex
);
484 return MEMCACHED_SUCCESS
;