Sha256: d35b9aedbb516309b41eb22d2ecca6921e6518f3fce6ed0e053e6570c7502078

Contents?: true

Size: 993 Bytes

Versions: 1

Compression:

Stored size: 993 Bytes

Contents

# 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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pecorino-0.6.0 lib/pecorino/block.rb