Class: Concurrent::Cancellation
- Inherits:
-
Synchronization::Object
- Object
- Synchronization::Object
- Concurrent::Cancellation
- Defined in:
- lib/concurrent-ruby-edge/concurrent/edge/cancellation.rb
Overview
The Cancellation abstraction provides cooperative cancellation.
The standard methods ‘Thread#raise` of `Thread#kill` available in Ruby are very dangerous (see linked the blog posts bellow). Therefore concurrent-ruby provides an alternative.
-
<jvns.ca/blog/2015/11/27/why-rubys-timeout-is-dangerous-and-thread-dot-raise-is-terrifying/>
-
<www.mikeperham.com/2015/05/08/timeout-rubys-most-dangerous-api/>
-
<blog.headius.com/2008/02/rubys-threadraise-threadkill-timeoutrb.html>
It provides an object which represents a task which can be executed, the task has to get the reference to the object and periodically cooperatively check that it is not cancelled. Good practices to make tasks cancellable:
-
check cancellation every cycle of a loop which does significant work,
-
do all blocking actions in a loop with a timeout then on timeout check cancellation and if ok block again with the timeout
The idea was inspired by <msdn.microsoft.com/en-us/library/dd537607(v=vs.110).aspx>
Class Method Summary collapse
-
.timeout(intended_time) ⇒ Cancellation
Create Cancellation which will cancel itself in given time.
Instance Method Summary collapse
-
#canceled? ⇒ true, false
Is the cancellation cancelled? Respective, was the origin of the cancellation resolved.
-
#check!(error = CancelledOperationError) ⇒ self
Raise error when cancelled.
-
#initialize(origin = Promises.resolvable_event) ⇒ Cancellation
constructor
Creates the cancellation object.
-
#join(*cancellations) ⇒ Cancellation
Creates a new Cancellation which is cancelled when first of the supplied cancellations or self is cancelled.
-
#origin ⇒ Promises::Future, Promises::Event
The event or future which is the origin of the cancellation.
-
#to_ary ⇒ Array(Cancellation, Promises::Future), Array(Cancellation, Promises::Event)
Allow to multi-assign the Cancellation object.
-
#to_s ⇒ String
(also: #inspect)
Short string representation.
Constructor Details
#initialize(origin = Promises.resolvable_event) ⇒ Cancellation
Creates the cancellation object.
54 55 56 57 |
# File 'lib/concurrent-ruby-edge/concurrent/edge/cancellation.rb', line 54 def initialize(origin = Promises.resolvable_event) super() @Origin = origin end |
Class Method Details
.timeout(intended_time) ⇒ Cancellation
Create Cancellation which will cancel itself in given time
43 44 45 |
# File 'lib/concurrent-ruby-edge/concurrent/edge/cancellation.rb', line 43 def self.timeout(intended_time) new Concurrent::Promises.schedule(intended_time) end |
Instance Method Details
#canceled? ⇒ true, false
Is the cancellation cancelled? Respective, was the origin of the cancellation resolved.
77 78 79 |
# File 'lib/concurrent-ruby-edge/concurrent/edge/cancellation.rb', line 77 def canceled? @Origin.resolved? end |
#check!(error = CancelledOperationError) ⇒ self
Raise error when cancelled
85 86 87 88 |
# File 'lib/concurrent-ruby-edge/concurrent/edge/cancellation.rb', line 85 def check!(error = CancelledOperationError) raise error if canceled? self end |
#join(*cancellations) ⇒ Cancellation
Creates a new Cancellation which is cancelled when first of the supplied cancellations or self is cancelled.
95 96 97 |
# File 'lib/concurrent-ruby-edge/concurrent/edge/cancellation.rb', line 95 def join(*cancellations) Cancellation.new Promises.any_event(*[@Origin, *cancellations.map(&:origin)]) end |
#origin ⇒ Promises::Future, Promises::Event
The event or future which is the origin of the cancellation
70 71 72 |
# File 'lib/concurrent-ruby-edge/concurrent/edge/cancellation.rb', line 70 def origin @Origin end |
#to_ary ⇒ Array(Cancellation, Promises::Future), Array(Cancellation, Promises::Event)
Allow to multi-assign the Cancellation object
64 65 66 |
# File 'lib/concurrent-ruby-edge/concurrent/edge/cancellation.rb', line 64 def to_ary [self, @Origin] end |
#to_s ⇒ String Also known as: inspect
Short string representation.
101 102 103 |
# File 'lib/concurrent-ruby-edge/concurrent/edge/cancellation.rb', line 101 def to_s format '%s %s>', super[0..-2], canceled? ? 'canceled' : 'pending' end |