Class: Pause::Redis::Adapter

Inherits:
Object
  • Object
show all
Includes:
Helper::Timing
Defined in:
lib/pause/redis/adapter.rb

Overview

This class encapsulates Redis operations used by Pause

Direct Known Subclasses

ShardedAdapter

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helper::Timing

#period_marker

Constructor Details

#initialize(config) ⇒ Adapter

Returns a new instance of Adapter.



12
13
14
15
16
# File 'lib/pause/redis/adapter.rb', line 12

def initialize(config)
  @resolution = config.resolution
  @time_blocks_to_keep = config.history / @resolution
  @history = config.history
end

Instance Attribute Details

#historyObject

Returns the value of attribute history.



10
11
12
# File 'lib/pause/redis/adapter.rb', line 10

def history
  @history
end

#resolutionObject

Returns the value of attribute resolution.



10
11
12
# File 'lib/pause/redis/adapter.rb', line 10

def resolution
  @resolution
end

#time_blocks_to_keepObject

Returns the value of attribute time_blocks_to_keep.



10
11
12
# File 'lib/pause/redis/adapter.rb', line 10

def time_blocks_to_keep
  @time_blocks_to_keep
end

Instance Method Details

#all_keys(scope) ⇒ Object



47
48
49
# File 'lib/pause/redis/adapter.rb', line 47

def all_keys(scope)
  keys(tracked_scope(scope))
end

#delete_rate_limited_key(scope, id) ⇒ Object



62
63
64
65
# File 'lib/pause/redis/adapter.rb', line 62

def delete_rate_limited_key(scope, id)
  delete_tracking_keys(scope, [id])
  redis.zrem rate_limited_list(scope), id
end

#delete_rate_limited_keys(scope) ⇒ Object

For a scope, delete the entire sorted set that holds the block list. Also delete the original tracking information, so we don’t immediately re-block the id



57
58
59
60
# File 'lib/pause/redis/adapter.rb', line 57

def delete_rate_limited_keys(scope)
  delete_tracking_keys(scope, rate_limited_keys(scope))
  redis.del rate_limited_list(scope)
end

#disable(scope) ⇒ Object



67
68
69
# File 'lib/pause/redis/adapter.rb', line 67

def disable(scope)
  redis.set("internal:|#{scope}|:disabled", "1")
end

#disabled?(scope) ⇒ Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/pause/redis/adapter.rb', line 75

def disabled?(scope)
  ! enabled?(scope)
end

#enable(scope) ⇒ Object



71
72
73
# File 'lib/pause/redis/adapter.rb', line 71

def enable(scope)
  redis.del("internal:|#{scope}|:disabled")
end

#enabled?(scope) ⇒ Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/pause/redis/adapter.rb', line 79

def enabled?(scope)
  redis.get("internal:|#{scope}|:disabled").nil?
end

#expire_block_list(scope) ⇒ Object



83
84
85
# File 'lib/pause/redis/adapter.rb', line 83

def expire_block_list(scope)
  redis.zremrangebyscore rate_limited_list(scope), '-inf', Time.now.to_i
end

#increment(scope, identifier, timestamp, count = 1) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/pause/redis/adapter.rb', line 18

def increment(scope, identifier, timestamp, count = 1)
  k = tracked_key(scope, identifier)
  redis.multi do |redis|
    redis.zincrby k, count, period_marker(resolution, timestamp)
    redis.expire k, history
  end

  if redis.zcard(k) > time_blocks_to_keep
    list = extract_set_elements(k)
    to_remove = list.slice(0, (list.size - time_blocks_to_keep))
    redis.zrem(k, to_remove.map(&:ts))
  end
end

#key_history(scope, identifier) ⇒ Object



32
33
34
# File 'lib/pause/redis/adapter.rb', line 32

def key_history(scope, identifier)
  extract_set_elements(tracked_key(scope, identifier))
end

#rate_limit!(scope, identifier, block_ttl) ⇒ Object



36
37
38
39
40
# File 'lib/pause/redis/adapter.rb', line 36

def rate_limit!(scope, identifier, block_ttl)
  timestamp = Time.now.to_i + block_ttl
  redis.zadd rate_limited_list(scope), timestamp, identifier
  expire_block_list(scope)
end

#rate_limited?(scope, identifier) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
45
# File 'lib/pause/redis/adapter.rb', line 42

def rate_limited?(scope, identifier)
  blocked_until = redis.zscore(rate_limited_list(scope), identifier)
  !!blocked_until && blocked_until > Time.now.to_i
end

#rate_limited_keys(scope) ⇒ Object



51
52
53
# File 'lib/pause/redis/adapter.rb', line 51

def rate_limited_keys(scope)
  redis.zrangebyscore rate_limited_list(scope), Time.now.to_i, '+inf'
end