Class: RateLimit::Limiter

Inherits:
Object
  • Object
show all
Defined in:
lib/ratelimit-ruby.rb

Instance Method Summary collapse

Constructor Details

#initialize(apikey:, on_error: :log_and_pass, logger: nil, debug: false, stats: nil, shared_cache: nil, in_process_cache: nil, use_expiry_cache: true, local: false) ⇒ Limiter

Returns a new instance of Limiter.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/ratelimit-ruby.rb', line 10

def initialize(apikey:,
               on_error: :log_and_pass,
               logger: nil,
               debug: false,
               stats: nil, # receives increment("it.ratelim.limitcheck", {:tags=>["policy_group:page_view", "pass:true"]})
               shared_cache: nil, # Something that quacks like Rails.cache ideally memcached
               in_process_cache: nil, # ideally ActiveSupport::Cache::MemoryStore.new(size: 2.megabytes)
               use_expiry_cache: true, # must have shared_cache defined
               local: false # local development
)
  @on_error = on_error
  @logger = (logger || Logger.new($stdout)).tap do |log|
    log.progname = "RateLimit"
  end
  @stats = (stats || NoopStats.new)
  @shared_cache = (shared_cache || NoopCache.new)
  @in_process_cache = (in_process_cache || NoopCache.new)
  @use_expiry_cache = use_expiry_cache
  @conn = Faraday.new(:url => self.base_url(local)) do |faraday|
    faraday.request :json # form-encode POST params
    faraday.headers["accept"] = "application/json"
    faraday.response :logger if debug
    faraday.options[:open_timeout] = 2
    faraday.options[:timeout] = 5
    faraday.adapter Faraday.default_adapter # make requests with Net::HTTP
  end
  (@account_id, pass) = apikey.split("|")
  @conn.basic_auth(@account_id, pass)
end

Instance Method Details

#acquire(group, acquire_amount, allow_partial_response: false) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/ratelimit-ruby.rb', line 85

def acquire(group, acquire_amount, allow_partial_response: false)

  expiry_cache_key = "it.ratelim.expiry:#{group}"
  if @use_expiry_cache
    expiry = @shared_cache.read(expiry_cache_key)
    if !expiry.nil? && Integer(expiry) > Time.now.utc.to_f * 1000
      @stats.increment("it.ratelim.limitcheck.expirycache.hit", tags: [])
      return OpenStruct.new(passed: false, amount: 0)
    end
  end

  result = @conn.post '/api/v1/limitcheck', { acquireAmount: acquire_amount,
                                              groups: [group],
                                              allowPartialResponse: allow_partial_response }.to_json
  handle_failure(result) unless result.success?
  res =JSON.parse(result.body, object_class: OpenStruct)
  res.amount ||= 0

  @stats.increment("it.ratelim.limitcheck", tags: ["policy_group:#{res.policyGroup}", "pass:#{res.passed}"])
  if @use_expiry_cache
    reset = result.headers['X-Rate-Limit-Reset']
    @shared_cache.write(expiry_cache_key, reset) unless reset.nil?
  end
  return res
rescue => e
  handle_error(e)
end

#acquire_or_wait(key:, acquire_amount:, max_wait_secs:, init_backoff: 0) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/ratelimit-ruby.rb', line 113

def acquire_or_wait(key:, acquire_amount:, max_wait_secs:, init_backoff: 0)
  start = Time.now
  sleep = init_backoff
  while Time.now - start < max_wait_secs
    sleep(sleep)
    res = acquire(key, acquire_amount)
    if res.passed
      return res
    end
    sleep += rand * WAIT_INCR_MAX
  end
  raise RateLimit::WaitExceeded
end

#base_url(local) ⇒ Object



166
167
168
# File 'lib/ratelimit-ruby.rb', line 166

def base_url(local)
  local ? 'http://localhost:8080' : 'http://www.ratelim.it'
end

#create_limit(group, limit, policy, burst: nil) ⇒ Object

create only. does not overwrite if it already exists



