Skip to content

Instantly share code, notes, and snippets.

@havenwood
Created July 7, 2020 01:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save havenwood/0e5c4e14a338b45e89c51bdac7f7c82d to your computer and use it in GitHub Desktop.
Save havenwood/0e5c4e14a338b45e89c51bdac7f7c82d to your computer and use it in GitHub Desktop.
An example HTTP gem connection pool extracted from some of my code for #ruby IRC
@executor = Concurrent::ThreadPoolExecutor.new min_threads: min_threads,
max_threads: max_threads
def connection_pool
@connection_pool ||= ConnectionPool.new size: @pool_size, timeout: @pool_timeout do
HTTP.auth("Bearer #{access_token}")
.persistent(HOST)
.timeout(@http_timeout)
.headers(accept_encoding: ACCEPT_ENCODING)
.use(:auto_inflate)
end
end
alias connect connection_pool
def disconnect
@connection_pool&.shutdown(&:close)
@connection_pool = nil
end
def future(&block)
Concurrent::Future.execute executor: @executor do
connection_pool.with(&block)
end.value
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment