Skip to content

Commit 8a919b5

Browse files
committed
Update
Fixed entirely ignores the CTRL+C kill signal.
1 parent 544632a commit 8a919b5

2 files changed

Lines changed: 8 additions & 4 deletions

File tree

source/telegrambot.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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) {
386389
bool 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},

source/telegrambot.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ class RateLimiter {
120120
class OutboundRateLimiter {
121121
public:
122122
OutboundRateLimiter() = default;
123-
void throttle(const std::string& chatId);
123+
void throttle(const std::string& chatId, volatile std::sig_atomic_t* shutdownPtr = nullptr);
124124

125125
private:
126126
std::mutex m_mutex;

0 commit comments

Comments
 (0)