Class: RedisThrottle::Api Private

Inherits:
Object
  • Object
show all
Defined in:
lib/redis_throttle/api.rb

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Instance Method Summary collapse

Constructor Details

#initialize(redis) ⇒ Api

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Api.

Parameters:

  • redis (Redis, Redis::Namespace, RedisClient, RedisClient::Decorator::Client)


29
30
31
# File 'lib/redis_throttle/api.rb', line 29

def initialize(redis)
  @redis = redis
end

Instance Method Details

#acquire(strategies:, token:) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

Returns:

  • (Boolean)


36
37
38
# File 'lib/redis_throttle/api.rb', line 36

def acquire(strategies:, token:)
  execute(:ACQUIRE, to_params(strategies.sort_by(&:itself)) << :TOKEN << token << :TS << Time.now.to_i).zero?
end

#info(strategies:) ⇒ Hash{Concurrency => Integer, RateLimit => Integer}

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

Returns:



70
71
72
# File 'lib/redis_throttle/api.rb', line 70

def info(strategies:)
  strategies.zip(execute(:INFO, to_params(strategies) << :TS << Time.now.to_i)).to_h
end

#release(strategies:, token:) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Parameters:



43
44
45
# File 'lib/redis_throttle/api.rb', line 43

def release(strategies:, token:)
  execute(:RELEASE, to_params(strategies) << :TOKEN << token)
end

#reset(strategies:) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Parameters:



49
50
51
# File 'lib/redis_throttle/api.rb', line 49

def reset(strategies:)
  execute(:RESET, to_params(strategies))
end

#strategies(match:) ⇒ Array<Concurrency, RateLimit>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • match (String)

Returns:



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/redis_throttle/api.rb', line 55

def strategies(match:)
  results = []
  block   = ->(key) { from_key(key)&.then { |strategy| results << strategy } }

  if redis_client?
    @redis.scan("MATCH", "#{NAMESPACE}:*:#{match}:*:*", &block)
  else
    @redis.scan_each(match: "#{NAMESPACE}:*:#{match}:*:*", &block)
  end

  results
end