Class: ConfigCat::AutoPollingCachePolicy
- Inherits:
-
CachePolicy
- Object
- CachePolicy
- ConfigCat::AutoPollingCachePolicy
- Defined in:
- lib/configcat/autopollingcachepolicy.rb
Instance Method Summary collapse
- #_run ⇒ Object
- #force_refresh ⇒ Object
- #get ⇒ Object
-
#initialize(config_fetcher, config_cache, cache_key, poll_interval_seconds = 60, max_init_wait_time_seconds = 5, on_configuration_changed_callback = nil) ⇒ AutoPollingCachePolicy
constructor
A new instance of AutoPollingCachePolicy.
- #stop ⇒ Object
Constructor Details
#initialize(config_fetcher, config_cache, cache_key, poll_interval_seconds = 60, max_init_wait_time_seconds = 5, on_configuration_changed_callback = nil) ⇒ AutoPollingCachePolicy
Returns a new instance of AutoPollingCachePolicy.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/configcat/autopollingcachepolicy.rb', line 7 def initialize(config_fetcher, config_cache, cache_key, poll_interval_seconds=60, max_init_wait_time_seconds=5, on_configuration_changed_callback=nil) if poll_interval_seconds < 1 poll_interval_seconds = 1 end if max_init_wait_time_seconds < 0 max_init_wait_time_seconds = 0 end @_config_fetcher = config_fetcher @_config_cache = config_cache @_cache_key = cache_key @_poll_interval_seconds = poll_interval_seconds @_max_init_wait_time_seconds = max_init_wait_time_seconds @_on_configuration_changed_callback = on_configuration_changed_callback @_initialized = false @_is_running = false @_start_time = Time.now.utc @_lock = Concurrent::ReadWriteLock.new() @_is_started = Concurrent::Event.new() @thread = Thread.new{_run()} @_is_started.wait() end |
Instance Method Details
#_run ⇒ Object
29 30 31 32 33 34 35 36 37 |
# File 'lib/configcat/autopollingcachepolicy.rb', line 29 def _run() @_is_running = true @_is_started.set() loop do force_refresh() sleep(@_poll_interval_seconds) break if !@_is_running end end |
#force_refresh ⇒ Object
51 52 53 54 55 56 57 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 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/configcat/autopollingcachepolicy.rb', line 51 def force_refresh() begin configuration_response = @_config_fetcher.get_configuration_json() begin @_lock.acquire_read_lock() old_configuration = @_config_cache.get(@_cache_key) ensure @_lock.release_read_lock() end if configuration_response.is_fetched() configuration = configuration_response.json() if configuration != old_configuration begin @_lock.acquire_write_lock() @_config_cache.set(@_cache_key, configuration) @_initialized = true ensure @_lock.release_write_lock() end begin if !@_on_configuration_changed_callback.equal?(nil) @_on_configuration_changed_callback.() end rescue Exception => e ConfigCat.logger.error("Exception in on_configuration_changed_callback: #{e.class}:'#{e}'") end end end if !@_initialized && !old_configuration.equal?(nil) @_initialized = true end rescue Timeout::Error => e ConfigCat.logger.error("Request timed out. Timeout values: [open: %ss, read: %ss]" % [@_config_fetcher.get_open_timeout(), @_config_fetcher.get_read_timeout()]) rescue Exception => e ConfigCat.logger.error("Double-check your SDK Key at https://app.configcat.com/sdkkey.") ConfigCat.logger.error "threw exception #{e.class}:'#{e}'" ConfigCat.logger.error "stacktrace: #{e.backtrace}" end end |
#get ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/configcat/autopollingcachepolicy.rb', line 39 def get() while !@_initialized && (Time.now.utc < @_start_time + @_max_init_wait_time_seconds) sleep(0.5) end begin @_lock.acquire_read_lock() return @_config_cache.get(@_cache_key) ensure @_lock.release_read_lock() end end |
#stop ⇒ Object
95 96 97 |
# File 'lib/configcat/autopollingcachepolicy.rb', line 95 def stop() @_is_running = false end |