2018-12-31 20:04:05 +01:00
|
|
|
//
|
2018-01-02 14:42:31 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2018
|
2018-12-31 20:04:05 +01:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#pragma once
|
2018-07-03 21:29:04 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/actor/actor.h"
|
|
|
|
#include "td/actor/PromiseFuture.h"
|
|
|
|
|
|
|
|
#include "td/utils/port/IPAddress.h"
|
|
|
|
#include "td/utils/Status.h"
|
|
|
|
|
|
|
|
#include <unordered_map>
|
|
|
|
|
|
|
|
namespace td {
|
2018-10-15 10:23:51 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
class GetHostByNameActor final : public td::Actor {
|
|
|
|
public:
|
|
|
|
explicit GetHostByNameActor(int32 ok_timeout = CACHE_TIME, int32 error_timeout = ERROR_CACHE_TIME);
|
2018-07-01 03:12:20 +02:00
|
|
|
void run(std::string host, int port, bool prefer_ipv6, td::Promise<td::IPAddress> promise);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
struct Value {
|
|
|
|
Result<td::IPAddress> ip;
|
|
|
|
double expire_at;
|
2018-06-27 18:24:42 +02:00
|
|
|
|
|
|
|
Value(Result<td::IPAddress> ip, double expire_at) : ip(std::move(ip)), expire_at(expire_at) {
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
2018-07-01 03:12:20 +02:00
|
|
|
std::unordered_map<string, Value> cache_[2];
|
2018-12-31 20:04:05 +01:00
|
|
|
static constexpr int32 CACHE_TIME = 60 * 29; // 29 minutes
|
|
|
|
static constexpr int32 ERROR_CACHE_TIME = 60 * 5; // 5 minutes
|
|
|
|
|
|
|
|
int32 ok_timeout_;
|
|
|
|
int32 error_timeout_;
|
2018-07-01 03:12:20 +02:00
|
|
|
|
|
|
|
Result<td::IPAddress> load_ip(string host, int port, bool prefer_ipv6) TD_WARN_UNUSED_RESULT;
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
2018-10-15 10:23:51 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace td
|