Skip to content

Commit

Permalink
Try to handle global rate limit on getChatAdmins better
Browse files Browse the repository at this point in the history
Signed-off-by: Yan Minari <[email protected]>
  • Loading branch information
yangm97 committed Jan 12, 2019
1 parent dd103d5 commit 8c79fd4
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions lua/groupbutler/utilities.lua
Original file line number Diff line number Diff line change
Expand Up @@ -261,25 +261,28 @@ function _M:cache_adminlist(chat_id)
local api = p(self).api
local red = p(self).red

local lock_key = "cache:chat:"..chat_id..":getadmin_lock"
local global_lock = "bot:getadmin_lock"
local chat_lock = "cache:chat:"..chat_id..":getadmin_lock"
local set = 'cache:chat:'..chat_id..':admins'

if red:exists(lock_key) == 1 then
while red:exists(set) == 0 and red:exists(lock_key) == 1 do
if red:exists(global_lock) == 1
or red:exists(chat_lock) == 1 then
while red:exists(set) == 0
and (red:exists(global_lock) == 1 or red:exists(chat_lock) == 1) do
sleep(0.1)
end
if red:exists(set) == 1 then
return true, 0 -- Another concurrent request has just updated the adminlist
end
end

red:setex(lock_key, 1, "")
red:setex(chat_lock, 5, "")
log.info('Saving the adminlist for: {chat_id}', {chat_id=chat_id})
self:metric_incr("api_getchatadministrators_count")
local ok, err = api:getChatAdministrators(chat_id)
if not ok then
if err.retry_after then
red:setex(lock_key, err.retry_after, "")
red:setex(global_lock, err.retry_after, "")
end
self:metric_incr("api_getchatadministrators_error_count")
return false, err
Expand Down

0 comments on commit 8c79fd4

Please sign in to comment.