Randomize ping delay for different connections.

This commit is contained in:
levlam 2022-08-28 00:49:56 +03:00
parent 57d7a2b10c
commit 49f8b1e14c
2 changed files with 24 additions and 21 deletions

View File

@ -720,11 +720,13 @@ void SessionConnection::on_read(size_t size) {
}
SessionConnection::SessionConnection(Mode mode, unique_ptr<RawConnection> raw_connection, AuthData *auth_data)
: raw_connection_(std::move(raw_connection)), auth_data_(auth_data) {
: random_delay_(Random::fast(0, 5000000) * 1e-6)
, state_(Init)
, mode_(mode)
, created_at_(Time::now())
, raw_connection_(std::move(raw_connection))
, auth_data_(auth_data) {
CHECK(raw_connection_);
state_ = Init;
mode_ = mode;
created_at_ = Time::now();
}
PollableFdInfo &SessionConnection::get_poll_info() {
@ -969,10 +971,11 @@ void SessionConnection::flush_packet() {
{
// LOG(ERROR) << (auth_data_->get_header().empty() ? '-' : '+');
uint64 parent_message_id = 0;
auto storer = PacketStorer<CryptoImpl>(
queries, auth_data_->get_header(), std::move(to_ack), ping_id, ping_disconnect_delay() + 2, max_delay,
max_after, max_wait, future_salt_n, to_get_state_info, to_resend_answer, to_cancel_answer, destroy_auth_key,
auth_data_, &container_id, &get_state_info_id, &resend_answer_id, &ping_message_id, &parent_message_id);
auto storer = PacketStorer<CryptoImpl>(queries, auth_data_->get_header(), std::move(to_ack), ping_id,
static_cast<int>(ping_disconnect_delay() + 2.0), max_delay, max_after,
max_wait, future_salt_n, to_get_state_info, to_resend_answer,
to_cancel_answer, destroy_auth_key, auth_data_, &container_id,
&get_state_info_id, &resend_answer_id, &ping_message_id, &parent_message_id);
auto quick_ack_token = use_quick_ack ? parent_message_id : 0;
send_crypto(storer, quick_ack_token);

View File

@ -139,32 +139,31 @@ class SessionConnection final
bool is_main_ = false;
bool was_moved_ = false;
int rtt() const {
return max(2, static_cast<int>(raw_connection_->extra().rtt * 1.5 + 1));
double rtt() const {
return max(2.0, raw_connection_->extra().rtt * 1.5 + 1);
}
int32 read_disconnect_delay() const {
return online_flag_ ? rtt() * 7 / 2 : 135;
double read_disconnect_delay() const {
return online_flag_ ? rtt() * 3.5 : 135 + random_delay_;
}
int32 ping_disconnect_delay() const {
return (online_flag_ && is_main_) ? rtt() * 5 / 2 : 135;
double ping_disconnect_delay() const {
return online_flag_ && is_main_ ? rtt() * 2.5 : 135 + random_delay_;
}
int32 ping_may_delay() const {
return online_flag_ ? rtt() / 2 : 30;
double ping_may_delay() const {
return online_flag_ ? rtt() * 0.5 : 30 + random_delay_;
}
int32 ping_must_delay() const {
return online_flag_ ? rtt() : 60;
double ping_must_delay() const {
return online_flag_ ? rtt() : 60 + random_delay_;
}
double http_max_wait() const {
return 25.0; // 25s. Longer could be closed by proxy
}
static constexpr int HTTP_MAX_AFTER = 10; // 0.01s
static constexpr int HTTP_MAX_DELAY = 30; // 0.03s
static constexpr int TEMP_KEY_TIMEOUT = 60 * 60 * 24; // one day
static constexpr int HTTP_MAX_AFTER = 10; // 0.01s
static constexpr int HTTP_MAX_DELAY = 30; // 0.03s
vector<MtprotoQuery> to_send_;
vector<int64> to_ack_;
@ -182,6 +181,7 @@ class SessionConnection final
// nobody cleans up this map. But it should be really small.
FlatHashMap<uint64, vector<uint64>> container_to_service_msg_;
double random_delay_ = 0;
double last_read_at_ = 0;
double last_ping_at_ = 0;
double last_pong_at_ = 0;