@@ -38,19 +38,13 @@ class Agent
3838 include Concurrent ::Observable
3939 include Logging
4040
41- # The default timeout value (in seconds); used when no timeout option
42- # is given at initialization
43- TIMEOUT = 5
44-
4541 attr_reader :timeout , :task_executor , :operation_executor
4642
4743 # Initialize a new Agent with the given initial value and provided options.
4844 #
4945 # @param [Object] initial the initial value
5046 # @param [Hash] opts the options used to define the behavior at update and deref
5147 #
52- # @option opts [Fixnum] :timeout (TIMEOUT) maximum number of seconds before an update is cancelled
53- #
5448 # @option opts [Boolean] :operation (false) when `true` will execute the future on the global
5549 # operation pool (for long-running operations), when `false` will execute the future on the
5650 # global task pool (for short-running tasks)
@@ -65,7 +59,6 @@ def initialize(initial, opts = {})
6559 @value = initial
6660 @rescuers = [ ]
6761 @validator = Proc . new { |result | true }
68- @timeout = opts . fetch ( :timeout , TIMEOUT ) . freeze
6962 self . observers = CopyOnWriteObserverSet . new
7063 @serialized_execution = SerializedExecution . new
7164 @task_executor = OptionsParser . get_task_executor_from ( opts )
@@ -145,12 +138,19 @@ def post(&block)
145138 # Update the current value with the result of the given block operation,
146139 # block can do blocking calls
147140 #
141+ # @param [Fixnum, nil] timeout maximum number of seconds before an update is cancelled
142+ #
148143 # @yield the operation to be performed with the current value in order to calculate
149144 # the new value
150145 # @yieldparam [Object] value the current value
151146 # @yieldreturn [Object] the new value
152147 # @return [true, nil] nil when no block is given
153- def post_off ( &block )
148+ def post_off ( timeout = nil , &block )
149+ block = if timeout
150+ lambda { |value | Concurrent ::timeout ( timeout ) { block . call ( value ) } }
151+ else
152+ block
153+ end
154154 post_on ( @operation_executor , &block )
155155 end
156156
@@ -203,10 +203,8 @@ def work(&handler) # :nodoc:
203203 validator , value = mutex . synchronize { [ @validator , @value ] }
204204
205205 begin
206- result , valid = Concurrent ::timeout ( @timeout ) do
207- result = handler . call ( value )
208- [ result , validator . call ( result ) ]
209- end
206+ result = handler . call ( value )
207+ valid = validator . call ( result )
210208 rescue Exception => ex
211209 exception = ex
212210 end
0 commit comments