forked from Shopify/limiter
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
df50c7f
commit 578af89
Showing
5 changed files
with
57 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'limiter/clock' | ||
require 'limiter/mixin' | ||
require 'limiter/rate_queue' | ||
require 'limiter/version' |
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'singleton' | ||
|
||
module Limiter | ||
class Clock | ||
include Singleton | ||
|
||
extend SingleForwardable | ||
def_single_delegators :instance, :skip, :time | ||
|
||
def initialize | ||
@offset = 0 | ||
end | ||
|
||
def skip(interval) | ||
@offset += interval | ||
end | ||
|
||
def time | ||
now + @offset | ||
end | ||
|
||
private | ||
|
||
def now | ||
Time.now | ||
end | ||
end | ||
end |
This file contains 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 characters
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'test_helper' | ||
|
||
module Limiter | ||
class ClockTest < Minitest::Test | ||
def setup | ||
super | ||
@start_time = Clock.time | ||
end | ||
|
||
def test_clock_is_advancing | ||
assert Clock.time > @start_time | ||
end | ||
|
||
def test_skips_interval | ||
Clock.skip(300) | ||
assert Clock.time >= (@start_time + 300) | ||
end | ||
end | ||
end |
This file contains 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 characters