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



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

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
# File 'lib/configcat/configservice.rb', line 33

def get_config
  if @polling_mode.is_a?(LazyLoadingMode)
    entry, _ = fetch_if_older(Utils.get_utc_now_seconds_since_epoch - @polling_mode.cache_refresh_interval_seconds)
    return !entry.empty? ?
      [entry.config, entry.fetch_time] :
      [nil, Utils::DISTANT_PAST]
  elsif @polling_mode.is_a?(AutoPollingMode) && !@initialized.set?
    elapsed_time = Utils.get_utc_now_seconds_since_epoch - @start_time # Elapsed time in 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(Utils::DISTANT_PAST, prefer_cache: @initialized.set?)
  return !entry.empty? ?
    [entry.config, entry.fetch_time] :
    [nil, Utils::DISTANT_PAST]
end

#offline?Boolean

Returns:

  • (Boolean)


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

def offline?
  return @is_offline
end

#refreshObject

:return [RefreshResult]



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

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



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

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



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

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