Class: SentrySmartSampler::RateLimit

Inherits:
Object
  • Object
show all
Defined in:
lib/sentry_smart_sampler/rate_limit.rb

Instance Method Summary collapse

Constructor Details

#initialize(key:, threshold:, interval:, increment: 1, cache: SentrySmartSampler.configuration.cache_storage) ⇒ RateLimit



11
12
13
14
15
16
17
# File 'lib/sentry_smart_sampler/rate_limit.rb', line 11

def initialize(key:, threshold:, interval:, increment: 1, cache: SentrySmartSampler.configuration.cache_storage)
  @key = key
  @threshold = threshold
  @interval = interval
  @increment = increment
  @cache = cache
end

Instance Method Details

#clear!Object



31
32
33
# File 'lib/sentry_smart_sampler/rate_limit.rb', line 31

def clear!
  cache.delete(storage_key)
end

#countObject



23
24
25
# File 'lib/sentry_smart_sampler/rate_limit.rb', line 23

def count
  cache.read(storage_key, raw: true).to_i
end

#increaseObject



35
36
37
# File 'lib/sentry_smart_sampler/rate_limit.rb', line 35

def increase
  cache.increment(storage_key, increment) || increment
end

#remainingObject



27
28
29
# File 'lib/sentry_smart_sampler/rate_limit.rb', line 27

def remaining
  threshold - count
end

#storage_keyObject



39
40
41
# File 'lib/sentry_smart_sampler/rate_limit.rb', line 39

def storage_key
  "#{KEY_PREFIX}/#{normalized_key}"
end

#throttled?Boolean



19
20
21
# File 'lib/sentry_smart_sampler/rate_limit.rb', line 19

def throttled?
  count >= threshold
end

#windowObject

let’s say that the interval is 1.hour the current time is 15:05 window is going to keep the same value until another interval time starts a new window is going to be start at 16:00



47
48
49
# File 'lib/sentry_smart_sampler/rate_limit.rb', line 47

def window
  Time.current.to_i / interval
end