Last active
February 24, 2017 19:39
-
-
Save ethnt/9c96de38f7801b5f771322c2c9a876c8 to your computer and use it in GitHub Desktop.
Revisions
-
ethnt revised this gist
Feb 24, 2017 . 1 changed file with 4 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,5 @@ require 'redis' module Referee class Timeout < StandardError; end @@ -7,6 +9,8 @@ def initialize(key, limit = 5, &block) @key = key @limit = limit @redis.set(@key, @limit) start @work = Thread.new { yield self } -
ethnt revised this gist
Feb 24, 2017 . 2 changed files with 23 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,5 @@ require 'redis' @redis = Redis.new @redis.set('timeout', 0) This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,13 +2,13 @@ module Referee class Timeout < StandardError; end class Timer def initialize(key, limit = 5, &block) @redis = Redis.new @key = key @limit = limit start @work = Thread.new { yield self } @work.join @@ -17,7 +17,21 @@ def initialize(limit, &block) def start @timer = Thread.new do @counter = 0 # TODO: Use promises to make sure we have the limit from redis before # incrementing the counter. loop do @limit = @redis.get(@key).to_i @counter += 1 if @counter > @limit break end sleep(1) end @timed_out = true -
ethnt created this gist
Feb 24, 2017 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,39 @@ module Referee class Timeout < StandardError; end class Timer def initialize(limit, &block) @limit = limit start @timed_out = false @work = Thread.new { yield self } @work.join @timer.join if @timed_out end def start @timer = Thread.new do sleep(@limit) @timed_out = true @work.kill raise Referee::Timeout end end def stop @timer.kill end def reset stop start end end end This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,12 @@ require_relative 'referee' begin Referee::Timer.new(5) do |timer| sleep(1) puts "yup" sleep(5) puts "nope" end rescue Referee::Timeout puts "timed out!" end