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
37
38
# 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?
  raise "PREFAB_API_KEY format invalid. Expecting 123-development-yourapikey-SDK" unless api_key.count("-") == 3
  @logdev = (logdev || $stdout)
  @log_formatter = log_formatter
  @local = local
  @stats = (stats || NoopStats.new)
  @shared_cache = (shared_cache || NoopCache.new)
  @api_key = api_key
  @project_id = api_key.split("-")[0].to_i
  @environment = api_key.split("-")[1]
  @namespace = namespace
  @interceptor = Prefab::AuthInterceptor.new(api_key)
  @stubs = {}

  at_exit do
    channel.destroy
  end
end

Instance Attribute Details

#api_key ⇒ Object (readonly)

Returns the value of attribute api_key.



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

def api_key
  @api_key
end

#environment ⇒ Object (readonly)

Returns the value of attribute environment.



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

def environment
  @environment
end

#interceptor ⇒ Object (readonly)

Returns the value of attribute interceptor.



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

def interceptor
  @interceptor
end

#namespace ⇒ Object (readonly)

Returns the value of attribute namespace.



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

def namespace
  @namespace
end

#project_id ⇒ Object (readonly)

Returns the value of attribute project_id.



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

def project_id
  @project_id
end

#shared_cache ⇒ Object (readonly)

Returns the value of attribute shared_cache.



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

def shared_cache
  @shared_cache
end

#stats ⇒ Object (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



93
94
95
# File 'lib/prefab/client.rb', line 93

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

#channel ⇒ Object



40
41
42
43
44
45
# File 'lib/prefab/client.rb', line 40

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

#config_client(timeout: 5.0) ⇒ Object



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

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

#feature_flag_client ⇒ Object



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

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

#log ⇒ Object



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

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

#log_internal(level, msg) ⇒ Object



63
64
65
# File 'lib/prefab/client.rb', line 63

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

#ratelimit_client(timeout: 5.0) ⇒ Object



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

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

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



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

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

#reset! ⇒ Object



97
98
99
100
# File 'lib/prefab/client.rb', line 97

def reset!
  @stubs.clear
  @_channel = nil
end