Commit 225c71e
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.)1 parent 6f49e2b commit 225c71e
File tree
3 files changed
+35
-20
lines changed- lib/slack/web/faraday
- spec/slack/web
3 files changed
+35
-20
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
| 3 | + | |
3 | 4 | | |
4 | 5 | | |
5 | 6 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
9 | | - | |
10 | | - | |
11 | | - | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
12 | 14 | | |
13 | | - | |
14 | | - | |
15 | | - | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
16 | 18 | | |
17 | | - | |
18 | | - | |
19 | | - | |
20 | | - | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
21 | 23 | | |
22 | | - | |
23 | | - | |
24 | | - | |
25 | | - | |
26 | | - | |
27 | | - | |
28 | | - | |
29 | | - | |
30 | | - | |
31 | | - | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
32 | 35 | | |
33 | 36 | | |
34 | 37 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
205 | 205 | | |
206 | 206 | | |
207 | 207 | | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
208 | 219 | | |
209 | 220 | | |
0 commit comments