Merge pull request #1372 from tleydxdy/patch-1

Add exponential backoff to refresh_channels
This commit is contained in:
Perflyst 2020-10-10 14:01:47 +02:00 committed by GitHub
commit 812a21bce6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -3,15 +3,17 @@ def refresh_channels(db, logger, config)
spawn do spawn do
max_threads = max_channel.receive max_threads = max_channel.receive
lim_threads = max_threads
active_threads = 0 active_threads = 0
active_channel = Channel(Bool).new active_channel = Channel(Bool).new
backoff = 1.seconds
loop do loop do
db.query("SELECT id FROM channels ORDER BY updated") do |rs| db.query("SELECT id FROM channels ORDER BY updated") do |rs|
rs.each do rs.each do
id = rs.read(String) id = rs.read(String)
if active_threads >= max_threads if active_threads >= lim_threads
if active_channel.receive if active_channel.receive
active_threads -= 1 active_threads -= 1
end end
@ -22,12 +24,22 @@ def refresh_channels(db, logger, config)
begin begin
channel = fetch_channel(id, db, config.full_refresh) channel = fetch_channel(id, db, config.full_refresh)
lim_threads = max_threads
db.exec("UPDATE channels SET updated = $1, author = $2, deleted = false WHERE id = $3", Time.utc, channel.author, id) db.exec("UPDATE channels SET updated = $1, author = $2, deleted = false WHERE id = $3", Time.utc, channel.author, id)
rescue ex rescue ex
logger.puts("#{id} : #{ex.message}")
if ex.message == "Deleted or invalid channel" if ex.message == "Deleted or invalid channel"
db.exec("UPDATE channels SET updated = $1, deleted = true WHERE id = $2", Time.utc, id) db.exec("UPDATE channels SET updated = $1, deleted = true WHERE id = $2", Time.utc, id)
else
lim_threads = 1
logger.puts("#{id} : backing off for #{backoff}s")
sleep backoff
if backoff < 1.days
backoff += backoff
else
backoff = 1.days
end
end end
logger.puts("#{id} : #{ex.message}")
end end
active_channel.send(true) active_channel.send(true)