Clamp float numbers before conversion to integers.

This commit is contained in:
levlam 2021-03-28 22:33:22 +03:00
parent fbc7e5a7f8
commit 4df03c8491
6 changed files with 10 additions and 7 deletions

View File

@ -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<int>(1000 * max(0.1, time_to_disconnect - rtt())));
max_wait = static_cast<int>(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;

View File

@ -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

View File

@ -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 << ' '

View File

@ -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<int32>((G()->server_time_cached() - notification.date - 1) * 1000));
auto passed_time_ms =
static_cast<int32>(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);
}

View File

@ -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<int32>(td::max(timeout.in(), 0.0) * 1000 + 1);
int timeout_ms = static_cast<int32>(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);

View File

@ -53,7 +53,7 @@ auto skip_eintr_timeout(F &&f, int32 timeout_ms) {
break;
}
left_timeout_ms =
td::max(static_cast<int32>((start.at() - Timestamp::now().at()) * 1000 + timeout_ms + 1 - 1e-9), 0);
static_cast<int32>(td::max((start.at() - Timestamp::now().at()) * 1000 + timeout_ms + 1 - 1e-9, 0.0));
}
return res;
}