Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cache Faraday::Connection for persistent adapters
In order to be a good web citizen and to allow maximum performance HTTP clients should use persistent connections to make requests. This has multiple benefits: * Users won't be delayed by TCP slow-start figuring out the window size for every HTTP request * Users won't be delayed by TLS handshakes for each new request * Bandwidth overhead per request is reduced through large TCP windows on established TLS connections. Some Slack endpoints are paginated and may take many requests to fetch all necessary data. Using persistent connections can result in a 4x speed boost. Slack::Web::Client uses Faraday which uses Net::HTTP by default. While Net::HTTP is capable of persistent connections Net::HTTP + Faraday is not configured this way. The Faraday::Adapter::NetHttpPersistent adapter does. Slack::Web::Client uses Faraday#default_adapter so the end user can switch adapters are used like this: Faraday.default_adapter = Faraday::Adapter::NetHttpPersistent c = Slack::Web::Client.new … Unfortunately Slack::Web::Client does not cache the Faraday::Connection object and instead creates a new connection for every request. This breaks the ability to use persistent connections with Slack::Web::Client. This can be observed through using Wireshark with an `ssl.handshake` filter, or by observing `SYN` packets sent by `tcpdump host slack.com`. This patch adds caching of the Faraday::Connection object to Slack::Web::Client to allow caching of connections through Faraday. A cached connection can be reused as a persistent connection. Recommending users use a persistent connection adapter (like Faraday::Adapter::NetHttpPersistent) is not part of this pull request, but I do recommend it. Your users should see an easy speed improvement. Here is a `time` output from a script that fetches ~4,000 conversations using the net-http-persistent adapter without this patch (making it ineffective): $ time ruby t.rb ruby t.rb 2.46s user 0.25s system 14% cpu 18.389 total and with this patch: $ time ruby t.rb ruby t.rb 0.99s user 0.20s system 13% cpu 9.053 total (This is only a 2x speed boost as the slack `conversation.list` API is highly variable in response time.)
- Loading branch information