quick connection pool example
require "thread" | |
require "connection_pool" | |
require "pp" | |
NUM_CLIENTS = 9 | |
MAX_AT_A_TIME = 3 | |
TIMEOUT = 10 | |
JOB_TIME = 3 | |
# we'll let people lease access to zero | |
pool = ConnectionPool.new(size: MAX_AT_A_TIME, timeout: TIMEOUT) { 0 } | |
res = NUM_CLIENTS.times.map do |n| | |
Thread.new do | |
begin | |
start = Time.now | |
# let's checkout that zero and have some fun with it! | |
pool.with do |zero| | |
zero * n | |
zero + n | |
zero - n | |
zero / (n.zero? ? n.succ : n) | |
sleep(JOB_TIME) | |
end | |
"Completed in #{(Time.now - start).round} seconds" | |
rescue Timeout::Error | |
:timed_out! | |
end | |
end | |
end | |
pp res.map(&:value) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment