Skip to content

Commit

Permalink
sdk_keys singleton check
Browse files Browse the repository at this point in the history
  • Loading branch information
kp-cat committed Mar 31, 2022
1 parent 6f0e812 commit e463f89
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
12 changes: 12 additions & 0 deletions lib/configcat/configcatclient.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
module ConfigCat
KeyValue = Struct.new(:key, :value)
class ConfigCatClient
@@sdk_keys = []

def initialize(sdk_key,
poll_interval_seconds:60,
max_init_wait_time_seconds:5,
Expand All @@ -27,6 +29,15 @@ def initialize(sdk_key,
if sdk_key === nil
raise ConfigCatClientException, "SDK Key is required."
end

if @@sdk_keys.include?(sdk_key)
ConfigCat.logger.warn("A ConfigCat Client is already initialized with sdk_key %s. "\
"We strongly recommend you to use the ConfigCat Client as "\
"a Singleton object in your application." % sdk_key)
else
@@sdk_keys.push(sdk_key)
end

@_sdk_key = sdk_key

if config_cache_class
Expand Down Expand Up @@ -144,6 +155,7 @@ def force_refresh()
def stop()
@_cache_policy.stop()
@_config_fetcher.close()
@@sdk_keys.delete(@_sdk_key)
end

private
Expand Down
4 changes: 3 additions & 1 deletion samples/consolesample2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@
puts("'isPOCFeatureEnabled' value from ConfigCat: " + value.to_s)

value = client.get_value("isAwesomeFeatureEnabled", "default value")
puts("'isAwesomeFeatureEnabled' value from ConfigCat: " + value.to_s)
puts("'isAwesomeFeatureEnabled' value from ConfigCat: " + value.to_s)

client.stop()

0 comments on commit e463f89

Please sign in to comment.