Class: Prefab::ConfigClient

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

Constant Summary collapse

RECONNECT_WAIT =
5
SUSPENDERS_RESET_LOCK_SEC =
20
DEFAULT_CHECKPOINT_FREQ_SEC =
10
DEFAULT_MAX_SUSPENDERS_AGE_SEC =
60

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_client, timeout) ⇒ ConfigClient

Returns a new instance of ConfigClient.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/prefab/config_client.rb', line 8

def initialize(base_client, timeout)
  @base_client = base_client
  @timeout = timeout
  @initialization_lock = Concurrent::ReadWriteLock.new

  @suspenders_max_age_secs = (ENV["PREFAB_SUSPENDERS_MAX_AGE_SEC"] || DEFAULT_MAX_SUSPENDERS_AGE_SEC)
  @checkpoint_freq_secs = (ENV["PREFAB_DEFAULT_CHECKPOINT_FREQ_SEC"] || DEFAULT_CHECKPOINT_FREQ_SEC)

  @config_loader = Prefab::ConfigLoader.new(@base_client)
  @config_resolver = Prefab::ConfigResolver.new(@base_client, @config_loader)

  @initialization_lock.acquire_write_lock

  load_or_save_checkpoint
  ensure_api_connection_started
  start_checkpointing_thread if has_real_cache?
end

Class Method Details

.value_to_delta(key, config_value, namespace = nil) ⇒ Object



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

def self.value_to_delta(key, config_value, namespace = nil)
  Prefab::ConfigDelta.new(key: [namespace, key].compact.join(":"),
                          value: config_value)
end

Instance Method Details

#get(prop) ⇒ Object



26
27
28
29
30
# File 'lib/prefab/config_client.rb', line 26

def get(prop)
  @initialization_lock.with_read_lock do
    @config_resolver.get(prop)
  end
end

#reset ⇒ Object



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

def reset
  @base_client.reset!
  @_stub = nil
end

#to_s ⇒ Object



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

def to_s
  @config_resolver.to_s
end

#upsert(key, config_value, namespace = nil, previous_key = nil) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/prefab/config_client.rb', line 32

def upsert(key, config_value, namespace = nil, previous_key = nil)
  raise "Key must not contain ':' set namespaces separately" if key.include? ":"
  raise "Namespace must not contain ':'" if namespace&.include?(":")
  config_delta = Prefab::ConfigClient.value_to_delta(key, config_value, namespace)
  upsert_req = Prefab::UpsertRequest.new(config_delta: config_delta)
  upsert_req.previous_key = previous_key if previous_key&.present?

  @base_client.request Prefab::ConfigService, :upsert, req_options: { timeout: @timeout }, params: upsert_req
  @base_client.stats.increment("prefab.config.upsert")
  @config_loader.set(config_delta)
  @config_loader.rm(previous_key) if previous_key&.present?
  @config_resolver.update
end