add rate limiter to telegram
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
// yamusic-bot/cmd/bot/main.go
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
@@ -15,6 +17,7 @@ import (
|
||||
"gitea.mrixs.me/Mrixs/yamusic-bot/pkg/tagger"
|
||||
"gitea.mrixs.me/Mrixs/yamusic-bot/pkg/yamusic"
|
||||
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||||
"golang.org/x/time/rate"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -54,11 +57,23 @@ func main() {
|
||||
// 4. Инициализация компонентов
|
||||
downloaderComponent := downloader.NewHTTPDownloader()
|
||||
taggerComponent := tagger.NewID3Tagger()
|
||||
telegramClient := bot.NewTelegramClientAdapter(tgAPI, cfg.TelegramCacheChatID)
|
||||
|
||||
// Создаем БЫСТРЫЙ лимитер для общих вызовов API (в секунду)
|
||||
fastLimiter := rate.NewLimiter(rate.Limit(cfg.TelegramAPIRateLimit), cfg.TelegramAPIRateLimit)
|
||||
slog.Info("General Telegram API rate limit set", "requests_per_second", cfg.TelegramAPIRateLimit)
|
||||
|
||||
// Создаем МЕДЛЕННЫЙ лимитер для кэш-канала (в минуту)
|
||||
// rate.Limit измеряется в событиях/секунду, поэтому конвертируем
|
||||
// Burst size = 1, чтобы избежать отправки пачки сообщений и долгого ожидания
|
||||
cacheRPS := float64(cfg.TelegramCacheRateLimitPerMinute) / 60.0
|
||||
cacheLimiter := rate.NewLimiter(rate.Limit(cacheRPS), 1)
|
||||
slog.Info("Cache channel Telegram API rate limit set", "requests_per_minute", cfg.TelegramCacheRateLimitPerMinute)
|
||||
|
||||
// Передаем оба лимитера в адаптер
|
||||
telegramClient := bot.NewTelegramClientAdapter(tgAPI, cfg.TelegramCacheChatID, fastLimiter, cacheLimiter)
|
||||
|
||||
trackProcessor := processor.NewTrackProcessor(db, yandexClient, downloaderComponent, taggerComponent, telegramClient)
|
||||
|
||||
// Передаем taggerComponent в admin.NewHandler
|
||||
adminHandler := admin.NewHandler(db, telegramClient, yandexClient, taggerComponent, startTime)
|
||||
inlineHandler := bot.NewInlineHandler(yandexClient, trackProcessor, telegramClient)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user