module GSKCache

Constants

BOOT_WAIT_TIME_MAX
CONNECT_TIMEOUT
HEADER_KEY
MAX_CACHE_TIMEOUT
MIN_CACHE_TIMEOUT
READ_TIMEOUT
SIGNING_KEY_URL

Keys used for signing in production

TEST_SIGNING_KEY_URL

Keys used for signing in a testing environment

UPDATER_BLOCK

Public Class Methods

set_timeout(read_timeout: nil, connect_timeout: nil) click to toggle source
# File lib/gsk_cache.rb, line 115
def self.set_timeout(read_timeout: nil, connect_timeout: nil)
  @@read_timeout    = read_timeout    if read_timeout
  @@connect_timeout = connect_timeout if connect_timeout
end
signing_keys() click to toggle source
# File lib/gsk_cache.rb, line 120
def self.signing_keys
  start if @@key_updater_thread.nil?

  @@key_updater_thread.thread_variable_get('keys')
end
start(logger: nil, source: nil, waittime: BOOT_WAIT_TIME_MAX) click to toggle source

Start a thread that keeps the Google signing keys updated.

# File lib/gsk_cache.rb, line 84
def self.start(logger: nil, source: nil, waittime: BOOT_WAIT_TIME_MAX)
  @@key_updater_semaphore.synchronize do
    # Another thread might have been waiting for on the mutex
    break unless @@key_updater_thread.nil?

    @@logger = logger
    @@source = if source
                 source
               elsif ENV['ENVIRONMENT'] == 'production'
                 SIGNING_KEY_URL
               else
                 TEST_SIGNING_KEY_URL
               end
    @@read_timeout    = READ_TIMEOUT
    @@connect_timeout = CONNECT_TIMEOUT

    new_thread = Thread.new(&UPDATER_BLOCK)

    start_time = Time.now.to_i

    while new_thread.thread_variable_get('keys').nil? && Time.now.to_i - start_time < waittime
      sleep 0.2
    end
    # Body has now been set.
    # Let other clients through.
    @@key_updater_thread = new_thread

    nil
  end
end
terminate() click to toggle source

Required for specs

# File lib/gsk_cache.rb, line 127
def self.terminate
  @@key_updater_thread&.terminate
  @@key_updater_thread = nil
end

Private Class Methods

log(level, message) click to toggle source
# File lib/gsk_cache.rb, line 135
def log(level, message)
  return if @@logger.nil?

  case level
  when :info
    @@logger.info(message)
  when :warning
    if @@logger.respond_to?(:warning)
      @@logger.warning(message)
    elsif @@logger.respond_to?(:warn)
      @@logger.warn(message)
    end
  when :error
    @@logger.error(message)
  when :fatal
    @@logger.fatal(message)
  else
    throw RuntimeError.new('Invalid log level')
  end
end
seconds_to_time(s) click to toggle source
# File lib/gsk_cache.rb, line 156
def seconds_to_time(s)
  days = (s / 86400).floor
  s %= 86400

  hours = (s / 3600).floor
  s %= 3600

  minutes = (s / 60).floor
  s %= 60

  out = ''
  out += "#{days} days "       if days.positive?
  out += "#{hours} hours "     if hours.positive?
  out += "#{minutes} minutes " if minutes.positive?
  out += "#{s} seconds"        if s.positive?

  out.rstrip
end