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D1=8F=20=D0=B2=20=D0=BE=D0=B3=D1=80=D0=B0?= =?UTF-8?q?=D0=BD=D0=B8=D1=87=D0=B8=D1=82=D0=B5=D0=BB=D0=B5=20=D1=87=D0=B0?= =?UTF-8?q?=D1=81=D1=82=D0=BE=D1=82=D1=8B,=20=D1=83=D0=BB=D1=83=D1=87?= =?UTF-8?q?=D1=88=D0=B0=D1=8F=20=D1=83=D0=BF=D1=80=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D0=B1=D0=BB=D0=BE=D0=BA=D0=B8=D1=80=D0=BE?= =?UTF-8?q?=D0=B2=D0=BA=D0=B0=D0=BC=D0=B8.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/bot/handlers.go | 6 +++++- internal/bot/helpers.go | 4 ++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/internal/bot/handlers.go b/internal/bot/handlers.go index 0771e21..0adfe10 100644 --- a/internal/bot/handlers.go +++ b/internal/bot/handlers.go @@ -67,7 +67,11 @@ func getCompiledRegex(pattern string) (*regexp.Regexp, error) { go func() { regx, err := regexp.Compile("(?i)" + pattern) - resultCh <- result{regx: regx, err: err} + select { + case resultCh <- result{regx: regx, err: err}: + case <-ctx.Done(): + // Context cancelled, goroutine exits without blocking + } }() select { diff --git a/internal/bot/helpers.go b/internal/bot/helpers.go index 636fd7b..329f71d 100644 --- a/internal/bot/helpers.go +++ b/internal/bot/helpers.go @@ -7,7 +7,6 @@ import ( "strconv" "sync" "time" - "unicode/utf8" "github.com/pyed/transmission" tgbotapi "gopkg.in/telegram-bot-api.v4" @@ -40,7 +39,6 @@ var ( // wait waits for the next available slot using token bucket algorithm func (rl *rateLimiter) wait() { rl.mu.Lock() - defer rl.mu.Unlock() // Refill tokens based on elapsed time now := time.Now() @@ -51,6 +49,7 @@ func (rl *rateLimiter) wait() { // If we have tokens, consume one and return immediately if rl.tokens >= 1.0 { rl.tokens -= 1.0 + rl.mu.Unlock() return } @@ -64,6 +63,7 @@ func (rl *rateLimiter) wait() { rl.mu.Lock() rl.tokens = 0.0 // Consume the token rl.lastRefill = time.Now() + rl.mu.Unlock() } // min returns the minimum of two float64 values