Minor GetHostByName improvements.

GitOrigin-RevId: 9ceba9a941e3213a1748d22f38a126ace2d57e56
This commit is contained in:
levlam 2019-02-04 03:12:40 +03:00
parent 83037ee014
commit 2dd4b91d51
2 changed files with 24 additions and 16 deletions

View File

@ -6,10 +6,12 @@
//
#include "td/net/GetHostByNameActor.h"
#include "td/net/SslStream.h"
#include "td/net/Wget.h"
#include "td/utils/JsonBuilder.h"
#include "td/utils/logging.h"
#include "td/utils/Slice.h"
#include "td/utils/Time.h"
namespace td {
@ -139,6 +141,7 @@ class DnsResolver : public Actor {
loop();
}
};
} // namespace detail
ActorOwn<> GetHostByNameActor::resolve(std::string host, ResolveOptions options, Promise<IPAddress> promise) {
@ -158,20 +161,7 @@ ActorOwn<> GetHostByNameActor::resolve(std::string host, ResolveOptions options,
}
}
GetHostByNameActor::GetHostByNameActor(Options options) : options_(options) {
}
void GetHostByNameActor::on_result(std::string host, bool prefer_ipv6, Result<IPAddress> res) {
auto value_it = cache_[prefer_ipv6].find(host);
CHECK(value_it != cache_[prefer_ipv6].end());
auto &value = value_it->second;
auto promises = std::move(value.promises);
auto end_time = Time::now() + (res.is_ok() ? options_.ok_timeout : options_.error_timeout);
value = Value{std::move(res), end_time};
for (auto &promise : promises) {
promise.second.set_result(value.get_ip_port(promise.first));
}
GetHostByNameActor::GetHostByNameActor(Options options) : options_(std::move(options)) {
}
void GetHostByNameActor::run(string host, int port, bool prefer_ipv6, Promise<IPAddress> promise) {
@ -197,4 +187,22 @@ void GetHostByNameActor::run(string host, int port, bool prefer_ipv6, Promise<IP
}
}
void GetHostByNameActor::on_result(std::string host, bool prefer_ipv6, Result<IPAddress> res) {
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 promises = std::move(value.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());
for (auto &promise : promises) {
promise.second.set_result(value.get_ip_port(promise.first));
}
}
} // namespace td

View File

@ -30,7 +30,7 @@ class GetHostByNameActor final : public Actor {
int32 error_timeout{DEFAULT_ERROR_CACHE_TIME};
};
explicit GetHostByNameActor(Options options = {});
explicit GetHostByNameActor(Options options);
void run(std::string host, int port, bool prefer_ipv6, Promise<IPAddress> promise);
@ -52,7 +52,7 @@ class GetHostByNameActor final : public Actor {
Value(Result<IPAddress> ip, double expire_at) : ip(std::move(ip)), expire_at(expire_at) {
}
Result<IPAddress> get_ip_port(int port) {
Result<IPAddress> get_ip_port(int port) const {
auto res = ip.clone();
if (res.is_ok()) {
res.ok_ref().set_port(port);