Skip to content

Commit 1e557fa

Browse files
authored
Merge pull request #56 from GetStream/feature/http-keepalive-connection-reuse
fix: add configurable keep-alive and Faraday adapter options
2 parents 74f4e27 + af3efbd commit 1e557fa

5 files changed

Lines changed: 82 additions & 13 deletions

File tree

‎README.md‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,23 @@ require 'getstream_ruby'
3636
client = GetStreamRuby.manual(
3737
api_key: "your_api_key",
3838
api_secret: "your_api_secret",
39+
# Optional HTTP tuning for keep-alive / connection reuse
40+
connection_keep_alive: true,
41+
# Optional: bring your own Faraday adapter (default is Faraday.default_adapter)
42+
faraday_adapter: :net_http,
43+
faraday_adapter_options: {
44+
# adapter-specific options
45+
}
3946
)
4047
```
4148

49+
You can also set these via environment variables:
50+
51+
```bash
52+
STREAM_CONNECTION_KEEP_ALIVE=true
53+
STREAM_FARADAY_ADAPTER=net_http
54+
```
55+
4256
### Method 2: .env File
4357

4458
Create a `.env` file in your project root:

‎lib/getstream_ruby.rb‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,11 @@ module GetStreamRuby
1212
class << self
1313

1414
# Method 1: Manual configuration (highest priority)
15-
def manual(api_key:, api_secret:, base_url: nil, timeout: nil)
15+
def manual(api_key:, api_secret:, **options)
1616
config = Configuration.manual(
1717
api_key: api_key,
1818
api_secret: api_secret,
19-
base_url: base_url,
20-
timeout: timeout,
19+
**options,
2120
)
2221
Client.new(config)
2322
end

‎lib/getstream_ruby/client.rb‎

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,15 @@ class Client
2121

2222
attr_reader :configuration
2323

24-
def initialize(config = nil, api_key: nil, api_secret: nil, base_url: nil, timeout: nil)
24+
def initialize(config = nil, api_key: nil, api_secret: nil, **options)
2525
@configuration = config || GetStreamRuby.configuration
2626

2727
# Create new configuration with overrides if any parameters provided
28-
if api_key || api_secret || base_url || timeout
28+
if api_key || api_secret || !options.empty?
2929
@configuration = Configuration.with_overrides(
3030
api_key: api_key,
3131
api_secret: api_secret,
32-
base_url: base_url,
33-
timeout: timeout,
32+
**options,
3433
)
3534
end
3635

@@ -131,12 +130,24 @@ def build_connection
131130
backoff_factor: 2,
132131
}
133132
conn.response :json, content_type: /\bjson$/
134-
conn.adapter Faraday.default_adapter
133+
conn.headers['Connection'] = 'keep-alive' if @configuration.connection_keep_alive
134+
configure_adapter(conn)
135135
conn.options.timeout = @configuration.timeout
136136

137137
end
138138
end
139139

140+
def configure_adapter(connection)
141+
adapter = @configuration.faraday_adapter || Faraday.default_adapter
142+
adapter_options = @configuration.faraday_adapter_options || {}
143+
connection.adapter(adapter, **adapter_options)
144+
rescue Faraday::Error, ArgumentError => e
145+
@configuration.logger&.warn(
146+
"Falling back to #{Faraday.default_adapter}: could not use adapter #{adapter} (#{e.message})",
147+
)
148+
connection.adapter Faraday.default_adapter
149+
end
150+
140151
def generate_auth_header
141152
JWT.encode(
142153
{

‎lib/getstream_ruby/configuration.rb‎

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,21 @@ module GetStreamRuby
44

55
class Configuration
66

7-
attr_accessor :api_key, :api_secret, :base_url, :timeout, :logger
7+
attr_accessor :api_key, :api_secret, :base_url, :timeout, :logger, :faraday_adapter, :faraday_adapter_options,
8+
:connection_keep_alive
9+
10+
def initialize(api_key: nil, api_secret: nil, use_env: true, **options)
11+
base_url = options[:base_url]
12+
timeout = options[:timeout]
13+
http_options = options[:http_options] || {}
14+
faraday_adapter = options[:faraday_adapter] || http_options[:faraday_adapter]
15+
faraday_adapter_options = options[:faraday_adapter_options] || http_options[:faraday_adapter_options]
16+
connection_keep_alive = if options.key?(:connection_keep_alive)
17+
options[:connection_keep_alive]
18+
else
19+
http_options[:connection_keep_alive]
20+
end
821

9-
def initialize(api_key: nil, api_secret: nil, base_url: nil, timeout: nil, use_env: true)
1022
if use_env
1123
@api_key = api_key || ENV.fetch('STREAM_API_KEY', nil)
1224
@api_secret = api_secret || ENV.fetch('STREAM_API_SECRET', nil)
@@ -20,6 +32,13 @@ def initialize(api_key: nil, api_secret: nil, base_url: nil, timeout: nil, use_e
2032
@timeout = timeout || 30
2133
end
2234

35+
@faraday_adapter = (faraday_adapter || ENV.fetch('STREAM_FARADAY_ADAPTER', nil))&.to_sym
36+
@faraday_adapter_options = faraday_adapter_options || default_adapter_options
37+
@connection_keep_alive = if connection_keep_alive.nil?
38+
ENV.fetch('STREAM_CONNECTION_KEEP_ALIVE', 'true') == 'true'
39+
else
40+
connection_keep_alive
41+
end
2342
@logger = nil
2443
end
2544

@@ -38,6 +57,9 @@ def dup
3857
api_secret: @api_secret,
3958
base_url: @base_url,
4059
timeout: @timeout,
60+
faraday_adapter: @faraday_adapter,
61+
faraday_adapter_options: @faraday_adapter_options.dup,
62+
connection_keep_alive: @connection_keep_alive,
4163
)
4264
end
4365

@@ -47,9 +69,8 @@ def self.with_overrides(overrides = {})
4769
end
4870

4971
# Method 1: Manual configuration (no environment variables)
50-
def self.manual(api_key:, api_secret:, base_url: nil, timeout: nil)
51-
new(api_key: api_key, api_secret: api_secret,
52-
base_url: base_url, timeout: timeout, use_env: false)
72+
def self.manual(api_key:, api_secret:, **options)
73+
new(api_key: api_key, api_secret: api_secret, use_env: false, **options)
5374
end
5475

5576
# Method 2: .env file (loads .env file via dotenv gem, falls back to env vars)
@@ -63,6 +84,12 @@ def self.from_system_env
6384
new(use_env: true)
6485
end
6586

87+
private
88+
89+
def default_adapter_options
90+
{}
91+
end
92+
6693
end
6794

6895
end

‎spec/getstream_ruby_spec.rb‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,24 @@
2323
expect(client).to be_a(GetStreamRuby::Client)
2424
expect(client.configuration.api_key).to eq('manual_key')
2525
expect(client.configuration.api_secret).to eq('manual_secret')
26+
expect(client.configuration.faraday_adapter).to be_nil
27+
expect(client.configuration.faraday_adapter_options).to eq({})
28+
expect(client.configuration.connection_keep_alive).to eq(true)
29+
30+
end
31+
32+
it 'creates a client with custom faraday adapter settings' do
33+
34+
client = GetStreamRuby.manual(
35+
api_key: 'manual_key',
36+
api_secret: 'manual_secret',
37+
faraday_adapter: :net_http,
38+
faraday_adapter_options: {},
39+
connection_keep_alive: false,
40+
)
41+
expect(client.configuration.faraday_adapter).to eq(:net_http)
42+
expect(client.configuration.faraday_adapter_options).to eq({})
43+
expect(client.configuration.connection_keep_alive).to eq(false)
2644

2745
end
2846

0 commit comments

Comments
 (0)