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)); return promise.set_result(value.get_ip_port(port));
} }
value.promises.emplace_back(port, std::move(promise)); auto &query = active_queries_[prefer_ipv6][host];
if (value.query.empty()) { query.promises.emplace_back(port, std::move(promise));
CHECK(value.promises.size() == 1); if (query.query.empty()) {
CHECK(query.promises.size() == 1);
ResolveOptions options; ResolveOptions options;
options.type = options_.type; options.type = options_.type;
options.scheduler_id = options_.scheduler_id; options.scheduler_id = options_.scheduler_id;
options.prefer_ipv6 = prefer_ipv6; options.prefer_ipv6 = prefer_ipv6;
value.query = query.query =
resolve(host, options, resolve(host, options,
PromiseCreator::lambda([actor_id = actor_id(this), host, prefer_ipv6](Result<IPAddress> res) mutable { 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)); 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); auto value_it = cache_[prefer_ipv6].find(host);
CHECK(value_it != cache_[prefer_ipv6].end()); CHECK(value_it != cache_[prefer_ipv6].end());
auto &value = value_it->second; auto &value = value_it->second;
CHECK(!value.promises.empty()); auto query_it = active_queries_[prefer_ipv6].find(host);
CHECK(!value.query.empty()); 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); auto end_time = Time::now() + (res.is_ok() ? options_.ok_timeout : options_.error_timeout);
value = Value{std::move(res), end_time}; value = Value{std::move(res), end_time};
CHECK(value.promises.empty()); active_queries_[prefer_ipv6].erase(query_it);
CHECK(value.query.empty());
for (auto &promise : promises) { for (auto &promise : promises) {
promise.second.set_result(value.get_ip_port(promise.first)); 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); static TD_WARN_UNUSED_RESULT ActorOwn<> resolve(std::string host, ResolveOptions options, Promise<IPAddress> promise);
private: private:
void on_result(std::string host, bool prefer_ipv6, Result<IPAddress> res);
struct Value { struct Value {
Result<IPAddress> ip; Result<IPAddress> ip;
double expire_at; 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) { 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]; 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 } // namespace td