]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
dnsdist: Get rid of the reference counter for DOHUnit
authorRemi Gacogne <remi.gacogne@powerdns.com>
Mon, 31 Jul 2023 09:46:16 +0000 (11:46 +0200)
committerRemi Gacogne <remi.gacogne@powerdns.com>
Thu, 7 Sep 2023 07:19:12 +0000 (09:19 +0200)
It is no longer needed since we now rely on moving the unique pointer
around.

pdns/dnsdist-idstate.hh
pdns/dnsdistdist/doh.cc
pdns/doh.hh

index b61984249e5a5a35f69aba2a9c41dccc98c6746c..cf5442fea0b254b16acadc0e5e6406f5bccfa6dd 100644 (file)
@@ -99,12 +99,7 @@ struct InternalQueryState
     std::string d_requestorID;
   };
 
-  static void DeleterPlaceHolder(DOHUnit*)
-  {
-  }
-
-  InternalQueryState() :
-    du(std::unique_ptr<DOHUnit, void (*)(DOHUnit*)>(nullptr, DeleterPlaceHolder))
+  InternalQueryState()
   {
     origDest.sin4.sin_family = 0;
   }
@@ -130,7 +125,7 @@ struct InternalQueryState
   std::unique_ptr<ProtoBufData> d_protoBufData{nullptr};
   boost::optional<uint32_t> tempFailureTTL{boost::none}; // 8
   ClientState* cs{nullptr}; // 8
-  std::unique_ptr<DOHUnit, void (*)(DOHUnit*)> du; // 8
+  std::unique_ptr<DOHUnit> du{nullptr}; // 8
   uint32_t cacheKey{0}; // 4
   uint32_t cacheKeyNoECS{0}; // 4
   // DoH-only */
index 3a05d1c2a74454c28a57b840dce2c090b02f8c08..eeb0af48084743300e25cc84e1518792a34496a4 100644 (file)
@@ -163,7 +163,6 @@ public:
 
 private:
   h2o_accept_ctx_t d_h2o_accept_ctx;
-  std::atomic<uint64_t> d_refcnt{1};
   time_t d_ticketsKeyNextRotation{0};
   std::atomic_flag d_rotatingTicketsKey;
 };
@@ -176,14 +175,14 @@ struct DOHServerConfig
   {
 #ifndef USE_SINGLE_ACCEPTOR_THREAD
     {
-      auto [sender, receiver] = pdns::channel::createObjectQueue<DOHUnit, void(*)(DOHUnit*)>(pdns::channel::SenderBlockingMode::SenderNonBlocking, pdns::channel::ReceiverBlockingMode::ReceiverBlocking, internalPipeBufferSize);
+      auto [sender, receiver] = pdns::channel::createObjectQueue<DOHUnit>(pdns::channel::SenderBlockingMode::SenderNonBlocking, pdns::channel::ReceiverBlockingMode::ReceiverBlocking, internalPipeBufferSize);
       d_querySender = std::move(sender);
       d_queryReceiver = std::move(receiver);
     }
 #endif /* USE_SINGLE_ACCEPTOR_THREAD */
 
     {
-      auto [sender, receiver] = pdns::channel::createObjectQueue<DOHUnit, void(*)(DOHUnit*)>(pdns::channel::SenderBlockingMode::SenderNonBlocking, pdns::channel::ReceiverBlockingMode::ReceiverNonBlocking, internalPipeBufferSize);
+      auto [sender, receiver] = pdns::channel::createObjectQueue<DOHUnit>(pdns::channel::SenderBlockingMode::SenderNonBlocking, pdns::channel::ReceiverBlockingMode::ReceiverNonBlocking, internalPipeBufferSize);
       d_responseSender = std::move(sender);
       d_responseReceiver = std::move(receiver);
     }
@@ -209,11 +208,11 @@ struct DOHServerConfig
   ClientState* cs{nullptr};
   std::shared_ptr<DOHFrontend> df{nullptr};
 #ifndef USE_SINGLE_ACCEPTOR_THREAD
-  pdns::channel::Sender<DOHUnit, void(*)(DOHUnit*)> d_querySender;
-  pdns::channel::Receiver<DOHUnit, void(*)(DOHUnit*)> d_queryReceiver;
+  pdns::channel::Sender<DOHUnit> d_querySender;
+  pdns::channel::Receiver<DOHUnit> d_queryReceiver;
 #endif /* USE_SINGLE_ACCEPTOR_THREAD */
-  pdns::channel::Sender<DOHUnit, void(*)(DOHUnit*)> d_responseSender;
-  pdns::channel::Receiver<DOHUnit, void(*)(DOHUnit*)> d_responseReceiver;
+  pdns::channel::Sender<DOHUnit> d_responseSender;
+  pdns::channel::Receiver<DOHUnit> d_responseReceiver;
 };
 
 /* This internal function sends back the object to the main thread to send a reply.
@@ -839,7 +838,7 @@ static void doh_dispatch_query(DOHServerConfig* dsc, h2o_handler_t* self, h2o_re
     /* we are doing quite some copies here, sorry about that,
        but we can't keep accessing the req object once we are in a different thread
        because the request might get killed by h2o at pretty much any time */
