Cache the peer address in the HTTP server

Later socket calls are expensive and might lead
into a race condition on close when logging it.

refs #6655
This commit is contained in:
Michael Friedrich 2018-10-09 15:40:16 +02:00
parent 9352f4bfb3
commit 5c32a5a7dc
2 changed files with 14 additions and 4 deletions

View File

@ -47,6 +47,17 @@ HttpServerConnection::HttpServerConnection(const String& identity, bool authenti
if (authenticated)
m_ApiUser = ApiUser::GetByClientCN(identity);
/* Cache the peer address. */
m_PeerAddress = "<unknown>";
if (stream) {
Socket::Ptr socket = m_Stream->GetSocket();
if (socket) {
m_PeerAddress = socket->GetPeerAddress();
}
}
}
void HttpServerConnection::StaticInitialize()
@ -84,7 +95,7 @@ void HttpServerConnection::Disconnect()
}
Log(LogInformation, "HttpServerConnection")
<< "HTTP client disconnected (from " << m_Stream->GetSocket()->GetPeerAddress() << ")";
<< "HTTP client disconnected (from " << m_PeerAddress << ")";
ApiListener::Ptr listener = ApiListener::GetInstance();
listener->RemoveHttpClient(this);
@ -201,11 +212,9 @@ bool HttpServerConnection::ManageHeaders(HttpResponse& response)
String requestUrl = m_CurrentRequest.RequestUrl->Format();
Socket::Ptr socket = m_Stream->GetSocket();
Log(LogInformation, "HttpServerConnection")
<< "Request: " << m_CurrentRequest.RequestMethod << " " << requestUrl
<< " (from " << (socket ? socket->GetPeerAddress() : "<unkown>")
<< " (from " << m_PeerAddress << ")"
<< ", user: " << (m_AuthenticatedUser ? m_AuthenticatedUser->GetName() : "<unauthenticated>") << ")";
ApiListener::Ptr listener = ApiListener::GetInstance();

View File

@ -59,6 +59,7 @@ private:
boost::recursive_mutex m_DataHandlerMutex;
WorkQueue m_RequestQueue;
int m_PendingRequests;
String m_PeerAddress;
StreamReadContext m_Context;