|
258 | 258 | it_behaves_like 'General error handling', :delete |
259 | 259 | it_behaves_like 'Node selection', :delete |
260 | 260 | end |
| 261 | + |
| 262 | + describe 'keep-alive connection caching' do |
| 263 | + subject(:api_call) { described_class.new(keep_alive_typesense.configuration) } |
| 264 | + |
| 265 | + let(:keep_alive_typesense) do |
| 266 | + Typesense::Client.new( |
| 267 | + api_key: 'abcd', |
| 268 | + nodes: typesense.configuration.nodes, |
| 269 | + connection_timeout_seconds: 10, |
| 270 | + retry_interval_seconds: 0.01, |
| 271 | + log_level: Logger::ERROR, |
| 272 | + keep_alive_connections: true |
| 273 | + ) |
| 274 | + end |
| 275 | + |
| 276 | + let(:node) { keep_alive_typesense.configuration.nodes[0] } |
| 277 | + |
| 278 | + before do |
| 279 | + keep_alive_typesense.configuration.nodes.each do |n| |
| 280 | + stub_request(:any, api_call.send(:uri_for, '/', n)) |
| 281 | + .to_return(status: 200, body: JSON.dump('ok' => true), headers: { 'Content-Type' => 'application/json' }) |
| 282 | + end |
| 283 | + end |
| 284 | + |
| 285 | + it 'reuses the same Faraday connection across calls to the same node on the same thread' do |
| 286 | + first = api_call.send(:connection_for, node) |
| 287 | + second = api_call.send(:connection_for, node) |
| 288 | + |
| 289 | + expect(second).to be(first) |
| 290 | + end |
| 291 | + |
| 292 | + it 'caches connections separately per node' do |
| 293 | + first_node_conn = api_call.send(:connection_for, keep_alive_typesense.configuration.nodes[0]) |
| 294 | + second_node_conn = api_call.send(:connection_for, keep_alive_typesense.configuration.nodes[1]) |
| 295 | + |
| 296 | + expect(second_node_conn).not_to be(first_node_conn) |
| 297 | + end |
| 298 | + |
| 299 | + it 'isolates the cache per thread' do |
| 300 | + main_thread_conn = api_call.send(:connection_for, node) |
| 301 | + |
| 302 | + other_thread_conn = Thread.new { api_call.send(:connection_for, node) }.value |
| 303 | + |
| 304 | + expect(other_thread_conn).not_to be(main_thread_conn) |
| 305 | + end |
| 306 | + |
| 307 | + it 'isolates the cache per ApiCall instance' do |
| 308 | + other_api_call = described_class.new(keep_alive_typesense.configuration) |
| 309 | + |
| 310 | + expect(other_api_call.send(:connection_for, node)) |
| 311 | + .not_to be(api_call.send(:connection_for, node)) |
| 312 | + end |
| 313 | + |
| 314 | + it 'evicts the cached connection when a network error occurs so retries open a fresh socket' do |
| 315 | + timeout_node = keep_alive_typesense.configuration.nodes[0] |
| 316 | + keep_alive_typesense.configuration.nodes.each do |n| |
| 317 | + stub_request(:any, api_call.send(:uri_for, '/', n)).to_timeout |
| 318 | + end |
| 319 | + |
| 320 | + pre_call_conn = api_call.send(:connection_for, timeout_node) |
| 321 | + |
| 322 | + begin |
| 323 | + api_call.get('/') |
| 324 | + rescue StandardError |
| 325 | + # expected: all nodes time out |
| 326 | + end |
| 327 | + |
| 328 | + cache = Thread.current[api_call.instance_variable_get(:@thread_connections_key)] || {} |
| 329 | + expect(cache[api_call.send(:connection_key, timeout_node)]).to be_nil |
| 330 | + |
| 331 | + post_retry_conn = api_call.send(:connection_for, timeout_node) |
| 332 | + expect(post_retry_conn).not_to be(pre_call_conn) |
| 333 | + end |
| 334 | + |
| 335 | + it 'uses the configured timeouts on the cached connection' do |
| 336 | + conn = api_call.send(:connection_for, node) |
| 337 | + |
| 338 | + expect(conn.options.timeout).to eq(keep_alive_typesense.configuration.connection_timeout_seconds) |
| 339 | + expect(conn.options.open_timeout).to eq(keep_alive_typesense.configuration.connection_timeout_seconds) |
| 340 | + 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 |
| 359 | + |
| 360 | + it 'defaults the pool size to 1' do |
| 361 | + expect(keep_alive_typesense.configuration.keep_alive_pool_size).to eq(1) |
| 362 | + end |
| 363 | + |
| 364 | + it 'honours a custom keep_alive_pool_size' do |
| 365 | + custom_client = Typesense::Client.new( |
| 366 | + api_key: 'abcd', |
| 367 | + nodes: typesense.configuration.nodes, |
| 368 | + connection_timeout_seconds: 10, |
| 369 | + log_level: Logger::ERROR, |
| 370 | + keep_alive_connections: true, |
| 371 | + keep_alive_pool_size: 5 |
| 372 | + ) |
| 373 | + |
| 374 | + expect(custom_client.configuration.keep_alive_pool_size).to eq(5) |
| 375 | + expect(described_class.new(custom_client.configuration).instance_variable_get(:@keep_alive_pool_size)).to eq(5) |
| 376 | + end |
| 377 | + end |
| 378 | + |
| 379 | + describe 'keep-alive disabled (default)' do |
| 380 | + it 'is off by default on the configuration' do |
| 381 | + expect(typesense.configuration.keep_alive_connections).to be(false) |
| 382 | + end |
| 383 | + |
| 384 | + it 'builds a fresh Faraday connection per request' do |
| 385 | + stub_request(:any, api_call.send(:uri_for, '/', typesense.configuration.nodes[0])) |
| 386 | + .to_return(status: 200, body: JSON.dump('ok' => true), headers: { 'Content-Type' => 'application/json' }) |
| 387 | + |
| 388 | + api_call.get('/') |
| 389 | + |
| 390 | + expect(Thread.current[api_call.instance_variable_get(:@thread_connections_key)]).to be_nil |
| 391 | + end |
| 392 | + |
| 393 | + it 'raises when keep_alive_idle_timeout_seconds is set without keep_alive_connections' do |
| 394 | + expect do |
| 395 | + Typesense::Client.new( |
| 396 | + api_key: 'abcd', |
| 397 | + nodes: typesense.configuration.nodes, |
| 398 | + log_level: Logger::ERROR, |
| 399 | + keep_alive_idle_timeout_seconds: 5 |
| 400 | + ) |
| 401 | + end.to raise_error(Typesense::Error::MissingConfiguration, /keep_alive_connections: true/) |
| 402 | + end |
| 403 | + |
| 404 | + it 'raises when keep_alive_pool_size is set without keep_alive_connections' do |
| 405 | + expect do |
| 406 | + Typesense::Client.new( |
| 407 | + api_key: 'abcd', |
| 408 | + nodes: typesense.configuration.nodes, |
| 409 | + log_level: Logger::ERROR, |
| 410 | + keep_alive_pool_size: 4 |
| 411 | + ) |
| 412 | + end.to raise_error(Typesense::Error::MissingConfiguration, /keep_alive_connections: true/) |
| 413 | + end |
| 414 | + end |
261 | 415 | end |
0 commit comments