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 keep-alive connections via keep_alive_connections
The gem currently builds a fresh `Faraday.new` (and therefore a new TCP +
TLS handshake) for every request in `ApiCall#perform_request`. On hot
endpoints this dominates request latency.
This adds an opt-in `keep_alive_connections` config flag (default `false`,
so existing users see no behaviour change). When enabled:
- Faraday connections are cached per `(thread, node)`. Net::HTTP is not
thread-safe, so per-thread caching keeps each Puma/Sidekiq worker
isolated while still respecting the existing node round-robin.
- Connections use the `:net_http_persistent` Faraday adapter with a 30s
idle timeout.
- 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 load-balancer idle timeouts.)
Adds RSpec coverage for connection reuse, per-node keying, per-thread
isolation, per-instance isolation, eviction on error, and verifies the
default-off behaviour preserves the existing per-request connection path.
Prompt: ask Claude to investigate Sentry profile of the v3 articles
search endpoint (Olio API), implement an opt-in keep-alive option in
this gem fork, gate behind config so the version bump itself is safe.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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 Puma/Sidekiq 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