38 {
39 {
40 std::lock_guard<std::mutex> lk(
mutex_);
41 auto itc =
cache_.find(host);
42 if (itc !=
cache_.end() &&
now_ms() < itc->second.expires) {
43 std::promise<std::string> p;
44 p.set_value(itc->second.ip);
45 return p.get_future();
46 }
49 return itp->second->get_future();
50 }
51 }
52
53 auto prom = std::make_shared<std::promise<std::string>>();
54 {
55 std::lock_guard<std::mutex> lk(
mutex_);
57 }
58
59 std::thread([this, host, prom]() {
60 std::string ip;
61 struct addrinfo hints{}, *res = nullptr;
63 hints.ai_socktype = SOCK_STREAM;
64 int rc = getaddrinfo(host.c_str(), nullptr, &hints, &res);
65 if (rc == 0 && res) {
66 char buf[INET_ADDRSTRLEN] = {0};
67 if (res->ai_family ==
AF_INET) {
68 auto *a = reinterpret_cast<struct sockaddr_in*>(res->ai_addr);
69 inet_ntop(
AF_INET, &a->sin_addr, buf,
sizeof(buf));
70 ip = buf;
71 }
72 freeaddrinfo(res);
73 }
74 {
75 std::lock_guard<std::mutex> lk(
mutex_);
78 }
79 prom->set_value(ip);
80 }).detach();
81
82 return prom->get_future();
83 }
std::unordered_map< std::string, std::shared_ptr< std::promise< std::string > > > pending_
static constexpr uint64_t TTL_MS
std::unordered_map< std::string, Entry > cache_