Menu

[r1522]: / cppdb / trunk / pool.cpp  Maximize  Restore  History

Download this file

154 lines (134 with data), 2.7 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include "pool.h"
#include "backend.h"
#include "utils.h"
#include "driver_manager.h"
#include <stdlib.h>
namespace cppdb {
struct pool::data {};
pool::pool(std::string const &cs) :
limit_(0),
life_time_(0),
size_(0)
{
connection_info inf(cs);
init(cs);
}
pool::pool(connection_info const &cs) :
limit_(0),
life_time_(0),
size_(0)
{
init(cs);
}
void pool::init(connection_info const &ci)
{
ci_ = ci;
size_ = 0;
limit_ = atoi(ci_.get("@pool","16").c_str());
life_time_ = atoi(ci_.get("@pool_max_idle","600").c_str());
}
pool::~pool()
{
}
ref_ptr<backend::connection> pool::open()
{
if(limit_ == 0)
return driver_manager::instance().connect(ci_);
ref_ptr<backend::connection> p = get();
if(!p) {
p=driver_manager::instance().connect(ci_);
}
p->recycle_pool(this);
return p;
}
// this is thread safe member function
ref_ptr<backend::connection> pool::get()
{
if(limit_ == 0)
return 0;
ref_ptr<backend::connection> c;
pool_type garbage;
time_t now = time(0);
{
mutex::guard l(lock_);
// Nothing there should throw so it is safe
pool_type::iterator p = pool_.begin(),tmp;
while(p!=pool_.end()) {
if(p->last_used + life_time_ < now) {
tmp=p;
p++;
garbage.splice(garbage.begin(),pool_,tmp);
size_ --;
}
else {
// all is sorted by time
break;
}
}
if(!pool_.empty()) {
c = pool_.back().conn;
pool_.pop_back();
size_ --;
}
}
return c;
}
// this is thread safe member function
void pool::put(backend::connection *c_in)
{
std::auto_ptr<backend::connection> c(c_in);
if(limit_ == 0)
return;
pool_type garbage;
time_t now = time(0);
{
mutex::guard l(lock_);
// under lock do all very fast
if(c.get()) {
pool_.push_back(entry());
pool_.back().last_used = now;
pool_.back().conn = c.release();
size_ ++;
}
// Nothing there should throw so it is safe
pool_type::iterator p = pool_.begin(),tmp;
while(p!=pool_.end()) {
if(p->last_used + life_time_ < now) {
tmp=p;
p++;
garbage.splice(garbage.begin(),pool_,tmp);
size_ --;
}
else {
// all is sorted by time
break;
}
}
// can be at most 1 entry bigger then limit
if(size_ > limit_) {
garbage.splice(garbage.begin(),pool_,pool_.begin());
size_--;
}
}
}
// this is thread safe member function
void pool::gc()
{
put(0);
}
namespace backend {
void connection::dispose(connection *c)
{
if(!c)
return;
pool *p = c->pool_;
c->pool_ = 0;
if(p) {
p->put(c);
}
else
delete c;
}
}
// mutex protected end
}
MongoDB Logo MongoDB