Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: make class 'dns_resolver' as a singleton #1902

Merged
merged 2 commits into from
Feb 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/runtime/rpc/dns_resolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,6 @@ rpc_address dns_resolver::resolve_address(const host_port &hp)
}
}

dns_resolver::~dns_resolver() { _dns_cache.clear(); }
GehaFearless marked this conversation as resolved.
Show resolved Hide resolved

} // namespace dsn
10 changes: 7 additions & 3 deletions src/runtime/rpc/dns_resolver.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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();

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);
Expand Down
5 changes: 2 additions & 3 deletions src/runtime/test/host_port_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
auto addr = dns_resolver::instance().resolve_address(hp);
GehaFearless marked this conversation as resolved.
Show resolved Hide resolved
ASSERT_TRUE(rpc_address("127.0.0.1", 8080) == addr ||
rpc_address("127.0.1.1", 8080) == addr);
}
Expand All @@ -248,7 +247,7 @@ 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 addr_grp = dns_resolver::instance().resolve_address(hp_grp);
GehaFearless marked this conversation as resolved.
Show resolved Hide resolved
auto g_addr = addr_grp.group_address();
GehaFearless marked this conversation as resolved.
Show resolved Hide resolved

ASSERT_EQ(g_addr->is_update_leader_automatically(), g_hp->is_update_leader_automatically());
Expand Down
Loading