Make CaresPTRResolver completely thread-safe

This commit is contained in:
Arthur Passos 2022-10-21 14:20:49 -03:00
parent 7d11054134
commit 2c41c0357d
4 changed files with 57 additions and 1 deletions

View File

@ -2,6 +2,7 @@
#include <span>
#include <poll.h>
#include <mutex>
#include "DNSPTRResolver.h"
using ares_channel = struct ares_channeldata *;
@ -46,6 +47,8 @@ namespace DB
void process_readable_sockets(std::span<pollfd> readable_sockets);
ares_channel channel;
static std::mutex mutex;
};
}

View File

@ -1,12 +1,15 @@
#include "DNSPTRResolverProvider.h"
#include "LockedDNSPTRResolver.h"
#include "CaresPTRResolver.h"
namespace DB
{
std::shared_ptr<DNSPTRResolver> DNSPTRResolverProvider::get()
{
return std::make_shared<CaresPTRResolver>(
static auto resolver = std::make_shared<CaresPTRResolver>(
CaresPTRResolver::provider_token {}
);
return resolver;
}
}

View File

@ -0,0 +1,25 @@
#include "LockedDNSPTRResolver.h"
namespace DB {
std::mutex LockedPTRResolver::mutex;
LockedPTRResolver::LockedPTRResolver(std::unique_ptr<DNSPTRResolver> resolver_)
: resolver(std::move(resolver_))
{}
std::unordered_set<std::string> LockedPTRResolver::resolve(const std::string & ip)
{
std::lock_guard guard(mutex);
return resolver->resolve(ip);
}
std::unordered_set<std::string> LockedPTRResolver::resolve_v6(const std::string & ip)
{
std::lock_guard guard(mutex);
return resolver->resolve_v6(ip);
}
}

View File

@ -0,0 +1,25 @@
#pragma once
#include <mutex>
#include <Common/DNSPTRResolver.h>
namespace DB {
class LockedPTRResolver : public DNSPTRResolver
{
public:
LockedPTRResolver(std::unique_ptr<DNSPTRResolver> resolver);
std::unordered_set<std::string> resolve(const std::string & ip) override;
std::unordered_set<std::string> resolve_v6(const std::string & ip) override;
private:
// this needs to be owned
std::unique_ptr<DNSPTRResolver> resolver;
static std::mutex mutex;
};
}