Skip to content
Permalink
Browse files

修复连接池计数不准的bug

  • Loading branch information...
yuyangzi
yuyangzi committed Apr 29, 2019
1 parent 9d6ea8b commit 781720f7ca6cf0de2d82e5a791a9c775cbad5aa3
Showing with 18 additions and 10 deletions.
  1. +18 −10 libgo/pool/connection_pool.h
@@ -39,7 +39,7 @@ class ConnectionPool
{
Connection* ptr = nullptr;
while (channel_.TryPop(ptr)) {
deleter_(ptr);
Delete(ptr);
}
}

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

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

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

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

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

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

0 comments on commit 781720f

Please sign in to comment.
You can’t perform that action at this time.