-
Notifications
You must be signed in to change notification settings - Fork 108
Socket pool liveness check #2574
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -50,17 +50,31 @@ void SSocketPool::_timeoutThreadFunc() | |
|
|
||
| unique_ptr<STCPManager::Socket> SSocketPool::getSocket() | ||
| { | ||
| { | ||
| // If there's an existing socket, return it. | ||
| lock_guard<mutex> lock(_poolMutex); | ||
| if (_sockets.size()) { | ||
| pair<chrono::steady_clock::time_point, unique_ptr<STCPManager::Socket>> s = move(_sockets.front()); | ||
| while (true) { | ||
| unique_ptr<STCPManager::Socket> socket; | ||
| { | ||
| lock_guard<mutex> lock(_poolMutex); | ||
| if (_sockets.empty()) { | ||
| break; | ||
| } | ||
| socket = move(_sockets.front().second); | ||
| _sockets.pop_front(); | ||
| return move(s.second); | ||
| } | ||
|
|
||
| // Verify the socket is still alive before returning it. | ||
| if (socket->state.load() == STCPManager::Socket::CONNECTED) { | ||
| struct pollfd pfd = {socket->s, POLLIN | POLLOUT, 0}; | ||
| int ret = poll(&pfd, 1, 0); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
If Useful? React with 👍 / 👎.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The chances of this happening are so low and even if it happens the cost is just opening a new socket, I don't think this matters. |
||
| if (ret >= 0 && !(pfd.revents & (POLLERR | POLLHUP | POLLNVAL))) { | ||
|
Comment on lines
+66
to
+68
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This liveness check treats a socket as healthy whenever Useful? React with 👍 / 👎.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @tylerkaraszewski is this valid? |
||
| return socket; | ||
| } | ||
| } | ||
|
|
||
| // Socket is dead, discard it (destructor closes fd). | ||
| SINFO("[SOCKET] Discarding stale socket from pool for host '" << host << "'."); | ||
| } | ||
|
|
||
| // If we get here, we need to create a socket to return. No need to hold the lock, so it goes out of scope. | ||
| // No live pooled socket available, create a new one. No need to hold the lock, so it goes out of scope. | ||
| try { | ||
| // TODO: Allow S_socket to take a parsed address instead of redoing all the parsing each time. | ||
| return unique_ptr<STCPManager::Socket>(new STCPManager::Socket(host)); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.