SquareSquash/ruby — pull request #9 by simonista (2014-06-04 23:01:16+00:00)
Title: Timeout protection

Hey Guys!

This PR makes it possible to override the timeout protection method used by squash. Our use-case is that we'd like to provide a custom timeout protection method that tracks failures in redis and if it sees a X number of failures in less than Y seconds (say, for example, squash was down), it would start failing without even trying to run the block. Something like this:

```ruby
Squash::Ruby.configure :timeout_protection => proc { |timeout, &block|
short_circuit_timeout("timeouts:squash", timeout, X, Y, &block)
}

def self.short_circuit_timeout(redis_key, timeout, cutoff, error_ttl, &block)
error_count = redis.get(redis_key)
if error_count.to_i >= cutoff
raise Timeout::Error
end

begin
Timeout.timeout(timeout) do
yield
end
rescue Timeout::Error => e
redis.incrby(redis_key, 1)
redis.expire(redis_key, error_ttl)
raise
end
end
```

Let me know if you have any feedback. Happy to make changes if you see the need.