Class: RestCore::Timeout::TimerThread
- Inherits:
-
Object
- Object
- RestCore::Timeout::TimerThread
- Defined in:
- lib/rest-core/middleware/timeout/timer_thread.rb
Instance Attribute Summary collapse
-
#error ⇒ Object
Returns the value of attribute error.
-
#timeout ⇒ Object
Returns the value of attribute timeout.
Instance Method Summary collapse
- #cancel ⇒ Object
- #canceled? ⇒ Boolean
-
#initialize(timeout, error, &block) ⇒ TimerThread
constructor
A new instance of TimerThread.
- #on_timeout(&block) ⇒ Object
- #start ⇒ Object
Constructor Details
#initialize(timeout, error, &block) ⇒ TimerThread
Returns a new instance of TimerThread.
5 6 7 8 9 10 11 12 |
# File 'lib/rest-core/middleware/timeout/timer_thread.rb', line 5 def initialize timeout, error, &block t = Thread.current self.timeout = timeout self.error = error self.block = block || lambda{ t.raise error } @canceled = false start end |
Instance Attribute Details
#error ⇒ Object
Returns the value of attribute error.
3 4 5 |
# File 'lib/rest-core/middleware/timeout/timer_thread.rb', line 3 def error @error end |
#timeout ⇒ Object
Returns the value of attribute timeout.
3 4 5 |
# File 'lib/rest-core/middleware/timeout/timer_thread.rb', line 3 def timeout @timeout end |
Instance Method Details
#cancel ⇒ Object
18 19 20 |
# File 'lib/rest-core/middleware/timeout/timer_thread.rb', line 18 def cancel @canceled = true end |
#canceled? ⇒ Boolean
22 23 24 |
# File 'lib/rest-core/middleware/timeout/timer_thread.rb', line 22 def canceled? @canceled end |
#on_timeout(&block) ⇒ Object
14 15 16 |
# File 'lib/rest-core/middleware/timeout/timer_thread.rb', line 14 def on_timeout &block self.block = block end |
#start ⇒ Object
26 27 28 29 30 31 32 |
# File 'lib/rest-core/middleware/timeout/timer_thread.rb', line 26 def start return if timeout.nil? || timeout.zero? self.thread = Thread.new{ sleep(timeout) block.call unless canceled? } end |