This is an automated email from the ASF dual-hosted git repository.

laiyingchun pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-pegasus.git


The following commit(s) were added to refs/heads/master by this push:
     new 9f41be714 refactor: make class 'dns_resolver' as a singleton (#1902)
9f41be714 is described below

commit 9f41be7148de5a2886896e7e4c6ff5c8bc02a009
Author: Guohao Li <[email protected]>
AuthorDate: Sun Feb 18 17:34:05 2024 +0800

    refactor: make class 'dns_resolver' as a singleton (#1902)
---
 src/runtime/rpc/dns_resolver.h      | 10 +++++++---
 src/runtime/rpc/group_address.h     |  4 ++--
 src/runtime/test/host_port_test.cpp |  7 +++----
 3 files changed, 12 insertions(+), 9 deletions(-)

diff --git a/src/runtime/rpc/dns_resolver.h b/src/runtime/rpc/dns_resolver.h
index aadb3287f..b4a1f0395 100644
--- a/src/runtime/rpc/dns_resolver.h
+++ b/src/runtime/rpc/dns_resolver.h
@@ -26,6 +26,7 @@
 #include "runtime/rpc/rpc_host_port.h"
 #include "utils/errors.h"
 #include "utils/metrics.h"
+#include "utils/singleton.h"
 #include "utils/synchronize.h"
 
 namespace dsn {
@@ -37,15 +38,18 @@ namespace dsn {
 // effect.
 // TODO(yingchun): Now the cache is unlimited, the cache size may be huge. 
Implement an expiration
 // mechanism to limit the cache size and make it possible to update the 
resolve result.
-class dns_resolver
+class dns_resolver : public utils::singleton<dns_resolver>
 {
 public:
-    explicit dns_resolver();
-
     // Resolve this host_port to an unique rpc_address.
     rpc_address resolve_address(const host_port &hp);
 
 private:
+    dns_resolver();
+    ~dns_resolver() = default;
+
+    friend class utils::singleton<dns_resolver>;
+
     bool get_cached_addresses(const host_port &hp, std::vector<rpc_address> 
&addresses);
 
     error_s resolve_addresses(const host_port &hp, std::vector<rpc_address> 
&addresses);
diff --git a/src/runtime/rpc/group_address.h b/src/runtime/rpc/group_address.h
index 08689b6fc..fdd97a798 100644
--- a/src/runtime/rpc/group_address.h
+++ b/src/runtime/rpc/group_address.h
@@ -55,7 +55,7 @@ public:
     void set_leader(rpc_address addr);
     bool remove(rpc_address addr) WARN_UNUSED_RESULT;
     bool contains(rpc_address addr) const WARN_UNUSED_RESULT;
-    int count();
+    int count() const;
 
     const std::vector<rpc_address> &members() const { return _members; }
     rpc_address random_member() const
@@ -194,7 +194,7 @@ inline bool rpc_group_address::contains(rpc_address addr) 
const
     return _members.end() != std::find(_members.begin(), _members.end(), addr);
 }
 
-inline int rpc_group_address::count()
+inline int rpc_group_address::count() const
 {
     alr_t l(_lock);
     return _members.size();
diff --git a/src/runtime/test/host_port_test.cpp 
b/src/runtime/test/host_port_test.cpp
index 2f7dd24a7..0ab4f1f2e 100644
--- a/src/runtime/test/host_port_test.cpp
+++ b/src/runtime/test/host_port_test.cpp
@@ -230,10 +230,9 @@ TEST(host_port_test, transfer_rpc_address)
 
 TEST(host_port_test, dns_resolver)
 {
-    dns_resolver resolver;
     {
         host_port hp("localhost", 8080);
-        auto addr = resolver.resolve_address(hp);
+        const auto &addr = dns_resolver::instance().resolve_address(hp);
         ASSERT_TRUE(rpc_address("127.0.0.1", 8080) == addr ||
                     rpc_address("127.0.1.1", 8080) == addr);
     }
@@ -248,8 +247,8 @@ TEST(host_port_test, dns_resolver)
         host_port hp2("localhost", 8081);
         g_hp->set_leader(hp2);
 
-        auto addr_grp = resolver.resolve_address(hp_grp);
-        auto g_addr = addr_grp.group_address();
+        const auto &addr_grp = 
dns_resolver::instance().resolve_address(hp_grp);
+        const auto *const g_addr = addr_grp.group_address();
 
         ASSERT_EQ(g_addr->is_update_leader_automatically(), 
g_hp->is_update_leader_automatically());
         ASSERT_STREQ(g_addr->name(), g_hp->name());


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to