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.
Incorrect
Section titled “Incorrect”def fetch(path) client = Net::HTTP.new("api.example.com", 443) client.use_ssl = true client.get(path).bodyendCorrect
Section titled “Correct”CLIENT = Net::HTTP.start("api.example.com", 443, use_ssl: true)
def fetch(path) CLIENT.get(path).bodyendCreate the HTTP client once and reuse it as a shared/pooled instance.