fixed a bug when acquiring a connection with a specific id
This commit is contained in:
@@ -19,7 +19,7 @@ template < class Connection >
|
||||
class connection_ptr
|
||||
{
|
||||
public:
|
||||
connection_ptr(IdConnection<Connection> *c, connection_pool<Connection> &pool)
|
||||
connection_ptr(IdConnection<Connection> *c, connection_pool<Connection> *pool)
|
||||
: connection_(c), pool_(pool) {}
|
||||
~connection_ptr();
|
||||
connection_ptr(const connection_ptr &) = delete;
|
||||
@@ -29,15 +29,16 @@ public:
|
||||
, pool_(x.pool_)
|
||||
{
|
||||
x.connection_ = nullptr;
|
||||
x.pool_ = nullptr;
|
||||
}
|
||||
connection_ptr& operator=(connection_ptr &&x) noexcept
|
||||
{
|
||||
if (this == &x) {
|
||||
return *this;
|
||||
}
|
||||
connection_ = x.connection_;
|
||||
pool_ = x.pool_;
|
||||
x.connection_ = nullptr;
|
||||
|
||||
std::swap(connection_, x.connection_);
|
||||
std::swap(pool_, x.pool_);
|
||||
|
||||
return *this;
|
||||
}
|
||||
@@ -52,8 +53,8 @@ public:
|
||||
private:
|
||||
friend class connection_pool<Connection>;
|
||||
|
||||
IdConnection<Connection> *connection_;
|
||||
connection_pool<Connection> &pool_;
|
||||
IdConnection<Connection> *connection_{};
|
||||
connection_pool<Connection> *pool_{};
|
||||
};
|
||||
|
||||
template < class Connection >
|
||||
@@ -74,10 +75,10 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
connection_ptr<Connection> acquire() {
|
||||
connection_pointer acquire() {
|
||||
std::unique_lock<std::mutex> lock(mutex_);
|
||||
if (idle_connections_.empty()) {
|
||||
return {nullptr, *this};
|
||||
return {nullptr, this};
|
||||
}
|
||||
|
||||
pointer next_connection{nullptr};
|
||||
@@ -87,10 +88,10 @@ public:
|
||||
inuse_connections_.insert(std::move(node));
|
||||
break;
|
||||
}
|
||||
return {next_connection, *this};
|
||||
return {next_connection, this};
|
||||
}
|
||||
|
||||
connection_ptr<Connection> acquire(size_t id) {
|
||||
connection_pointer acquire(size_t id) {
|
||||
using namespace std::chrono_literals;
|
||||
pointer next_connection{nullptr};
|
||||
auto try_count{0};
|
||||
@@ -98,17 +99,17 @@ public:
|
||||
|
||||
do {
|
||||
if (auto it = idle_connections_.find(id); it != idle_connections_.end()) {
|
||||
next_connection = it->second.second;
|
||||
next_connection = it->second;
|
||||
auto node = idle_connections_.extract(it);
|
||||
inuse_connections_.insert(std::move(node));
|
||||
} else {
|
||||
lock.unlock();
|
||||
std::this_thread::sleep_for(500ms);
|
||||
std::this_thread::sleep_for(100ms);
|
||||
lock.lock();
|
||||
}
|
||||
} while(try_count++ < 5);
|
||||
|
||||
return {next_connection, *this};
|
||||
return {next_connection, this};
|
||||
}
|
||||
|
||||
void release(IdConnection<Connection> *c) {
|
||||
@@ -153,7 +154,7 @@ private:
|
||||
|
||||
template<class Connection>
|
||||
connection_ptr<Connection>::~connection_ptr() {
|
||||
pool_.release(connection_);
|
||||
pool_->release(connection_);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user