Class: Prefab::ConfigClient

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

Constant Summary collapse

LOG =
Prefab::InternalLogger.new(self)
DEFAULT_CHECKPOINT_FREQ_SEC =
60
STALE_CACHE_WARN_HOURS =
5

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_client, timeout) ⇒ ConfigClient

Returns a new instance of ConfigClient.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/prefab/config_client.rb', line 9

def initialize(base_client, timeout)
  @base_client = base_client
  @options = base_client.options
  LOG.debug 'Initialize ConfigClient'
  @timeout = timeout

  @checkpoint_freq_secs = DEFAULT_CHECKPOINT_FREQ_SEC

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

  @initialization_lock = Concurrent::CountDownLatch.new(1)

  if @options.local_only?
    finish_init!(:local_only, nil)
  elsif @options.datafile?
    load_json_file(@options.datafile)
  else
    load_checkpoint
    start_checkpointing_thread
    start_streaming
  end
end

Class Method Details

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



65
66
67
68
# File 'lib/prefab/config_client.rb', line 65

def self.value_to_delta(key, config_value, namespace = nil)
  PrefabProto::Config.new(key: [namespace, key].compact.join(':'),
                          rows: [PrefabProto::ConfigRow.new(value: config_value)])
end

Instance Method Details

#get(key, default = NO_DEFAULT_PROVIDED, properties = NO_DEFAULT_PROVIDED) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/prefab/config_client.rb', line 70

def get(key, default = NO_DEFAULT_PROVIDED, properties = NO_DEFAULT_PROVIDED)
  context = @config_resolver.make_context(properties)

  if !context.blank? && @base_client.example_contexts_aggregator
    @base_client.example_contexts_aggregator.record(context)
  end

  evaluation = _get(key, properties)

  @base_client.context_shape_aggregator&.push(context)

  if evaluation
    evaluation.report_and_return(@base_client.evaluation_summary_aggregator)
  else
    handle_default(key, default)
  end
end

#initialized?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/prefab/config_client.rb', line 88

def initialized?
  @initialization_lock.count <= 0
end

#resolverObject



61
62
63
# File 'lib/prefab/config_client.rb', line 61

def resolver
  @config_resolver
end

#start_streamingObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/prefab/config_client.rb', line 37

def start_streaming
  Thread.new do
    # wait for the config loader to have a highwater mark before we connect the SSE
    loop do
      break if @config_loader.highwater_mark > 0

      sleep 0.1
    end

    stream_lock = Concurrent::ReadWriteLock.new
    @sse_config_client = Prefab::SSEConfigClient.new(@options, @config_loader)

    @sse_config_client.start do |configs, _event, source|
      stream_lock.with_write_lock do
        load_configs(configs, source)
      end
    end
  end
end

#stopObject



33
34
35
# File 'lib/prefab/config_client.rb', line 33

def stop
  @sse_config_client&.close
end

#to_sObject



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

def to_s
  @config_resolver.to_s
end