Class: Prefab::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/prefab/client.rb

Constant Summary collapse

MAX_SLEEP_SEC =
10
BASE_SLEEP_SEC =
0.5
DEFAULT_LOG_FORMATTER =
proc {|severity, datetime, progname, msg|
  "#{severity.ljust(5)} #{datetime}: #{progname} #{msg}\n"
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key: ENV['PREFAB_API_KEY'], logdev: nil, stats: nil, shared_cache: nil, local: false, namespace: "", log_formatter: DEFAULT_LOG_FORMATTER) ⇒ Client

Returns a new instance of Client.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/prefab/client.rb', line 13

def initialize(api_key: ENV['PREFAB_API_KEY'],
               logdev: nil,
               stats: nil, # receives increment("prefab.limitcheck", {:tags=>["policy_group:page_view", "pass:true"]})
               shared_cache: nil, # Something that quacks like Rails.cache ideally memcached
               local: false,
               namespace: "",
               log_formatter: DEFAULT_LOG_FORMATTER
)
  raise "No API key. Set PREFAB_API_KEY env var" if api_key.nil? || api_key.empty?
  @logdev = (logdev || $stdout)
  @log_formatter = log_formatter
  @local = local
  @stats = (stats || NoopStats.new)
  @shared_cache = (shared_cache || NoopCache.new)
  @api_key = api_key
  @account_id = api_key.split("|")[0].to_i
  @namespace = namespace
  @interceptor = AuthInterceptor.new(api_key)
  @stubs = {}

  at_exit do
    channel.destroy
  end
end

Instance Attribute Details

#account_idObject (readonly)

Returns the value of attribute account_id.



11
12
13
# File 'lib/prefab/client.rb', line 11

def 
  @account_id
end

#api_keyObject (readonly)

Returns the value of attribute api_key.



11
12
13
# File 'lib/prefab/client.rb', line 11

def api_key
  @api_key
end

#interceptorObject (readonly)

Returns the value of attribute interceptor.



11
12
13
# File 'lib/prefab/client.rb', line 11

def interceptor
  @interceptor
end

#namespaceObject (readonly)

Returns the value of attribute namespace.



11
12
13
# File 'lib/prefab/client.rb', line 11

def namespace
  @namespace
end

#shared_cacheObject (readonly)

Returns the value of attribute shared_cache.



11
12
13
# File 'lib/prefab/client.rb', line 11

def shared_cache
  @shared_cache
end

#statsObject (readonly)

Returns the value of attribute stats.



11
12
13
# File 'lib/prefab/client.rb', line 11

def stats
  @stats
end

Instance Method Details

#cache_key(post_fix) ⇒ Object



90
91
92
# File 'lib/prefab/client.rb', line 90

def cache_key(post_fix)
  "prefab:#{account_id}:#{post_fix}"
end

#channelObject



38
39
40
41
42
# File 'lib/prefab/client.rb', line 38

def channel
  credentials = ENV["PREFAB_CLOUD_HTTP"] == "true" ? :this_channel_is_insecure : creds
  url = ENV["PREFAB_API_URL"] || 'api.prefab.cloud:443'
  @_channel ||= GRPC::Core::Channel.new(url, nil, credentials)
end

#config_client(timeout: 5.0) ⇒ Object



44
45
46
# File 'lib/prefab/client.rb', line 44

def config_client(timeout: 5.0)
  @config_client ||= Prefab::ConfigClient.new(self, timeout)
end

#feature_flag_clientObject



52
53
54
# File 'lib/prefab/client.rb', line 52

def feature_flag_client
  @feature_flag_client ||= Prefab::FeatureFlagClient.new(self)
end

#logObject



56
57
58
# File 'lib/prefab/client.rb', line 56

def log
  @logger_client ||= Prefab::LoggerClient.new(@logdev, formatter: @log_formatter)
end

#log_internal(level, msg) ⇒ Object



60
61
62
# File 'lib/prefab/client.rb', line 60

def log_internal(level, msg)
  log.log_internal msg, "prefab", nil, level
end

#ratelimit_client(timeout: 5.0) ⇒ Object



48
49
50
# File 'lib/prefab/client.rb', line 48

def ratelimit_client(timeout: 5.0)
  @ratelimit_client ||= Prefab::RateLimitClient.new(self, timeout)
end

#request(service, method, req_options: {}, params: {}) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/prefab/client.rb', line 64

def request(service, method, req_options: {}, params: {})
  opts = { timeout: 10 }.merge(req_options)

  attempts = 0
  start_time = Time.now

  begin
    attempts += 1
    return stub_for(service, opts[:timeout]).send(method, *params)
  rescue => exception

    log_internal Logger::WARN, exception

    if Time.now - start_time > opts[:timeout]
      raise exception
    end
    sleep_seconds = [BASE_SLEEP_SEC * (2 ** (attempts - 1)), MAX_SLEEP_SEC].min
    sleep_seconds = sleep_seconds * (0.5 * (1 + rand()))
    sleep_seconds = [BASE_SLEEP_SEC, sleep_seconds].max
    log_internal Logger::INFO, "Sleep #{sleep_seconds} and Reset #{service} #{method}"
    sleep sleep_seconds
    reset!
    retry
  end
end