-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
for blocking for an arbitrary timespan
- Loading branch information
Showing
6 changed files
with
72 additions
and
12 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
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,24 @@ | ||
# frozen_string_literal: true | ||
|
||
# Provides access to Pecorino blocks - same blocks which get set when a throttle triggers. The blocks | ||
# are just keys in the data store which have an expiry value. This can be useful if you want to restrict | ||
# access to a resource for an arbitrary timespan. | ||
class Pecorino::Block | ||
# Sets a block for the given key. The block will also be seen by the Pecorino::Throttle with the same key | ||
# | ||
# @param key[String] the key to set the block for | ||
# @param block_for[Float] the number of seconds or a time interval to block for | ||
# @return [Time] the time when the block will be released | ||
def self.set!(key:, block_for:) | ||
Pecorino.adapter.set_block(key: key, block_for: block_for) | ||
Time.now + block_for | ||
end | ||
|
||
# Returns the time until a certain block is in effect | ||
# | ||
# @return [Time,nil] the time when the block will be released | ||
def self.blocked_until(key:) | ||
t = Pecorino.adapter.blocked_until(key: key) | ||
(t && t > Time.now) ? t : nil | ||
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
# frozen_string_literal: true | ||
|
||
module Pecorino | ||
VERSION = "0.5.0" | ||
VERSION = "0.6.0" | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# frozen_string_literal: true | ||
|
||
require "test_helper" | ||
|
||
class BlockTest < ActiveSupport::TestCase | ||
def setup | ||
create_postgres_database | ||
end | ||
|
||
def teardown | ||
drop_postgres_database | ||
end | ||
|
||
test "sets a block" do | ||
k = Base64.strict_encode64(Random.bytes(4)) | ||
assert_nil Pecorino::Block.blocked_until(key: k) | ||
assert Pecorino::Block.set!(key: k, block_for: 30.minutes) | ||
|
||
blocked_until = Pecorino::Block.blocked_until(key: k) | ||
assert_in_delta Time.now + 30.minutes, blocked_until, 10 | ||
end | ||
|
||
test "does not return a block which has lapsed" do | ||
k = Base64.strict_encode64(Random.bytes(4)) | ||
assert_nil Pecorino::Block.blocked_until(key: k) | ||
assert Pecorino::Block.set!(key: k, block_for: -30.minutes) | ||
|
||
blocked_until = Pecorino::Block.blocked_until(key: k) | ||
assert_nil blocked_until | ||
end | ||
end |