52
53
54
# File 'lib/ratelimit-ruby.rb', line 52

def create_limit(group, limit, policy, burst: nil)
  upsert(LimitDefinition.new(group, limit, policy, false, burst || limit), :post)
end

#create_returnable_limit(group, total_tokens, seconds_to_refill_one_token) ⇒ Object



41
42
43
# File 'lib/ratelimit-ruby.rb', line 41

def create_returnable_limit(group, total_tokens, seconds_to_refill_one_token)
  upsert_returnable_limit(group, total_tokens, seconds_to_refill_one_token, method: :post)
end

#feature_is_on?(feature) ⇒ Boolean

Returns:

  • (Boolean)


137
138
139
# File 'lib/ratelimit-ruby.rb', line 137

def feature_is_on?(feature)
  feature_is_on_for?(feature, nil)
end

#feature_is_on_for?(feature, lookup_key, attributes: []) ⇒ Boolean

Returns:

  • (Boolean)


141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/ratelimit-ruby.rb', line 141

def feature_is_on_for?(feature, lookup_key, attributes: [])
  @stats.increment("it.ratelim.featureflag.on", tags: ["feature:#{feature}"])

  cache_key = "it.ratelim.ff:#{feature}.#{lookup_key}.#{attributes}"
  @in_process_cache.fetch(cache_key, expires_in: 60) do
    next uncached_feature_is_on_for?(feature, lookup_key, attributes) if @shared_cache.class == NoopCache

    feature_obj = get_feature(feature)
    if feature_obj.nil?
      next false
    end

    attributes << lookup_key if lookup_key
    if (attributes & feature_obj.whitelisted).size > 0
      next true
    end

    if lookup_key
      next get_user_pct(feature, lookup_key) < feature_obj.pct
    end

    next feature_obj.pct > 0.999
  end
end

#pass?(group) ⇒ Boolean

Returns:

  • (Boolean)


80
81
82
83
# File 'lib/ratelimit-ruby.rb', line 80

def pass?(group)
  result = acquire(group, 1)
  return result.passed
end

#return(limit_result) ⇒ Object



127
128
129
130
131
132
133
134
135
# File 'lib/ratelimit-ruby.rb', line 127

def return(limit_result)
  result = @conn.post '/api/v1/limitreturn',
                      { enforcedGroup: limit_result.enforcedGroup,
                        expiresAt: limit_result.expiresAt,
                        amount: limit_result.amount }.to_json
  handle_failure(result) unless result.success?
rescue => e
  handle_error(e)
end

#upsert(limit_definition, method) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/ratelimit-ruby.rb', line 61

def upsert(limit_definition, method)
  to_send = { limit: limit_definition.limit,
              group: limit_definition.group,
              burst: limit_definition.burst,
              policyName: limit_definition.policy,
              safetyLevel: limit_definition.safety_level,
              returnable: limit_definition.returnable }.to_json
  result= @conn.send(method, '/api/v1/limits', to_send)
  if !result.success?
    if method == :put
      handle_failure(result)
    elsif result.status != 409 # conflicts routinely expected on create
      handle_failure(result)
    end
  end
rescue => e
  handle_error(e)
end

#upsert_limit(group, limit, policy, burst: nil) ⇒ Object

upsert. overwrite whatever is there



57
58
59
# File 'lib/ratelimit-ruby.rb', line 57

def upsert_limit(group, limit, policy, burst: nil)
  upsert(LimitDefinition.new(group, limit, policy, false, burst || limit), :put)
end

#upsert_returnable_limit(group, total_tokens, seconds_to_refill_one_token, method: :put) ⇒ Object



45
46
47
48
49
# File 'lib/ratelimit-ruby.rb', line 45

def upsert_returnable_limit(group, total_tokens, seconds_to_refill_one_token, method: :put)
  recharge_rate = (24*60*60)/seconds_to_refill_one_token
  recharge_policy = DAILY_ROLLING
  upsert(LimitDefinition.new(group, recharge_rate, recharge_policy, true, total_tokens), method)
end