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 <libmemcachedutil/common.h>
46 struct memcached_pool_st
48 pthread_mutex_t mutex
;
51 memcached_st
**server_pool
;
54 uint32_t current_size
;
56 struct timespec _timeout
;
58 memcached_pool_st(memcached_st
*master_arg
, size_t max_arg
) :
66 pthread_mutex_init(&mutex
, NULL
);
67 pthread_cond_init(&cond
, NULL
);
72 const struct timespec
& timeout() const
77 bool release(memcached_st
*, memcached_return_t
& rc
);
79 memcached_st
*fetch(memcached_return_t
& rc
);
80 memcached_st
*fetch(const struct timespec
&, memcached_return_t
& rc
);
82 bool init(uint32_t initial
);
86 for (int x
= 0; x
<= firstfree
; ++x
)
88 memcached_free(server_pool
[x
]);
89 server_pool
[x
] = NULL
;
92 pthread_mutex_destroy(&mutex
);
93 pthread_cond_destroy(&cond
);
94 delete [] server_pool
;
97 memcached_free(master
);
101 void increment_version()
103 ++master
->configure
.version
;
106 bool compare_version(const memcached_st
*arg
) const
108 return (arg
->configure
.version
== version());
111 int32_t version() const
113 return master
->configure
.version
;
119 * Grow the connection pool by creating a connection structure and clone the
120 * original memcached handle.
122 static bool grow_pool(memcached_pool_st
* pool
)
127 if (not (obj
= memcached_clone(NULL
, pool
->master
)))
132 pool
->server_pool
[++pool
->firstfree
]= obj
;
133 pool
->current_size
++;
134 obj
->configure
.version
= pool
->version();
139 bool memcached_pool_st::init(uint32_t initial
)
141 server_pool
= new (std::nothrow
) memcached_st
*[size
];
146 Try to create the initial size of the pool. An allocation failure at
147 this time is not fatal..
149 for (unsigned int x
= 0; x
< initial
; ++x
)
151 if (grow_pool(this) == false)
161 static inline memcached_pool_st
*_pool_create(memcached_st
* master
, uint32_t initial
, uint32_t max
)
163 if (initial
== 0 or max
== 0 or (initial
> max
))
168 memcached_pool_st
*object
= new (std::nothrow
) memcached_pool_st(master
, max
);
175 Try to create the initial size of the pool. An allocation failure at
176 this time is not fatal..
178 if (not object
->init(initial
))
187 memcached_pool_st
*memcached_pool_create(memcached_st
* master
, uint32_t initial
, uint32_t max
)
189 return _pool_create(master
, initial
, max
);
192 memcached_pool_st
* memcached_pool(const char *option_string
, size_t option_string_length
)
194 memcached_st
*memc
= memcached(option_string
, option_string_length
);
201 memcached_pool_st
*self
= memcached_pool_create(memc
, memc
->configure
.initial_pool_size
, memc
->configure
.max_pool_size
);
204 memcached_free(memc
);
208 self
->_owns_master
= true;
213 memcached_st
* memcached_pool_destroy(memcached_pool_st
* pool
)
220 // Legacy that we return the original structure
221 memcached_st
*ret
= NULL
;
222 if (pool
->_owns_master
)
234 memcached_st
* memcached_pool_st::fetch(memcached_return_t
& rc
)
236 static struct timespec relative_time
= { 0, 0 };
237 return fetch(relative_time
, rc
);
240 memcached_st
* memcached_pool_st::fetch(const struct timespec
& relative_time
, memcached_return_t
& rc
)
242 rc
= MEMCACHED_SUCCESS
;
244 if (pthread_mutex_lock(&mutex
))
246 rc
= MEMCACHED_IN_PROGRESS
;
250 memcached_st
*ret
= NULL
;
255 ret
= server_pool
[firstfree
--];
257 else if (current_size
== size
)
259 if (relative_time
.tv_sec
== 0 and relative_time
.tv_nsec
== 0)
261 pthread_mutex_unlock(&mutex
);
262 rc
= MEMCACHED_NOTFOUND
;
267 struct timespec time_to_wait
= {0, 0};
268 time_to_wait
.tv_sec
= time(NULL
) +relative_time
.tv_sec
;
269 time_to_wait
.tv_nsec
= relative_time
.tv_nsec
;
272 if ((thread_ret
= pthread_cond_timedwait(&cond
, &mutex
, &time_to_wait
)) != 0)
274 pthread_mutex_unlock(&mutex
);
276 if (thread_ret
== ETIMEDOUT
)
278 rc
= MEMCACHED_TIMEOUT
;
289 else if (grow_pool(this) == false)
291 (void)pthread_mutex_unlock(&mutex
);
294 } while (ret
== NULL
);
296 pthread_mutex_unlock(&mutex
);
301 bool memcached_pool_st::release(memcached_st
*released
, memcached_return_t
& rc
)
303 rc
= MEMCACHED_SUCCESS
;
304 if (released
== NULL
)
306 rc
= MEMCACHED_INVALID_ARGUMENTS
;
310 if (pthread_mutex_lock(&mutex
))
312 rc
= MEMCACHED_IN_PROGRESS
;
317 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.
319 if (compare_version(released
) == false)
322 if ((memc
= memcached_clone(NULL
, master
)))
324 memcached_free(released
);
329 server_pool
[++firstfree
]= released
;
331 if (firstfree
== 0 and current_size
== size
)
333 /* we might have people waiting for a connection.. wake them up :-) */
334 pthread_cond_broadcast(&cond
);
337 (void)pthread_mutex_unlock(&mutex
);
342 memcached_st
* memcached_pool_fetch(memcached_pool_st
* pool
, struct timespec
* relative_time
, memcached_return_t
* rc
)
349 memcached_return_t unused
;
355 if (relative_time
== NULL
)
357 return pool
->fetch(*rc
);
360 return pool
->fetch(*relative_time
, *rc
);
363 memcached_st
* memcached_pool_pop(memcached_pool_st
* pool
,
365 memcached_return_t
*rc
)
372 memcached_return_t unused
;
381 memc
= pool
->fetch(pool
->timeout(), *rc
);
385 memc
= pool
->fetch(*rc
);
391 memcached_return_t
memcached_pool_release(memcached_pool_st
* pool
, memcached_st
*released
)
395 return MEMCACHED_INVALID_ARGUMENTS
;
398 memcached_return_t rc
;
400 (void) pool
->release(released
, rc
);
405 memcached_return_t
memcached_pool_push(memcached_pool_st
* pool
, memcached_st
*released
)
407 return memcached_pool_release(pool
, released
);
411 memcached_return_t
memcached_pool_behavior_set(memcached_pool_st
*pool
,
412 memcached_behavior_t flag
,
417 return MEMCACHED_INVALID_ARGUMENTS
;
420 if (pthread_mutex_lock(&pool
->mutex
))
422 return MEMCACHED_IN_PROGRESS
;
425 /* update the master */
426 memcached_return_t rc
= memcached_behavior_set(pool
->master
, flag
, data
);
427 if (memcached_failed(rc
))
429 (void)pthread_mutex_unlock(&pool
->mutex
);
433 pool
->increment_version();
434 /* update the clones */
435 for (int xx
= 0; xx
<= pool
->firstfree
; ++xx
)
437 if (memcached_success(memcached_behavior_set(pool
->server_pool
[xx
], flag
, data
)))
439 pool
->server_pool
[xx
]->configure
.version
= pool
->version();
444 if ((memc
= memcached_clone(NULL
, pool
->master
)))
446 memcached_free(pool
->server_pool
[xx
]);
447 pool
->server_pool
[xx
]= memc
;
448 /* I'm not sure what to do in this case.. this would happen
449 if we fail to push the server list inside the client..
450 I should add a testcase for this, but I believe the following
451 would work, except that you would add a hole in the pool list..
452 in theory you could end up with an empty pool....
458 (void)pthread_mutex_unlock(&pool
->mutex
);
463 memcached_return_t
memcached_pool_behavior_get(memcached_pool_st
*pool
,
464 memcached_behavior_t flag
,
469 return MEMCACHED_INVALID_ARGUMENTS
;
472 if (pthread_mutex_lock(&pool
->mutex
))
474 return MEMCACHED_IN_PROGRESS
;
477 *value
= memcached_behavior_get(pool
->master
, flag
);
479 (void)pthread_mutex_unlock(&pool
->mutex
);
481 return MEMCACHED_SUCCESS
;