Class: Shugoi::ConfigCache

Inherits:
Object
  • Object
show all
Defined in:
lib/shugoi/config_cache.rb

Constant Summary collapse

TTL_MS =
30_000
STALE_MAX_MS =
600_000

Instance Method Summary collapse

Constructor Details

#initialize(api_client, signing_secret = nil) ⇒ ConfigCache

Returns a new instance of ConfigCache.



6
7
8
9
10
11
12
13
14
15
# File 'lib/shugoi/config_cache.rb', line 6

def initialize(api_client, signing_secret = nil)
  @api_client = api_client
  @signing_secret = signing_secret
  @mutex = Mutex.new
  @whitelist = []
  @flags = {}
  @skip_paths = []
  @fetched_at = 0
  @refreshing = false
end

Instance Method Details

#fetch(site_key) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/shugoi/config_cache.rb', line 17

def fetch(site_key)
  now = Utils.now_ms
  refresh_now = false
  refresh_async = false

  @mutex.synchronize do
    age = now - @fetched_at
    refresh_now = @fetched_at.zero? || age > STALE_MAX_MS
    refresh_async = !refresh_now && age > TTL_MS && !@refreshing
    @refreshing = true if refresh_now || refresh_async
  end

  if refresh_now
    refresh(site_key)
  elsif refresh_async
    Thread.new { refresh(site_key) }.abort_on_exception = false
  end

  @mutex.synchronize do
    { whitelist: @whitelist.dup, flags: @flags.dup, skip_paths: @skip_paths.dup }
  end
end