Created
May 1, 2019 18:00
-
-
Save jstrong-tios/9263da38d310f6e72996617bf9457313 to your computer and use it in GitHub Desktop.
bare-bones example with concurrent-ruby gem of async io with limiting in flight requests to an arbitrary size
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'json' | |
require 'date' | |
require 'http' | |
require 'concurrent' | |
# I could not get bundle to install the 'concurrent-edge' gem so I just | |
# grabbed a couple classes I needed. code for Concurrent::Throttle is here: | |
# https://github.com/ruby-concurrency/concurrent-ruby/blob/master/lib-edge/concurrent/edge/throttle.rb | |
# | |
# Throttle lso uses Concurrent::LockFreeQueue, which is here: | |
# https://github.com/ruby-concurrency/concurrent-ruby/blob/master/lib-edge/concurrent/edge/lock_free_queue.rb | |
require './lib/throttle' | |
START = Time.now.utc | |
N_ITER = 100 | |
MAX_CONCURRENCY = 10 | |
N_EXECUTING = Concurrent::AtomicFixnum.new 0 | |
N_STARTED = Concurrent::AtomicFixnum.new 0 | |
THROTTLE = Concurrent::Throttle.new MAX_CONCURRENCY | |
def http_request(n) | |
resp = HTTP.get('http://worldclockapi.com/api/json/utc/now') | |
raise StandardError if resp.code != 200 | |
d = JSON.load(resp.body) | |
utc = d['currentDateTime'] | |
puts "#{Time.now.utc - START} #{utc} #{n}" # #{resp.body}" | |
utc | |
end | |
# technique used adapted from Throttle docs example: http://ruby-concurrency.github.io/concurrent-ruby/master/Concurrent/Throttle.html | |
# in original example, it loads value from N_EXECUTING to demonstrate the number of | |
# jobs in flight does not exceed the throttle limit | |
job = -> do | |
N_EXECUTING.increment | |
n = N_STARTED.increment(1) | |
utc = http_request(n) | |
N_EXECUTING.decrement | |
utc | |
end | |
request_results = Array.new(N_ITER) do | |
Thread.new do | |
THROTTLE.acquire(&job) | |
end | |
end.map(&:value) | |
puts "#{request_results}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment