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
- NO_DEFAULT_PROVIDED =
:no_default_provided
Instance Attribute Summary collapse
-
#api_key ⇒ Object
readonly
Returns the value of attribute api_key.
-
#interceptor ⇒ Object
readonly
Returns the value of attribute interceptor.
-
#namespace ⇒ Object
readonly
Returns the value of attribute namespace.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#prefab_api_url ⇒ Object
readonly
Returns the value of attribute prefab_api_url.
-
#project_id ⇒ Object
readonly
Returns the value of attribute project_id.
-
#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
- #enabled?(feature_name, lookup_key = nil, attributes = {}) ⇒ Boolean
- #feature_flag_client ⇒ Object
- #get(key, default_or_lookup_key = NO_DEFAULT_PROVIDED, attributes = {}, ff_default = NO_DEFAULT_PROVIDED) ⇒ Object
-
#initialize(options = Prefab::Options.new) ⇒ Client
constructor
A new instance of Client.
- #log ⇒ Object
- #log_internal(level, msg, path = "prefab") ⇒ Object
- #ratelimit_client(timeout: 5.0) ⇒ Object
- #request(service, method, req_options: {}, params: {}) ⇒ Object
- #reset! ⇒ Object
Constructor Details
#initialize(options = Prefab::Options.new) ⇒ Client
Returns a new instance of Client.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/prefab/client.rb', line 10 def initialize( = Prefab::Options.new) = @shared_cache = .shared_cache @stats = .stats @namespace = .namespace @stubs = {} if .local_only? @project_id = 0 log_internal Logger::INFO, "Prefab Running in Local Mode" else @api_key = .api_key raise Prefab::Errors::InvalidApiKeyError.new(@api_key) if @api_key.nil? || @api_key.empty? || api_key.count("-") != 3 @project_id = @api_key.split("-")[0].to_i # unvalidated, but that's ok. APIs only listen to the actual passwd @interceptor = Prefab::AuthInterceptor.new(@api_key) @prefab_api_url = .prefab_api_url @prefab_grpc_url = .prefab_grpc_url log_internal Logger::INFO, "Prefab Connecting to: #{@prefab_api_url} and #{@prefab_grpc_url} Secure: #{http_secure?}" at_exit do channel.destroy end end end |
Instance Attribute Details
#api_key ⇒ Object (readonly)
Returns the value of attribute api_key.
8 9 10 |
# File 'lib/prefab/client.rb', line 8 def api_key @api_key end |
#interceptor ⇒ Object (readonly)
Returns the value of attribute interceptor.
8 9 10 |
# File 'lib/prefab/client.rb', line 8 def interceptor @interceptor end |
#namespace ⇒ Object (readonly)
Returns the value of attribute namespace.
8 9 10 |
# File 'lib/prefab/client.rb', line 8 def namespace @namespace end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
8 9 10 |
# File 'lib/prefab/client.rb', line 8 def end |
#prefab_api_url ⇒ Object (readonly)
Returns the value of attribute prefab_api_url.
8 9 10 |
# File 'lib/prefab/client.rb', line 8 def prefab_api_url @prefab_api_url end |
#project_id ⇒ Object (readonly)
Returns the value of attribute project_id.
8 9 10 |
# File 'lib/prefab/client.rb', line 8 def project_id @project_id end |
#shared_cache ⇒ Object (readonly)
Returns the value of attribute shared_cache.
8 9 10 |
# File 'lib/prefab/client.rb', line 8 def shared_cache @shared_cache end |
#stats ⇒ Object (readonly)
Returns the value of attribute stats.
8 9 10 |
# File 'lib/prefab/client.rb', line 8 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:#{project_id}:#{post_fix}" end |
#channel ⇒ Object
34 35 36 37 |
# File 'lib/prefab/client.rb', line 34 def channel credentials = http_secure? ? creds : :this_channel_is_insecure @_channel ||= GRPC::Core::Channel.new(@prefab_grpc_url, nil, credentials) 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 |
#enabled?(feature_name, lookup_key = nil, attributes = {}) ⇒ Boolean
94 95 96 |
# File 'lib/prefab/client.rb', line 94 def enabled?(feature_name, lookup_key=nil, attributes={}) feature_flag_client.feature_is_on_for?(feature_name, lookup_key, attributes: attributes) 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 |
#get(key, default_or_lookup_key = NO_DEFAULT_PROVIDED, attributes = {}, ff_default = NO_DEFAULT_PROVIDED) ⇒ Object
98 99 100 101 102 103 104 105 106 |
# File 'lib/prefab/client.rb', line 98 def get(key, default_or_lookup_key=NO_DEFAULT_PROVIDED, attributes={}, ff_default=NO_DEFAULT_PROVIDED) result = config_client.get(key, default_or_lookup_key) if result.is_a?(Prefab::FeatureFlag) feature_flag_client.get(key, default_or_lookup_key, attributes, default: ff_default) else result end 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, path = "prefab") ⇒ Object
55 56 57 |
# File 'lib/prefab/client.rb', line 55 def log_internal(level, msg, path = "prefab") log.log_internal msg, path, 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 |
#reset! ⇒ Object
89 90 91 92 |
# File 'lib/prefab/client.rb', line 89 def reset! @stubs.clear @_channel = nil end |