class Enrichment
def call(member)
tasks = [
-> { fetch_github(member) },
-> { fetch_blog(member) },
-> { fetch_forum(member) }
]
threads = tasks.map do |task|
Thread.new do
ActiveRecord::Base.connection_pool.with_connection { task.call }
end
end
threads.map(&:value).compact
ensure
threads&.each { |t| t.kill unless t.stop? }
end
private
def fetch_github(member) = GithubClient.new.get("/users/#{member.nickname}")
def fetch_blog(member) = BlogClient.new.get("/authors/#{member.id}")
def fetch_forum(member) = ForumClient.new.get("/profiles/#{member.id}")
end
If you have to hit multiple APIs, you can cut tail latency by running calls concurrently. Keep it bounded and use timeouts. Rails itself is thread-safe for reads; be careful with DB connections and use with_connection for threaded work.