# HG changeset patch # User nanaya # Date 1622050098 -32400 # Node ID 17461f7682a233f09eef60385a160664a0fd7029 # Parent c5a101bc4a098376a7b34181afcab51045c02591 One client for everyone diff -r c5a101bc4a09 -r 17461f7682a2 app/lib/clients.rb --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/app/lib/clients.rb Thu May 27 02:28:18 2021 +0900 @@ -0,0 +1,23 @@ +class Clients + def self.client_options(id) + { + :timeouts => { + :connect => 5, + :read => 5, + :write => 5, + }, + }.merge $cfg[:twitter][id] + end + + def self.instance + @@instance ||= self.new + end + + def initialize + @clients = {} + end + + def get(id) + @clients[id] ||= Twitter::REST::Client.new(self.class.client_options id) + end +end diff -r c5a101bc4a09 -r 17461f7682a2 app/models/tweet.rb --- a/app/models/tweet.rb Thu May 27 01:53:53 2021 +0900 +++ b/app/models/tweet.rb Thu May 27 02:28:18 2021 +0900 @@ -79,7 +79,7 @@ end def client - @clients[client_config_id] ||= Twitter::REST::Client.new(self.class.client_options client_config_id) + Clients.instance.get client_config_id end def client_try(method, *args) @@ -88,7 +88,7 @@ begin data = client.public_send method, *args rescue Twitter::Error::TooManyRequests - @client_config_id += 1 + @client_config_id = (1 + @client_config_id) % @client_config_count if initial_config_id == client_config_id raise @@ -106,6 +106,6 @@ @client_config_count ||= $cfg[:twitter].size @client_config_id ||= rand(@client_config_count) - @client_config_id %= @client_config_count + @client_config_id end end