diff --git a/td/mtproto/SessionConnection.cpp b/td/mtproto/SessionConnection.cpp index eca0e9845..f81c47a00 100644 --- a/td/mtproto/SessionConnection.cpp +++ b/td/mtproto/SessionConnection.cpp @@ -19,6 +19,7 @@ #include "td/utils/format.h" #include "td/utils/Gzip.h" #include "td/utils/logging.h" +#include "td/utils/misc.h" #include "td/utils/Random.h" #include "td/utils/ScopeGuard.h" #include "td/utils/Time.h" @@ -863,7 +864,7 @@ void SessionConnection::flush_packet() { max_after = HTTP_MAX_AFTER; auto time_to_disconnect = min(ping_disconnect_delay() + last_pong_at_, read_disconnect_delay() + last_read_at_) - Time::now_cached(); - max_wait = min(http_max_wait(), static_cast(1000 * max(0.1, time_to_disconnect - rtt()))); + max_wait = static_cast(1000 * clamp(time_to_disconnect - rtt(), 0.1, http_max_wait())); } else if (mode_ == Mode::Http) { max_delay = HTTP_MAX_DELAY; max_after = HTTP_MAX_AFTER; diff --git a/td/mtproto/SessionConnection.h b/td/mtproto/SessionConnection.h index ca9e1fb16..d13f866ed 100644 --- a/td/mtproto/SessionConnection.h +++ b/td/mtproto/SessionConnection.h @@ -151,8 +151,8 @@ class SessionConnection return online_flag_ ? rtt() : 60; } - int http_max_wait() const { - return 25 * 1000; // 25s. Longer could be closed by proxy + 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 diff --git a/td/telegram/Client.cpp b/td/telegram/Client.cpp index fc514fe86..74be58155 100644 --- a/td/telegram/Client.cpp +++ b/td/telegram/Client.cpp @@ -277,7 +277,7 @@ class TdReceiver { if (is_locked) { LOG(FATAL) << "Receive is called after Client destroy, or simultaneously from different threads"; } - auto response = receive_unlocked(timeout); + auto response = receive_unlocked(clamp(timeout, 0.0, 1000000.0)); is_locked = receive_lock_.exchange(false); CHECK(is_locked); VLOG(td_requests) << "End to wait for updates, returning object " << response.request_id << ' ' diff --git a/td/telegram/NotificationManager.cpp b/td/telegram/NotificationManager.cpp index f2393b91f..6c79a6a95 100644 --- a/td/telegram/NotificationManager.cpp +++ b/td/telegram/NotificationManager.cpp @@ -845,7 +845,8 @@ int32 NotificationManager::get_notification_delay_ms(DialogId dialog_id, const P return 0; }(); - auto passed_time_ms = max(0, static_cast((G()->server_time_cached() - notification.date - 1) * 1000)); + auto passed_time_ms = + static_cast(clamp(G()->server_time_cached() - notification.date - 1, 0.0, 1000000.0) * 1000); return max(max(min_delay_ms, delay_ms) - passed_time_ms, MIN_NOTIFICATION_DELAY_MS); } diff --git a/tdactor/td/actor/impl/Scheduler.cpp b/tdactor/td/actor/impl/Scheduler.cpp index 7ed44c1f6..c5c5c88a5 100644 --- a/tdactor/td/actor/impl/Scheduler.cpp +++ b/tdactor/td/actor/impl/Scheduler.cpp @@ -17,6 +17,7 @@ #include "td/utils/format.h" #include "td/utils/List.h" #include "td/utils/logging.h" +#include "td/utils/misc.h" #include "td/utils/ObjectPool.h" #include "td/utils/port/thread_local.h" #include "td/utils/ScopeGuard.h" @@ -428,7 +429,7 @@ void Scheduler::set_actor_timeout_at(ActorInfo *actor_info, double timeout_at) { void Scheduler::run_poll(Timestamp timeout) { // we can't wait for less than 1ms - int timeout_ms = static_cast(td::max(timeout.in(), 0.0) * 1000 + 1); + int timeout_ms = static_cast(clamp(timeout.in(), 0.0, 1000000.0) * 1000 + 1); #if TD_PORT_WINDOWS CHECK(inbound_queue_); inbound_queue_->reader_get_event_fd().wait(timeout_ms); diff --git a/tdutils/td/utils/port/detail/skip_eintr.h b/tdutils/td/utils/port/detail/skip_eintr.h index 5afa24159..e501269c7 100644 --- a/tdutils/td/utils/port/detail/skip_eintr.h +++ b/tdutils/td/utils/port/detail/skip_eintr.h @@ -53,7 +53,7 @@ auto skip_eintr_timeout(F &&f, int32 timeout_ms) { break; } left_timeout_ms = - td::max(static_cast((start.at() - Timestamp::now().at()) * 1000 + timeout_ms + 1 - 1e-9), 0); + static_cast(td::max((start.at() - Timestamp::now().at()) * 1000 + timeout_ms + 1 - 1e-9, 0.0)); } return res; }