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

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: 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
# 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)
  @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
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



84
85
86
# File 'lib/prefab/client.rb', line 84

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

#channel ⇒ Object



31
32
33
34
35
36
# File 'lib/prefab/client.rb', line 31

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



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

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

#feature_flag_client ⇒ Object



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

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

#log ⇒ Object



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

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

#log_internal(level, msg) ⇒ Object



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

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

#ratelimit_client(timeout: 5.0) ⇒ Object



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

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

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



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/prefab/client.rb', line 58

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