Created
July 7, 2020 01:17
-
-
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
This file contains hidden or 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
@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