Skip to content

Instantly share code, notes, and snippets.

@MaryamAdnan3
Last active September 1, 2023 06:45
Show Gist options
  • Save MaryamAdnan3/7c08b0cda0b5e8486fe2079911da094f to your computer and use it in GitHub Desktop.
Save MaryamAdnan3/7c08b0cda0b5e8486fe2079911da094f to your computer and use it in GitHub Desktop.
connection pooling
require 'faraday'
class ConnectionPool
# See https://github.com/mperham/connection_pool
def initialize(pool_size: 8, pool_timeout: 5, **connection_config)
@connections = ConnectionPool.new(size: pool_size, timeout: pool_timeout) do
create_connection(**connection_config)
end
end
def connection(&block)
@connections.with(&block)
end
def create_connection(timeout:, max_retries:, retry_interval:,
backoff_factor:, retry_statuses:, retry_methods:,
cache: false, verify: true)
Faraday.new do |faraday|
faraday.use Faraday::HttpCache, serializer: Marshal if cache
faraday.use FaradayMiddleware::FollowRedirects
faraday.use :gzip
faraday.request :multipart
faraday.request :url_encoded
faraday.ssl[:ca_file] = Certifi.where
faraday.ssl[:verify] = verify
faraday.request :retry, max: max_retries, interval: retry_interval,
backoff_factor: backoff_factor,
retry_statuses: retry_statuses,
methods: retry_methods
faraday.adapter Faraday.default_adapter
faraday.options[:params_encoder] = Faraday::FlatParamsEncoder
faraday.options[:timeout] = timeout if timeout.positive?
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment