forked from tjanczuk/iisnode
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix #122: named pipe connection pooling
- Loading branch information
Showing
10 changed files
with
225 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
#include "precomp.h" | ||
|
||
CConnectionPool::CConnectionPool() | ||
: count(0), list(NULL) | ||
{ | ||
} | ||
|
||
HRESULT CConnectionPool::Initialize(IHttpContext* ctx) | ||
{ | ||
HRESULT hr; | ||
|
||
ErrorIf(NULL == (this->list = (PSLIST_HEADER)_aligned_malloc(sizeof(SLIST_HEADER), MEMORY_ALLOCATION_ALIGNMENT)), ERROR_NOT_ENOUGH_MEMORY); | ||
this->maxPoolSize = 512; // TODO, expose configuration for this | ||
this->maxAge = 30000; // TODO, expose configuration for this | ||
|
||
InitializeSListHead(this->list); | ||
|
||
return S_OK; | ||
Error: | ||
return hr; | ||
} | ||
|
||
CConnectionPool::~CConnectionPool() | ||
{ | ||
PSLIST_ENTRY entry; | ||
|
||
if (this->list) | ||
{ | ||
while (NULL != (entry = InterlockedPopEntrySList(this->list))) | ||
{ | ||
CloseHandle(((PCONNECTION_ENTRY)entry)->connection); | ||
_aligned_free(entry); | ||
} | ||
|
||
_aligned_free(this->list); | ||
this->list = NULL; | ||
} | ||
} | ||
|
||
HRESULT CConnectionPool::Return(HANDLE connection) | ||
{ | ||
HRESULT hr; | ||
PCONNECTION_ENTRY entry = NULL; | ||
|
||
if (this->count >= this->maxPoolSize) | ||
{ | ||
CloseHandle(connection); | ||
} | ||
else | ||
{ | ||
ErrorIf(NULL == (entry = (PCONNECTION_ENTRY)_aligned_malloc(sizeof(CONNECTION_ENTRY), MEMORY_ALLOCATION_ALIGNMENT)), ERROR_NOT_ENOUGH_MEMORY); | ||
entry->connection = connection; | ||
entry->timestamp = GetTickCount(); | ||
InterlockedPushEntrySList(this->list, &(entry->listEntry)); | ||
InterlockedIncrement(&this->count); | ||
} | ||
|
||
return S_OK; | ||
Error: | ||
|
||
return hr; | ||
} | ||
|
||
HANDLE CConnectionPool::Take() | ||
{ | ||
HANDLE result = INVALID_HANDLE_VALUE; | ||
PCONNECTION_ENTRY entry; | ||
DWORD now = GetTickCount(); | ||
|
||
while (NULL != (entry = (PCONNECTION_ENTRY)InterlockedPopEntrySList(this->list))) | ||
{ | ||
InterlockedDecrement(&this->count); | ||
if ((now - entry->timestamp) < this->maxAge) | ||
{ | ||
result = entry->connection; | ||
} | ||
else | ||
{ | ||
CloseHandle(entry->connection); | ||
} | ||
|
||
_aligned_free(entry); | ||
|
||
if (INVALID_HANDLE_VALUE != result) | ||
{ | ||
break; | ||
} | ||
} | ||
|
||
return result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#ifndef __CCONNECTIONPOOL_H__ | ||
#define __CCONNECTIONPOOL_H__ | ||
|
||
class CConnectionPool | ||
{ | ||
private: | ||
|
||
typedef struct _CONNECTION_ENTRY { | ||
SLIST_ENTRY listEntry; | ||
HANDLE connection; | ||
DWORD timestamp; | ||
} CONNECTION_ENTRY, *PCONNECTION_ENTRY; | ||
|
||
PSLIST_HEADER list; | ||
unsigned long count; | ||
DWORD maxPoolSize; | ||
DWORD maxAge; | ||
|
||
public: | ||
|
||
CConnectionPool(); | ||
~CConnectionPool(); | ||
|
||
HRESULT Initialize(IHttpContext* ctx); | ||
HRESULT Return(HANDLE connection); | ||
HANDLE Take(); | ||
|
||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters