]> granicus.if.org Git - icinga2/commitdiff
Cache the peer address in the HTTP server 6661/head
authorMichael Friedrich <michael.friedrich@icinga.com>
Tue, 9 Oct 2018 13:40:16 +0000 (15:40 +0200)
committerMichael Friedrich <michael.friedrich@icinga.com>
Tue, 9 Oct 2018 13:40:16 +0000 (15:40 +0200)
Later socket calls are expensive and might lead
into a race condition on close when logging it.

refs #6655

lib/remote/httpserverconnection.cpp
lib/remote/httpserverconnection.hpp

index f4bb144394dceb968e65db97bb4fcc575f25e923..9d8f192c453aaf231136e77605a4e817c0b4d7bd 100644 (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();
index 6d1c3bd46871c1ee7df178005c165c98ed68903b..7712ad66d85d55ed34454d1ff5de90238002c763 100644 (file)
@@ -59,6 +59,7 @@ private:
        boost::recursive_mutex m_DataHandlerMutex;
        WorkQueue m_RequestQueue;
        int m_PendingRequests;
+       String m_PeerAddress;
 
        StreamReadContext m_Context;