Skip to content

Http Client Lifetime (ARCH6083)

Key ruby.performance.http-client-lifetime · Severity warning · Category performance

Creating a new HTTP client/connection on every call reuses no connection and exhausts sockets. Keep the client as a shared, long-lived instance.

def fetch(path)
client = Net::HTTP.new("api.example.com", 443)
client.use_ssl = true
client.get(path).body
end
CLIENT = Net::HTTP.start("api.example.com", 443, use_ssl: true)
def fetch(path)
CLIENT.get(path).body
end

Create the HTTP client once and reuse it as a shared/pooled instance.