Class: Interceptors::TimeoutInterceptor

Inherits:
Interceptor show all
Defined in:
lib/interceptors/timeout_interceptor.rb

Instance Method Summary collapse

Methods inherited from Interceptor

#after, #before

Constructor Details

#initialize(seconds:) ⇒ TimeoutInterceptor

Returns a new instance of TimeoutInterceptor.

Raises:

  • (ArgumentError)


7
8
9
10
11
# File 'lib/interceptors/timeout_interceptor.rb', line 7

def initialize(seconds:)
  raise ArgumentError, "seconds must be positive" unless seconds.to_f.positive?

  @seconds = seconds.to_f
end

Instance Method Details

#around(ctx) ⇒ Object



13
14
15
16
17
# File 'lib/interceptors/timeout_interceptor.rb', line 13

def around(ctx)
  Timeout.timeout(@seconds) { yield ctx }
rescue Timeout::Error
  Result.err(AppError.new("Timeout", code: "timeout", http_status: 504))
end