@@ -66,7 +66,7 @@ bool RateLimiter::allowRequest(const std::string& userId) {
6666}
6767
6868// ─── OutboundRateLimiter ─────────────────────────────────────────────────────
69- void OutboundRateLimiter::throttle (const std::string& chatId) {
69+ void OutboundRateLimiter::throttle (const std::string& chatId, volatile std:: sig_atomic_t * shutdownPtr ) {
7070 using namespace std ::chrono;
7171 while (true ) {
7272 std::unique_lock<std::mutex> lock (m_mutex);
@@ -101,7 +101,10 @@ void OutboundRateLimiter::throttle(const std::string& chatId) {
101101 globalOk ? now : wakeGlobal,
102102 perChatOk ? now : wakePerChat});
103103 lock.unlock ();
104- std::this_thread::sleep_until (wake);
104+ while (steady_clock::now () < wake) {
105+ if (shutdownPtr && *shutdownPtr) return ;
106+ std::this_thread::sleep_for (milliseconds (50 ));
107+ }
105108 }
106109}
107110
@@ -386,7 +389,8 @@ void TelegramBot::startWebhook(volatile std::sig_atomic_t& shutdown) {
386389bool TelegramBot::sendMessage (const std::string& chatId, const std::string& text,
387390 bool useMarkdown, std::optional<Json::Value> replyMarkup) {
388391 // Block if we are about to exceed Telegram API rate limits
389- m_outboundLimiter.throttle (chatId);
392+ m_outboundLimiter.throttle (chatId, m_shutdownPtr);
393+ if (m_shutdownPtr && *m_shutdownPtr) return false ;
390394
391395 std::map<std::string, std::string> params = {
392396 {" chat_id" , chatId},
0 commit comments