lostisland / faraday-net_http_persistent

Faraday Adapter for NetHttpPersistent
MIT License
12 stars 9 forks source link

How do I call Net::HTTP::Persistent.shutdown? #19

Open sudoudaisuke opened 7 months ago

sudoudaisuke commented 7 months ago

Hello. I want to reuse a Net::HTTP connection. However, I want to destroy (close) the connection after one sequence operation.

sample code

conn = Faraday.new do |f|
    f.adapter :net_http_persistent, pool_size: 5 do |http|
        http.idle_timeout = 5
    end
end
conn.get('http://example.com/1')
conn.get('http://example.com/2') # reuse connection
conn.get('http://example.com/3') # reuse connection, end of sequence

conn.close # I want to destroy connection pool

# create new connection pool
conn = Faraday.new do |f|
    f.adapter :net_http_persistent, pool_size: 5 do |http|
        http.idle_timeout = 5
    end
end
conn.get('http://example.com/1')
conn.get('http://example.com/2') # reuse connection
conn.get('http://example.com/3') # reuse connection, end of sequence

conn.close # I want to destroy connection pool

I think the conn.close method is just calling Faraday::Adapter::NetHttpPersistent.close. And Faraday::Adapter::NetHttpPersistent.close is not implemented.

I think calling @cached_connection.shutdown is destory connections, Like this.

conn.app.instance_variable_get(:@cached_connection).shutdown

Is there an easier way to destory a connection? Or is it a process that does not require destroying the connection?

olleolleolle commented 2 months ago

@sudoudaisuke Thank you for asking, I made the smallest implementation of #23, would you try that and see if it fits your needs?