GetHostByName: split cached values and active queries.

GitOrigin-RevId: c3d0ebd9202a22bf83ef03a044bda0b2d00d46b6
This commit is contained in:
levlam 2019-02-04 03:19:31 +03:00
parent 2dd4b91d51
commit 73a39a4699
2 changed files with 20 additions and 14 deletions

View File

@ -171,15 +171,16 @@ void GetHostByNameActor::run(string host, int port, bool prefer_ipv6, Promise<IP
return promise.set_result(value.get_ip_port(port));
}
value.promises.emplace_back(port, std::move(promise));
if (value.query.empty()) {
CHECK(value.promises.size() == 1);
auto &query = active_queries_[prefer_ipv6][host];
query.promises.emplace_back(port, std::move(promise));
if (query.query.empty()) {
CHECK(query.promises.size() == 1);
ResolveOptions options;
options.type = options_.type;
options.scheduler_id = options_.scheduler_id;
options.prefer_ipv6 = prefer_ipv6;
value.query =
query.query =
resolve(host, options,
PromiseCreator::lambda([actor_id = actor_id(this), host, prefer_ipv6](Result<IPAddress> res) mutable {
send_closure(actor_id, &GetHostByNameActor::on_result, std::move(host), prefer_ipv6, std::move(res));
@ -191,14 +192,16 @@ void GetHostByNameActor::on_result(std::string host, bool prefer_ipv6, Result<IP
auto value_it = cache_[prefer_ipv6].find(host);
CHECK(value_it != cache_[prefer_ipv6].end());
auto &value = value_it->second;
CHECK(!value.promises.empty());
CHECK(!value.query.empty());
auto query_it = active_queries_[prefer_ipv6].find(host);
CHECK(query_it != active_queries_[prefer_ipv6].end());
auto &query = query_it->second;
CHECK(!query.promises.empty());
CHECK(!query.query.empty());
auto promises = std::move(value.promises);
auto promises = std::move(query.promises);
auto end_time = Time::now() + (res.is_ok() ? options_.ok_timeout : options_.error_timeout);
value = Value{std::move(res), end_time};
CHECK(value.promises.empty());
CHECK(value.query.empty());
active_queries_[prefer_ipv6].erase(query_it);
for (auto &promise : promises) {
promise.second.set_result(value.get_ip_port(promise.first));

View File

@ -42,13 +42,12 @@ class GetHostByNameActor final : public Actor {
static TD_WARN_UNUSED_RESULT ActorOwn<> resolve(std::string host, ResolveOptions options, Promise<IPAddress> promise);
private:
void on_result(std::string host, bool prefer_ipv6, Result<IPAddress> res);
struct Value {
Result<IPAddress> ip;
double expire_at;
ActorOwn<> query;
std::vector<std::pair<int, Promise<IPAddress>>> promises;
Value(Result<IPAddress> ip, double expire_at) : ip(std::move(ip)), expire_at(expire_at) {
}
@ -62,9 +61,13 @@ class GetHostByNameActor final : public Actor {
};
std::unordered_map<string, Value> cache_[2];
Options options_;
struct Query {
ActorOwn<> query;
std::vector<std::pair<int, Promise<IPAddress>>> promises;
};
std::unordered_map<string, Query> active_queries_[2];
void on_result(std::string host, bool prefer_ipv6, Result<IPAddress> res);
Options options_;
};
} // namespace td