Skip to content

Commit 99aec2c

Browse files
committed
Add custom keep alive timeout seconds
1 parent 86908f7 commit 99aec2c

4 files changed

Lines changed: 23 additions & 4 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ Notes:
4747

4848
- 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.
4949
- 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.
50+
- Idle sockets are closed after 30 seconds by default. Override with `keep_alive_idle_timeout_seconds` to match or stay under your load balancer's idle timeout.
5151
- The option defaults to `false`, so upgrading the gem does not change behaviour until you opt in.
5252

5353
## Compatibility

lib/typesense/api_call.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
module Typesense
77
class ApiCall
88
API_KEY_HEADER_NAME = 'X-TYPESENSE-API-KEY'
9-
KEEP_ALIVE_IDLE_TIMEOUT_SECONDS = 30
109

1110
attr_reader :logger
1211

@@ -21,6 +20,7 @@ def initialize(configuration)
2120
@num_retries_per_request = @configuration.num_retries
2221
@retry_interval_seconds = @configuration.retry_interval_seconds
2322
@keep_alive_connections = @configuration.keep_alive_connections
23+
@keep_alive_idle_timeout_seconds = @configuration.keep_alive_idle_timeout_seconds
2424

2525
@logger = @configuration.logger
2626

@@ -176,7 +176,7 @@ def build_keep_alive_connection(node)
176176
f.options.timeout = @connection_timeout_seconds
177177
f.options.open_timeout = @connection_timeout_seconds
178178
f.adapter :net_http_persistent, pool_size: 1 do |http|
179-
http.idle_timeout = KEEP_ALIVE_IDLE_TIMEOUT_SECONDS
179+
http.idle_timeout = @keep_alive_idle_timeout_seconds
180180
end
181181
end
182182
end

lib/typesense/configuration.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
module Typesense
66
class Configuration
7-
attr_accessor :nodes, :nearest_node, :connection_timeout_seconds, :healthcheck_interval_seconds, :num_retries, :retry_interval_seconds, :api_key, :logger, :log_level, :keep_alive_connections
7+
attr_accessor :nodes, :nearest_node, :connection_timeout_seconds, :healthcheck_interval_seconds, :num_retries, :retry_interval_seconds, :api_key, :logger, :log_level, :keep_alive_connections, :keep_alive_idle_timeout_seconds
88

99
def initialize(options = {})
1010
@nodes = options[:nodes] || []
@@ -15,6 +15,7 @@ def initialize(options = {})
1515
@retry_interval_seconds = options[:retry_interval_seconds] || 0.1
1616
@api_key = options[:api_key]
1717
@keep_alive_connections = options.fetch(:keep_alive_connections, false)
18+
@keep_alive_idle_timeout_seconds = options[:keep_alive_idle_timeout_seconds] || 30
1819

1920
@logger = options[:logger] || Logger.new($stdout)
2021
@log_level = options[:log_level] || Logger::WARN

spec/typesense/api_call_spec.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,24 @@
338338
expect(conn.options.timeout).to eq(keep_alive_typesense.configuration.connection_timeout_seconds)
339339
expect(conn.options.open_timeout).to eq(keep_alive_typesense.configuration.connection_timeout_seconds)
340340
end
341+
342+
it 'defaults the idle timeout to 30 seconds' do
343+
expect(keep_alive_typesense.configuration.keep_alive_idle_timeout_seconds).to eq(30)
344+
end
345+
346+
it 'honours a custom keep_alive_idle_timeout_seconds' do
347+
custom_client = Typesense::Client.new(
348+
api_key: 'abcd',
349+
nodes: typesense.configuration.nodes,
350+
connection_timeout_seconds: 10,
351+
log_level: Logger::ERROR,
352+
keep_alive_connections: true,
353+
keep_alive_idle_timeout_seconds: 5
354+
)
355+
356+
expect(custom_client.configuration.keep_alive_idle_timeout_seconds).to eq(5)
357+
expect(described_class.new(custom_client.configuration).instance_variable_get(:@keep_alive_idle_timeout_seconds)).to eq(5)
358+
end
341359
end
342360

343361
describe 'keep-alive disabled (default)' do

0 commit comments

Comments
 (0)