NicoNex / echotron

An elegant and concurrent library for the Telegram bot API in Go.
https://t.me/s/echotronnews
GNU Lesser General Public License v3.0
372 stars 26 forks source link

Caycedo patch 2 #53

Closed Caycedo closed 2 months ago

Caycedo commented 2 months ago

Echotron Rate Limiter Documentation

Default Settings

When initializing the Echotron client, the following default rate limit settings are applied:

  1. Global Rate Limit:

    • 30 requests per second
    • Burst of 10 requests
  2. Per-Chat Rate Limit:

    • 20 requests per minute
    • Burst of 5 requests
  3. Cleanup Interval: 1 minute

  4. Rate Limiting: Enabled by default

Helper Functions

1. SetGlobalRequestLimit

Sets the global rate limit for all requests.

func SetGlobalRequestLimit(rps rate.Limit, burst int)

Example:

// Set global limit to 50 requests per second with a burst of 20
echotron.SetGlobalRequestLimit(rate.Every(20*time.Millisecond), 20)

2. SetChatRequestLimit

Sets the per-chat rate limit and cleanup interval.

func SetChatRequestLimit(rps rate.Limit, burst int, cleanup time.Duration)

Example:

// Set chat limit to 30 requests per minute with a burst of 10, cleanup every 5 minutes
echotron.SetChatRequestLimit(rate.Every(2*time.Second), 10, 5*time.Minute)

3. SetRateLimiterEnabled

Enables or disables the rate limiter.

func SetRateLimiterEnabled(enabled bool)

Example:

// Disable rate limiting
echotron.SetRateLimiterEnabled(false)

// Enable rate limiting
echotron.SetRateLimiterEnabled(true)

Usage Notes

  1. These functions affect the global client instance used by all API calls.
  2. Changes take effect immediately for new requests.
  3. For SetChatRequestLimit, existing chat limiters are updated to the new settings.
  4. When rate limiting is disabled, no limits are applied to requests.
  5. The cleanup interval removes inactive chat limiters to prevent memory leaks.

Best Practices

  1. Set appropriate limits to comply with Telegram's API usage guidelines.
  2. Use per-chat limits to ensure fair usage across different chats.
  3. Adjust the cleanup interval based on your bot's usage patterns.
  4. Monitor your bot's performance and adjust limits as needed.

Remember, excessive API calls may lead to your bot being temporarily or permanently blocked by Telegram. Always use rate limiting responsibly.