-    auto du = std::unique_ptr<DOHUnit, void(*)(DOHUnit*)>(new DOHUnit(std::move(query), std::move(path), std::string(req->authority.base, req->authority.len)), DOHUnit::release);
+    auto du = std::make_unique<DOHUnit>(std::move(query), std::move(path), std::string(req->authority.base, req->authority.len));
     du->dsc = dsc;
     du->req = req;
     du->ids.origDest = local;
@@ -870,7 +869,7 @@ static void doh_dispatch_query(DOHServerConfig* dsc, h2o_handler_t* self, h2o_re
     *(du->self) = du.get();
 
 #ifdef USE_SINGLE_ACCEPTOR_THREAD
-    processDOHQuery(DOHUnitUniquePtr(du.release(), DOHUnit::release), true);
+    processDOHQuery(du, true);
 #else /* USE_SINGLE_ACCEPTOR_THREAD */
     try {
       if (!dsc->d_querySender.send(std::move(du))) {
@@ -1232,13 +1231,13 @@ void DOHUnit::setHTTPResponse(uint16_t statusCode, PacketBuffer&& body_, const s
 /* query has been parsed by h2o, which called doh_handler() in the main DoH thread.
    In order not to block for long, doh_handler() called doh_dispatch_query() which allocated
    a DOHUnit object and passed it to us */
-static void dnsdistclient(pdns::channel::Receiver<DOHUnit, void(*)(DOHUnit*)>&& receiver)
+static void dnsdistclient(pdns::channel::Receiver<DOHUnit>&& receiver)
 {
   setThreadName("dnsdist/doh-cli");
 
   for(;;) {
     try {
-      auto tmp = receiver.receive(DOHUnit::release);
+      auto tmp = receiver.receive();
       if (!tmp) {
         continue;
       }
@@ -1281,9 +1280,9 @@ static void on_dnsdist(h2o_socket_t *listener, const char *err)
      memory and likely coming up too late after the client has gone away */
   auto* dsc = static_cast<DOHServerConfig*>(listener->data);
   while (true) {
-    std::unique_ptr<DOHUnit, void(*)(DOHUnit*)> du{nullptr, DOHUnit::release};
+    std::unique_ptr<DOHUnit> du{nullptr};
     try {
-      auto tmp = dsc->d_responseReceiver.receive(DOHUnit::release);
+      auto tmp = dsc->d_responseReceiver.receive();
       if (!tmp) {
         return;
       }
index b38c7bbf302071bbbec991746ff261c8e201077c..6f3816c300fa8f131eed7da0b86aed551e081df5 100644 (file)
@@ -180,18 +180,6 @@ struct DOHFrontend
 #ifndef HAVE_DNS_OVER_HTTPS
 struct DOHUnit
 {
-  static void release(DOHUnit*)
-  {
-  }
-
-  void get()
-  {
-  }
-
-  void release()
-  {
-  }
-
   size_t proxyProtocolPayloadSize{0};
   uint16_t status_code{200};
 };
@@ -215,29 +203,6 @@ struct DOHUnit
   DOHUnit(const DOHUnit&) = delete;
   DOHUnit& operator=(const DOHUnit&) = delete;
 
-  void get()
-  {
-    ++d_refcnt;
-  }
-
-  void release()
-  {
-    if (--d_refcnt == 0) {
-      if (self) {
-        *self = nullptr;
-      }
-
-      delete this;
-    }
-  }
-
-  static void release(DOHUnit* ptr)
-  {
-    if (ptr) {
-      ptr->release();
-    }
-  }
-
   InternalQueryState ids;
   std::string sni;
   std::string path;
@@ -251,8 +216,7 @@ struct DOHUnit
   st_h2o_req_t* req{nullptr};
   DOHUnit** self{nullptr};
   DOHServerConfig* dsc{nullptr};
-  pdns::channel::Sender<DOHUnit, void(*)(DOHUnit*)>* responseSender{nullptr};
-  std::atomic<uint64_t> d_refcnt{1};
+  pdns::channel::Sender<DOHUnit>* responseSender{nullptr};
   size_t query_at{0};
   size_t proxyProtocolPayloadSize{0};
   int rsock{-1};
@@ -277,7 +241,7 @@ struct DOHUnit
   void setHTTPResponse(uint16_t statusCode, PacketBuffer&& body, const std::string& contentType="");
 };
 
-void handleUDPResponseForDoH(std::unique_ptr<DOHUnit, void(*)(DOHUnit*)>&&, PacketBuffer&& response, InternalQueryState&& state);
+void handleUDPResponseForDoH(std::unique_ptr<DOHUnit>&&, PacketBuffer&& response, InternalQueryState&& state);
 
 struct CrossProtocolQuery;
 struct DNSQuestion;
@@ -287,6 +251,6 @@ std::unique_ptr<CrossProtocolQuery> getDoHCrossProtocolQueryFromDQ(DNSQuestion&
 #endif /* HAVE_LIBH2OEVLOOP */
 #endif /* HAVE_DNS_OVER_HTTPS  */
 
-using DOHUnitUniquePtr = std::unique_ptr<DOHUnit, void(*)(DOHUnit*)>;
+using DOHUnitUniquePtr = std::unique_ptr<DOHUnit>;
 
 void handleDOHTimeout(DOHUnitUniquePtr&& oldDU);