Skip to content

Commit

Permalink
修复连接池计数不准的bug
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyangzi committed Apr 29, 2019
1 parent 9d6ea8b commit 781720f
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions libgo/pool/connection_pool.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class ConnectionPool
{ {
Connection* ptr = nullptr; Connection* ptr = nullptr;
while (channel_.TryPop(ptr)) { while (channel_.TryPop(ptr)) {
deleter_(ptr); Delete(ptr);
} }
} }


Expand All @@ -51,7 +51,10 @@ class ConnectionPool
{ {
Connection* connection = CreateOne(); Connection* connection = CreateOne();
if (!connection) break; if (!connection) break;
if (!channel_.TryPush(connection)) break; if (!channel_.TryPush(connection)) {
Delete(connection);
break;
}
} }
} }


Expand All @@ -65,7 +68,7 @@ class ConnectionPool
retry_get: retry_get:
if (channel_.TryPop(connection)) { if (channel_.TryPop(connection)) {
if (checkAliveOnGet && !checkAliveOnGet(connection)) { if (checkAliveOnGet && !checkAliveOnGet(connection)) {
deleter_(connection); Delete(connection);
connection = nullptr; connection = nullptr;
goto retry_get; goto retry_get;
} }
Expand All @@ -79,7 +82,7 @@ class ConnectionPool


channel_ >> connection; channel_ >> connection;
if (checkAliveOnGet && !checkAliveOnGet(connection)) { if (checkAliveOnGet && !checkAliveOnGet(connection)) {
deleter_(connection); Delete(connection);
connection = nullptr; connection = nullptr;
goto retry_get; goto retry_get;
} }
Expand All @@ -103,7 +106,7 @@ class ConnectionPool
retry_get: retry_get:
if (channel_.TryPop(connection)) { if (channel_.TryPop(connection)) {
if (checkAliveOnGet && !checkAliveOnGet(connection)) { if (checkAliveOnGet && !checkAliveOnGet(connection)) {
deleter_(connection); Delete(connection);
connection = nullptr; connection = nullptr;
goto retry_get; goto retry_get;
} }
Expand All @@ -117,7 +120,7 @@ class ConnectionPool


if (channel_.TimedPop(connection, deadline)) { if (channel_.TimedPop(connection, deadline)) {
if (checkAliveOnGet && !checkAliveOnGet(connection)) { if (checkAliveOnGet && !checkAliveOnGet(connection)) {
deleter_(connection); Delete(connection);
connection = nullptr; connection = nullptr;
goto retry_get; goto retry_get;
} }
Expand All @@ -136,7 +139,7 @@ class ConnectionPool
private: private:
Connection* CreateOne() Connection* CreateOne()
{ {
if (++count_ >= maxConnection_) { if (++count_ > maxConnection_) {
--count_; --count_;
return nullptr; return nullptr;
} }
Expand All @@ -147,16 +150,21 @@ class ConnectionPool
void Put(Connection* connection) void Put(Connection* connection)
{ {
if (!channel_.TryPush(connection)) { if (!channel_.TryPush(connection)) {
--count_; Delete(connection);
deleter_(connection);
} }
} }


void Delete(Connection* connection)
{
--count_;
deleter_(connection);
}

ConnectionPtr Out(Connection* connection, CheckAlive checkAliveOnPut) ConnectionPtr Out(Connection* connection, CheckAlive checkAliveOnPut)
{ {
return ConnectionPtr(connection, [this, checkAliveOnPut](Connection* ptr){ return ConnectionPtr(connection, [this, checkAliveOnPut](Connection* ptr){
if (checkAliveOnPut && !checkAliveOnPut(ptr)) { if (checkAliveOnPut && !checkAliveOnPut(ptr)) {
this->deleter_(ptr); this->Delete(ptr);
return ; return ;
} }


Expand Down

0 comments on commit 781720f

Please sign in to comment.