You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat: opt-in HTTP keep-alive via keep_alive_connections
Currently `Typesense::ApiCall#perform_request` builds a fresh
`Faraday.new(...)` (and therefore a new TCP and TLS handshake) on every
request. On hot endpoints this can dominate the Typesense round-trip
latency.
This adds an opt-in `keep_alive_connections` configuration option (default
`false`, so existing users see no behaviour change). When enabled:
* Faraday connections are cached per `(thread, node)` rather than
constructed per request. Net::HTTP is not thread-safe, so per-thread
caching keeps concurrent callers isolated while still respecting the
existing node round-robin.
* Connections use the `:net_http_persistent` Faraday adapter with a 30s
idle timeout, so reused sockets are dropped before most load balancers
cull them.
* On any rescued network error, the cached connection is dropped before
the gem retries, so a half-closed keep-alive socket cannot fail the
retry as well. Pair with `num_retries >= 1` for transparent recovery
from server- or load-balancer-side idle timeouts.
The `:net_http_persistent` adapter and its `net-http-persistent` runtime
dependency are listed in the gemspec, and `require 'faraday/net_http_persistent'`
is gated on the option being enabled, so loading the gem with the option
off does not import the new dependency at runtime.
New RSpec coverage:
* connection reuse on the same thread
* per-node cache keying
* per-thread cache isolation
* per-instance cache isolation
* eviction on network error
* timeouts propagate to the cached connection
* the option defaults to false and the legacy per-request connection
path is preserved
Copy file name to clipboardExpand all lines: README.md
+21Lines changed: 21 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -29,6 +29,27 @@ Here are some examples with inline comments that walk you through how to use the
29
29
30
30
Tests are also a good place to know how the the library works internally: [spec](spec)
31
31
32
+
### Keep-alive connections
33
+
34
+
By default, the client opens a fresh HTTP connection (and TLS handshake) for every request. For high-traffic applications this can dominate request latency. Setting `keep_alive_connections: true` enables persistent connections via the `:net_http_persistent` Faraday adapter:
- Connections are cached per `(thread, node)`. `Net::HTTP` is not thread-safe, so each thread maintains its own keep-alive socket to each Typesense node, and the existing node round-robin still works.
49
+
- A cached connection is dropped automatically when a network error occurs, so retries open a fresh socket. We recommend setting `num_retries` to at least `1` so the gem can recover from a server- or load-balancer-side idle timeout transparently.
50
+
- Idle sockets are closed after 30 seconds; tune your load balancer's idle timeout to match or exceed this.
51
+
- The option defaults to `false`, so upgrading the gem does not change behaviour until you opt in.
0 commit comments