Class: ConfigCat::ConfigService

Inherits:
Object
  • Object
show all
Defined in:
lib/configcat/configservice.rb

Instance Method Summary collapse

Constructor Details

#initialize(sdk_key, polling_mode, hooks, config_fetcher, log, config_cache, is_offline) ⇒ ConfigService

Returns a new instance of ConfigService.



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/configcat/configservice.rb', line 9

def initialize(sdk_key, polling_mode, hooks, config_fetcher, log, config_cache, is_offline)
  @cached_entry = ConfigEntry::EMPTY
  @cached_entry_string = ''
  @polling_mode = polling_mode
  @log = log
  @config_cache = config_cache
  @hooks = hooks
  @cache_key = ConfigService.get_cache_key(sdk_key)
  @config_fetcher = config_fetcher
  @is_offline = is_offline
  @response_future = nil
  @initialized = Concurrent::Event.new
  @lock = Mutex.new
  @ongoing_fetch = false
  @fetch_finished = Concurrent::Event.new
  @start_time = Utils.get_utc_now_seconds_since_epoch

  if @polling_mode.is_a?(AutoPollingMode) && !@is_offline
    start_poll
  else
    set_initialized
  end
end

Instance Method Details

#closeObject



108
109
110
111
112
# File 'lib/configcat/configservice.rb', line 108

def close
  if @polling_mode.is_a?(AutoPollingMode)
    @stopped.set
  end
end

#get_configObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/configcat/configservice.rb', line 33

def get_config
  threshold = Utils::DISTANT_PAST
  prefer_cached = @initialized.set?
  if @polling_mode.is_a?(LazyLoadingMode)
    threshold = Utils.get_utc_now_seconds_since_epoch - @polling_mode.cache_refresh_interval_seconds
    prefer_cached = false
  elsif @polling_mode.is_a?(AutoPollingMode) && !@initialized.set?
    elapsed_time = Utils.get_utc_now_seconds_since_epoch - @start_time # Elapsed time in seconds
    threshold = Utils.get_utc_now_seconds_since_epoch - @polling_mode.poll_interval_seconds
    if elapsed_time < @polling_mode.max_init_wait_time_seconds
      @initialized.wait(@polling_mode.max_init_wait_time_seconds - elapsed_time)

      # Max wait time expired without result, notify subscribers with the cached config.
      if !@initialized.set?
        set_initialized
        return !@cached_entry.empty? ?
          [@cached_entry.config, @cached_entry.fetch_time] :
          [nil, Utils::DISTANT_PAST]
      end
    end
  end

  # If we are initialized, we prefer the cached results
  entry, _ = fetch_if_older(threshold, prefer_cached: prefer_cached)
  return !entry.empty? ?
    [entry.config, entry.fetch_time] :
    [nil, Utils::DISTANT_PAST]
end

#offline?Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/configcat/configservice.rb', line 104

def offline?
  return @is_offline
end

#refreshObject

:return [RefreshResult]



63
64
65
66
67
68
69
70
71
72
# File 'lib/configcat/configservice.rb', line 63

def refresh
  if offline?
    offline_warning = "Client is in offline mode, it cannot initiate HTTP calls."
    @log.warn(3200, offline_warning)
    return RefreshResult.new(success = false, error = offline_warning)
  end

  _, error = fetch_if_older(Utils::DISTANT_FUTURE)
  return RefreshResult.new(success = error.nil?, error = error)
end

#set_offlineObject



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/configcat/configservice.rb', line 88

def set_offline
  @lock.synchronize do
    if @is_offline
      return
    end

    @is_offline = true
    if @polling_mode.is_a?(AutoPollingMode)
      @stopped.set
      @thread.join
    end

    @log.info(5200, "Switched to OFFLINE mode.")
  end
end

#set_onlineObject



74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/configcat/configservice.rb', line 74

def set_online
  @lock.synchronize do
    if !@is_offline
      return
    end

    @is_offline = false
    if @polling_mode.is_a?(AutoPollingMode)
      start_poll
    end
    @log.info(5200, "Switched to ONLINE mode.")
  end
end