From 382f072664a0a402625b9111d240e4f3b557f6db Mon Sep 17 00:00:00 2001 From: Alexander Kutsan Date: Sun, 28 Apr 2024 11:48:49 +0400 Subject: [PATCH 1/4] Add rate limiting accordint to telegram bot api limits --- shard.yml | 4 ++++ src/tourmaline/client.cr | 7 ++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/shard.yml b/shard.yml index fd1d2e69c..f7c8fe1d2 100644 --- a/shard.yml +++ b/shard.yml @@ -16,6 +16,9 @@ dependencies: http_proxy: github: mamantoha/http_proxy branch: master + rate_limiter: + github: lbarasti/rate_limiter + version: ~> v1.0.1 development_dependencies: ngrok: @@ -29,4 +32,5 @@ development_dependencies: github: watzon/code_writer branch: main + license: MIT diff --git a/src/tourmaline/client.cr b/src/tourmaline/client.cr index f063ba0bc..3e4bcb128 100644 --- a/src/tourmaline/client.cr +++ b/src/tourmaline/client.cr @@ -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`. @@ -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}") @@ -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.get Log.debug { "sending ►► #{path.split("/").last}(#{params.to_pretty_json})" } begin From b49d4a616acbe4590e0ea3b5ff85e1af0c138210 Mon Sep 17 00:00:00 2001 From: Alexander Kutsan Date: Sun, 28 Apr 2024 11:52:35 +0400 Subject: [PATCH 2/4] Update client.cr --- src/tourmaline/client.cr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tourmaline/client.cr b/src/tourmaline/client.cr index 3e4bcb128..a2a5ac4c9 100644 --- a/src/tourmaline/client.cr +++ b/src/tourmaline/client.cr @@ -180,7 +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.get + @rate_limiter.try &.get Log.debug { "sending ►► #{path.split("/").last}(#{params.to_pretty_json})" } begin From 1648db0426dc4560316e8472f34ae4c15b657b23 Mon Sep 17 00:00:00 2001 From: Alexander Kutsan Date: Sun, 28 Apr 2024 11:56:13 +0400 Subject: [PATCH 3/4] Update shard.yml --- shard.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shard.yml b/shard.yml index f7c8fe1d2..8e7c3f990 100644 --- a/shard.yml +++ b/shard.yml @@ -18,7 +18,7 @@ dependencies: branch: master rate_limiter: github: lbarasti/rate_limiter - version: ~> v1.0.1 + version: ~> 1.0.1 development_dependencies: ngrok: From 115457f2609b6a83723750b93c682e853bec41eb Mon Sep 17 00:00:00 2001 From: Alexander Kutsan Date: Sun, 28 Apr 2024 11:56:31 +0400 Subject: [PATCH 4/4] Update shard.yml --- shard.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/shard.yml b/shard.yml index 8e7c3f990..1cb0dc918 100644 --- a/shard.yml +++ b/shard.yml @@ -32,5 +32,4 @@ development_dependencies: github: watzon/code_writer branch: main - license: MIT