0.0
No commit activity in last 3 years
No release in over 3 years
a timeout with a rolling window that can be moved forward
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies

Development

~> 1.3
>= 0
 Project Readme

Build Status

RollingTimeout

A timeout class with a rolling window. Call reset to reset the timer back to 0, stop and start do what you might expect.

Installation

Add this line to your application's Gemfile:

	gem 'rolling_timeout'

And then execute:

	$ bundle

Or install it yourself as:

	$ gem install rolling_timeout

Usage

require 'rolling_timeout'

begin
	RollingTimeout.new(1) {|timer| # 1 second timeout
		sleep(0.9)
		timer.reset # Back to 1 second on the timeout clock
		sleep(0.9)
		timer.stop  # The timer is stopped, we can do what we want for as long as we want
		sleep(0.2)  # This would have taken us over the 1 second timeout if the timer was running
		timer.start # restart the timer
		sleep(100)  # This will now timeout quickly
	}
rescue Timeout::Error
	puts "Timed out"
end

Requirements

Works with ruby 2.0, probably 1.9.3 too. 1.8.7 YMMV.

Issues

It's probably not a very accurate (relatively speaking) timer. How the VM schedules the code after the timer thread wakes up will affect how long after the actual time limit expires that the code thread is stopped and an exception raised. Also, if the code finishes at the same time the timeout thread wakes up it's a race to return.

That said, this was accurate enough for my needs. If you need something more accurate please feel free to submit a patch.

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

Author