Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rate limiting according to telegram bot api limits #82

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions shard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ dependencies:
http_proxy:
github: mamantoha/http_proxy
branch: master
rate_limiter:
github: lbarasti/rate_limiter
version: ~> 1.0.1

development_dependencies:
ngrok:
Expand Down
7 changes: 6 additions & 1 deletion src/tourmaline/client.cr
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ module Tourmaline

@dispatcher : Dispatcher?

@rate_limiter : RateLimiter::Limiter?

private getter pool : DB::Pool(HTTP::Client)

# Create a new instance of `Tourmaline::Client`.
Expand Down Expand Up @@ -92,7 +94,9 @@ module Tourmaline
proxy_host = nil,
proxy_port = nil,
proxy_user = nil,
proxy_pass = nil)
proxy_pass = nil,
@rate_limiter = RateLimiter.new(interval: 1.second / 30)
)
if !proxy
if proxy_uri
proxy_uri = proxy_uri.is_a?(URI) ? proxy_uri : URI.parse(proxy_uri.starts_with?("http") ? proxy_uri : "http://#{proxy_uri}")
Expand Down Expand Up @@ -176,6 +180,7 @@ module Tourmaline
def request_internal(path, params = {} of String => String, multipart = false)
# Wrap this so pool can attempt a retry
using_connection do |client|
@rate_limiter.try &.get
Log.debug { "sending ►► #{path.split("/").last}(#{params.to_pretty_json})" }

begin
Expand Down