Class: TcpUserTimeout::Rack::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/tcp_user_timeout/rack.rb

Overview

Rack middleware that scopes TCP_USER_TIMEOUT to the duration of each request. Only sockets *opened during the request* get the deadline —pre-existing pooled connections (DB, persistent HTTP) created at app boot are untouched, so production pools that should outlive any single request keep their original behavior.

use TcpUserTimeout::Rack::Middleware, seconds: 25

# Or, derive the bound from each request (e.g. from a header):
use TcpUserTimeout::Rack::Middleware, seconds: ->(env) {
  env["HTTP_X_REQUEST_TIMEOUT_S"]&.to_f || 25
}

Recommended setup: pick a value slightly under the web server’s worker-kill timeout (Puma’s ‘worker_timeout`, Heroku’s 30s router cap, etc.) so the kernel kills wedged sockets before the worker is SIGKILL’d. Defaults to off — you must opt in.

Instance Method Summary collapse

Constructor Details

#initialize(app, seconds:) ⇒ Middleware

Returns a new instance of Middleware.



25
26
27
28
# File 'lib/tcp_user_timeout/rack.rb', line 25

def initialize(app, seconds:)
  @app = app
  @seconds = seconds
end

Instance Method Details

#call(env) ⇒ Object



30
31
32
33
34
35
# File 'lib/tcp_user_timeout/rack.rb', line 30

def call(env)
  seconds = @seconds.respond_to?(:call) ? @seconds.call(env) : @seconds
  return @app.call(env) unless seconds&.positive?

  TcpUserTimeout.with_timeout(seconds) { @app.call(env) }
end