Class: Prefab::Client
- Inherits:
-
Object
- Object
- Prefab::Client
- Defined in:
- lib/prefab/client.rb
Constant Summary collapse
- MAX_SLEEP_SEC =
10
- BASE_SLEEP_SEC =
0.5
Instance Attribute Summary collapse
-
#account_id ⇒ Object
readonly
Returns the value of attribute account_id.
-
#api_key ⇒ Object
readonly
Returns the value of attribute api_key.
-
#creds ⇒ Object
readonly
Returns the value of attribute creds.
-
#interceptor ⇒ Object
readonly
Returns the value of attribute interceptor.
-
#namespace ⇒ Object
readonly
Returns the value of attribute namespace.
-
#shared_cache ⇒ Object
readonly
Returns the value of attribute shared_cache.
-
#stats ⇒ Object
readonly
Returns the value of attribute stats.
Instance Method Summary collapse
- #cache_key(post_fix) ⇒ Object
- #channel ⇒ Object
- #config_client(timeout: 5.0) ⇒ Object
- #feature_flag_client ⇒ Object
-
#initialize(api_key: ENV['PREFAB_API_KEY'], logdev: nil, stats: nil, shared_cache: nil, local: false, namespace: "", log_formatter: nil) ⇒ Client
constructor
A new instance of Client.
- #log ⇒ Object
- #log_internal(level, msg) ⇒ Object
- #ratelimit_client(timeout: 5.0) ⇒ Object
- #request(service, method, req_options: {}, params: {}) ⇒ Object
Constructor Details
#initialize(api_key: ENV['PREFAB_API_KEY'], logdev: nil, stats: nil, shared_cache: nil, local: false, namespace: "", log_formatter: nil) ⇒ Client
Returns a new instance of Client.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/prefab/client.rb', line 9 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: nil ) 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) @creds = GRPC::Core::ChannelCredentials.new(ssl_certs) @stubs = {} end |
Instance Attribute Details
#account_id ⇒ Object (readonly)
Returns the value of attribute account_id.
7 8 9 |
# File 'lib/prefab/client.rb', line 7 def account_id @account_id end |
#api_key ⇒ Object (readonly)
Returns the value of attribute api_key.
7 8 9 |
# File 'lib/prefab/client.rb', line 7 def api_key @api_key end |
#creds ⇒ Object (readonly)
Returns the value of attribute creds.
7 8 9 |
# File 'lib/prefab/client.rb', line 7 def creds @creds end |
#interceptor ⇒ Object (readonly)
Returns the value of attribute interceptor.
7 8 9 |
# File 'lib/prefab/client.rb', line 7 def interceptor @interceptor end |
#namespace ⇒ Object (readonly)
Returns the value of attribute namespace.
7 8 9 |
# File 'lib/prefab/client.rb', line 7 def namespace @namespace end |
#shared_cache ⇒ Object (readonly)
Returns the value of attribute shared_cache.
7 8 9 |
# File 'lib/prefab/client.rb', line 7 def shared_cache @shared_cache end |
#stats ⇒ Object (readonly)
Returns the value of attribute stats.
7 8 9 |
# File 'lib/prefab/client.rb', line 7 def stats @stats end |
Instance Method Details
#cache_key(post_fix) ⇒ Object
85 86 87 |
# File 'lib/prefab/client.rb', line 85 def cache_key(post_fix) "prefab:#{account_id}:#{post_fix}" end |
#channel ⇒ Object
32 33 34 35 36 37 |
# File 'lib/prefab/client.rb', line 32 def channel @_channel ||= @local ? GRPC::Core::Channel.new('localhost:8443', nil, :this_channel_is_insecure) : GRPC::Core::Channel.new('api.prefab.cloud:8443', nil, @creds) end |
#config_client(timeout: 5.0) ⇒ Object
39 40 41 |
# File 'lib/prefab/client.rb', line 39 def config_client(timeout: 5.0) @config_client ||= Prefab::ConfigClient.new(self, timeout) end |
#feature_flag_client ⇒ Object
47 48 49 |
# File 'lib/prefab/client.rb', line 47 def feature_flag_client @feature_flag_client ||= Prefab::FeatureFlagClient.new(self) end |
#log ⇒ Object
51 52 53 |
# File 'lib/prefab/client.rb', line 51 def log @logger_client ||= Prefab::LoggerClient.new(@logdev, formatter: @log_formatter) end |
#log_internal(level, msg) ⇒ Object
55 56 57 |
# File 'lib/prefab/client.rb', line 55 def log_internal(level, msg) log.log_internal msg, "prefab", nil, level end |
#ratelimit_client(timeout: 5.0) ⇒ Object
43 44 45 |
# File 'lib/prefab/client.rb', line 43 def ratelimit_client(timeout: 5.0) @ratelimit_client ||= Prefab::RateLimitClient.new(self, timeout) end |
#request(service, method, req_options: {}, params: {}) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/prefab/client.rb', line 59 def request(service, method, req_options: {}, params: {}) opts = { timeout: 10 }.merge() 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 |