Module: ASIR::PollThrottle
- Defined in:
- lib/asir/poll_throttle.rb
Instance Method Summary collapse
-
#poll_throttle(opts = nil) ⇒ Object
Polls block until non-nil block result.
Instance Method Details
#poll_throttle(opts = nil) ⇒ Object
Polls block until non-nil block result. If non-nil, retry after sleeping for s sec, which starts at opts. Will retry up to opts times, if defined. s is multiplied by opts and incremented by opts, if defined. s is limited by opts. s is adjusted by s * opts, if defined. Returns result yield from block.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/asir/poll_throttle.rb', line 12 def poll_throttle opts = nil opts ||= { } i = 0 s = opts[:min_sleep] ||= 0.01 opts[:max_sleep] ||= 60 # opts[:inc_sleep] ||= 1 # opts[:mul_sleep] ||= 1.5 result = nil loop do i += 1 unless (result = yield).nil? return result end if x = opts[:max_tries] and i >= x return result end this_s = s if x = opts[:rand_sleep] this_s += s * rand * x end if opts[:verbose] $stderr.puts " #{self}: poll_throttle: sleeping for #{this_s} sec" end sleep this_s if this_s > 0 if x = opts[:mul_sleep] s *= x end if x = opts[:inc_sleep] s += x end if x = opts[:max_sleep] and s > x s = x end if x = opts[:min_sleep] and s < x s = x end end result end |