Добавлена обработка отмены контекста в горутине компиляции регулярных выражений для предотвращения блокировок. Оптимизирован алгоритм ожидания в ограничителе частоты, улучшая управление блокировками.
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user