webserver: Remove the Request ID post serving.

It is observed that for every request there is a mapping
of Request ID to ProtocolHandler is being added to the map
while processing the request. After successfully processing
the request, Request ID is not removed from the map causing
the heap memory to increase constantly. This CL fixes this
problem by removing the Request ID from the map once
it is successfully completed.

BUG=b:153568555
TEST=Compiled successfully and verified
     It is observed that post this change and testing for
     more than 16hours didn't observe significant increase
     in heap memory

Change-Id: Id6b201529a0136f222d22f41df675ba62eb48f2c
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform2/+/2162355
Reviewed-by: Raju Konduru <rkonduru@google.com>
Reviewed-by: John Riordan <jrio@chromium.org>
Reviewed-by: Kishan Kunduru <kkunduru@chromium.org>
Reviewed-by: Hidehiko Abe <hidehiko@chromium.org>
Commit-Queue: Kishan Kunduru <kkunduru@chromium.org>
Tested-by: Kishan Kunduru <kkunduru@chromium.org>
(cherry picked from commit b144d64fd4d9b5c2c03c035c2742e3898f9b7831)
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform2/+/2172113
Reviewed-by: SHAMANTHA KUMAR PERAMACHANAHALLI RAMARAO <shamanthakumar@google.com>
Tested-by: SHAMANTHA KUMAR PERAMACHANAHALLI RAMARAO <shamanthakumar@google.com>
Tested-by: Shiva Sai Kothapalli <kshivasai@google.com>
Commit-Queue: Shiva Sai Kothapalli <kshivasai@google.com>
diff --git a/webserver/libwebserv/dbus_protocol_handler.cc b/webserver/libwebserv/dbus_protocol_handler.cc
index 721c2c1..b5953e1 100644
--- a/webserver/libwebserv/dbus_protocol_handler.cc
+++ b/webserver/libwebserv/dbus_protocol_handler.cc
@@ -167,8 +167,10 @@
 
 void DBusProtocolHandler::Disconnect(const dbus::ObjectPath& object_path) {
   proxies_.erase(object_path);
-  if (proxies_.empty())
+  if (proxies_.empty()) {
     remote_handler_id_map_.clear();
+    request_id_map_.clear();
+  }
   for (auto& pair : request_handlers_)
     pair.second.remote_handler_ids.clear();
 }
@@ -194,7 +196,6 @@
                                      const std::string& request_id,
                                      std::unique_ptr<Request> request,
                                      brillo::ErrorPtr* error) {
-  request_id_map_.emplace(request_id, protocol_handler_id);
   auto id_iter = remote_handler_id_map_.find(remote_handler_id);
   if (id_iter == remote_handler_id_map_.end()) {
     brillo::Error::AddToPrintf(error, FROM_HERE,
@@ -213,6 +214,7 @@
                                id_iter->second);
     return false;
   }
+  request_id_map_.emplace(request_id, protocol_handler_id);
   handler_iter->second.handler->HandleRequest(
       std::move(request),
       std::unique_ptr<Response>{new DBusResponse{this, request_id}});
@@ -224,10 +226,18 @@
     int status_code,
     const std::multimap<std::string, std::string>& headers,
     brillo::StreamPtr data_stream) {
+
+  // Once request gets a response and the request_id_map should remove
+  // the request id from the map
+  auto clear_request_itr = request_id_map_.find(request_id);
   ProtocolHandlerProxyInterface* proxy =
       GetRequestProtocolHandlerProxy(request_id);
-  if (!proxy)
+  if (!proxy) {
+    if (clear_request_itr != request_id_map_.end()) {
+      request_id_map_.erase(clear_request_itr);
+    }
     return;
+  }
 
   std::vector<std::tuple<std::string, std::string>> header_list;
   header_list.reserve(headers.size());
@@ -241,6 +251,9 @@
       request_id, status_code, header_list, data_size,
       base::Bind(&WriteResponseData, base::Passed(&data_stream)),
       base::Bind(&IgnoreDBusError));
+  if (clear_request_itr != request_id_map_.end()) {
+    request_id_map_.erase(clear_request_itr);
+  }
 }
 
 void DBusProtocolHandler::GetFileData(