Class: Mato::Timeout

Inherits:
Object
  • Object
show all
Defined in:
lib/mato/timeout.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filter, timeout:, on_timeout:) ⇒ Timeout

Returns a new instance of Timeout.



11
12
13
14
15
16
17
18
19
# File 'lib/mato/timeout.rb', line 11

def initialize(filter, timeout:, on_timeout:)
  @filter = filter
  @duration_sec = timeout
  @on_timeout = on_timeout

  unless on_timeout
    raise ArgumentError, "Missing on_timeout callback"
  end
end

Instance Attribute Details

#duration_secObject (readonly)

Returns the value of attribute duration_sec.



8
9
10
# File 'lib/mato/timeout.rb', line 8

def duration_sec
  @duration_sec
end

#filterObject (readonly)

Returns the value of attribute filter.



7
8
9
# File 'lib/mato/timeout.rb', line 7

def filter
  @filter
end

#on_timeoutObject (readonly)

Returns the value of attribute on_timeout.



9
10
11
# File 'lib/mato/timeout.rb', line 9

def on_timeout
  @on_timeout
end

Instance Method Details

#call(content) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/mato/timeout.rb', line 21

def call(content)
  ::Timeout.timeout(duration_sec) do
    filter.call(content)
  end
rescue ::Timeout::Error => e
  on_timeout.call(e)
  content
end