Skip to content

Commit

Permalink
Merge pull request #8 from configcat/globalcdn
Browse files Browse the repository at this point in the history
Globalcdn
  • Loading branch information
kp-cat authored Oct 12, 2020
2 parents 4fb6d26 + 46d1ae0 commit 101f613
Show file tree
Hide file tree
Showing 19 changed files with 612 additions and 102 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2019 ConfigCat
Copyright (c) 2020 ConfigCat

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
50 changes: 36 additions & 14 deletions lib/configcat.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@ class << self
attr_accessor :logger
end

def ConfigCat.create_client(sdk_key)
def ConfigCat.create_client(sdk_key, data_governance: DataGovernance::GLOBAL)
#
# Create an instance of ConfigCatClient and setup Auto Poll mode with default options
#
# :param sdk_key: ConfigCat SDK Key to access your configuration.
# :param data_governance:
# Default: Global. Set this parameter to be in sync with the Data Governance preference on the Dashboard:
# https://app.configcat.com/organization/data-governance
# (Only Organization Admins have access)
#
return create_client_with_auto_poll(sdk_key)
return create_client_with_auto_poll(sdk_key, data_governance: data_governance)
end

def ConfigCat.create_client_with_auto_poll(sdk_key,
Expand All @@ -25,10 +29,11 @@ def ConfigCat.create_client_with_auto_poll(sdk_key,
on_configuration_changed_callback: nil,
config_cache_class: nil,
base_url: nil,
proxy_address:nil,
proxy_port:nil,
proxy_user:nil,
proxy_pass:nil)
proxy_address: nil,
proxy_port: nil,
proxy_user: nil,
proxy_pass: nil,
data_governance: DataGovernance::GLOBAL)
#
# Create an instance of ConfigCatClient and setup Auto Poll mode with custom options
#
Expand All @@ -43,6 +48,10 @@ def ConfigCat.create_client_with_auto_poll(sdk_key,
# :param proxy_port: Proxy port
# :param proxy_user: username for proxy authentication
# :param proxy_pass: password for proxy authentication
# :param data_governance:
# Default: Global. Set this parameter to be in sync with the Data Governance preference on the Dashboard:
# https://app.configcat.com/organization/data-governance
# (Only Organization Admins have access)
#
if sdk_key === nil
raise ConfigCatClientException, "SDK Key is required."
Expand All @@ -63,17 +72,19 @@ def ConfigCat.create_client_with_auto_poll(sdk_key,
proxy_address: proxy_address,
proxy_port: proxy_port,
proxy_user: proxy_user,
proxy_pass: proxy_pass)
proxy_pass: proxy_pass,
data_governance: data_governance)
end

def ConfigCat.create_client_with_lazy_load(sdk_key,
cache_time_to_live_seconds: 60,
config_cache_class: nil,
base_url: nil,
proxy_address:nil,
proxy_port:nil,
proxy_user:nil,
proxy_pass:nil)
proxy_address: nil,
proxy_port: nil,
proxy_user: nil,
proxy_pass: nil,
data_governance: DataGovernance::GLOBAL)
#
# Create an instance of ConfigCatClient and setup Lazy Load mode with custom options
#
Expand All @@ -86,6 +97,10 @@ def ConfigCat.create_client_with_lazy_load(sdk_key,
# :param proxy_port: Proxy port
# :param proxy_user: username for proxy authentication
# :param proxy_pass: password for proxy authentication
# :param data_governance:
# Default: Global. Set this parameter to be in sync with the Data Governance preference on the Dashboard:
# https://app.configcat.com/organization/data-governance
# (Only Organization Admins have access)
#
if sdk_key === nil
raise ConfigCatClientException, "SDK Key is required."
Expand All @@ -103,7 +118,8 @@ def ConfigCat.create_client_with_lazy_load(sdk_key,
proxy_address: proxy_address,
proxy_port: proxy_port,
proxy_user: proxy_user,
proxy_pass: proxy_pass)
proxy_pass: proxy_pass,
data_governance: data_governance)
end

def ConfigCat.create_client_with_manual_poll(sdk_key,
Expand All @@ -112,7 +128,8 @@ def ConfigCat.create_client_with_manual_poll(sdk_key,
proxy_address:nil,
proxy_port:nil,
proxy_user:nil,
proxy_pass:nil)
proxy_pass:nil,
data_governance: DataGovernance::GLOBAL)
#
# Create an instance of ConfigCatClient and setup Manual Poll mode with custom options
#
Expand All @@ -124,6 +141,10 @@ def ConfigCat.create_client_with_manual_poll(sdk_key,
# :param proxy_port: Proxy port
# :param proxy_user: username for proxy authentication
# :param proxy_pass: password for proxy authentication
# :param data_governance:
# Default: Global. Set this parameter to be in sync with the Data Governance preference on the Dashboard:
# https://app.configcat.com/organization/data-governance
# (Only Organization Admins have access)
#
if sdk_key === nil
raise ConfigCatClientException, "SDK Key is required."
Expand All @@ -138,7 +159,8 @@ def ConfigCat.create_client_with_manual_poll(sdk_key,
proxy_address: proxy_address,
proxy_port: proxy_port,
proxy_user: proxy_user,
proxy_pass: proxy_pass)
proxy_pass: proxy_pass,
data_governance: data_governance)
end

end
10 changes: 6 additions & 4 deletions lib/configcat/autopollingcachepolicy.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
require 'configcat/interfaces'
require 'configcat/constants'
require 'concurrent'

module ConfigCat
class AutoPollingCachePolicy < CachePolicy
def initialize(config_fetcher, config_cache, poll_interval_seconds=60, max_init_wait_time_seconds=5, on_configuration_changed_callback=nil)
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
Expand All @@ -12,6 +13,7 @@ def initialize(config_fetcher, config_cache, poll_interval_seconds=60, max_init_
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
Expand Down Expand Up @@ -40,7 +42,7 @@ def get()
end
begin
@_lock.acquire_read_lock()
return @_config_cache.get()
return @_config_cache.get(@_cache_key)
ensure
@_lock.release_read_lock()
end
Expand All @@ -52,7 +54,7 @@ def force_refresh()

begin
@_lock.acquire_read_lock()
old_configuration = @_config_cache.get()
old_configuration = @_config_cache.get(@_cache_key)
ensure
@_lock.release_read_lock()
end
Expand All @@ -62,7 +64,7 @@ def force_refresh()
if configuration != old_configuration
begin
@_lock.acquire_write_lock()
@_config_cache.set(configuration)
@_config_cache.set(@_cache_key, configuration)
@_initialized = true
ensure
@_lock.release_write_lock()
Expand Down
10 changes: 5 additions & 5 deletions lib/configcat/configcache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
module ConfigCat
class InMemoryConfigCache < ConfigCache
def initialize()
@_value = nil
@_value = {}
end

def get()
return @_value
def get(key)
return @_value.fetch(key, nil)
end

def set(value)
@_value = value
def set(key, value)
@_value[key] = value
end
end
end
53 changes: 36 additions & 17 deletions lib/configcat/configcatclient.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
require 'configcat/manualpollingcachepolicy'
require 'configcat/lazyloadingcachepolicy'
require 'configcat/rolloutevaluator'
require 'configcat/datagovernance'

module ConfigCat
KeyValue = Struct.new(:key, :value)
Expand All @@ -19,7 +20,8 @@ def initialize(sdk_key,
proxy_address:nil,
proxy_port:nil,
proxy_user:nil,
proxy_pass:nil)
proxy_pass:nil,
data_governance: DataGovernance::GLOBAL)
if sdk_key === nil
raise ConfigCatClientException, "SDK Key is required."
end
Expand All @@ -32,15 +34,15 @@ def initialize(sdk_key,
end

if poll_interval_seconds > 0
@_config_fetcher = CacheControlConfigFetcher.new(sdk_key, "p", base_url, proxy_address, proxy_port, proxy_user, proxy_pass)
@_cache_policy = AutoPollingCachePolicy.new(@_config_fetcher, @_config_cache, poll_interval_seconds, max_init_wait_time_seconds, on_configuration_changed_callback)
@_config_fetcher = CacheControlConfigFetcher.new(sdk_key, "p", base_url, proxy_address, proxy_port, proxy_user, proxy_pass, data_governance)
@_cache_policy = AutoPollingCachePolicy.new(@_config_fetcher, @_config_cache, _get_cache_key(), poll_interval_seconds, max_init_wait_time_seconds, on_configuration_changed_callback)
else
if cache_time_to_live_seconds > 0
@_config_fetcher = CacheControlConfigFetcher.new(sdk_key, "l", base_url, proxy_address, proxy_port, proxy_user, proxy_pass)
@_cache_policy = LazyLoadingCachePolicy.new(@_config_fetcher, @_config_cache, cache_time_to_live_seconds)
@_config_fetcher = CacheControlConfigFetcher.new(sdk_key, "l", base_url, proxy_address, proxy_port, proxy_user, proxy_pass, data_governance)
@_cache_policy = LazyLoadingCachePolicy.new(@_config_fetcher, @_config_cache, _get_cache_key(), cache_time_to_live_seconds)
else
@_config_fetcher = CacheControlConfigFetcher.new(sdk_key, "m", base_url, proxy_address, proxy_port, proxy_user, proxy_pass)
@_cache_policy = ManualPollingCachePolicy.new(@_config_fetcher, @_config_cache)
@_config_fetcher = CacheControlConfigFetcher.new(sdk_key, "m", base_url, proxy_address, proxy_port, proxy_user, proxy_pass, data_governance)
@_cache_policy = ManualPollingCachePolicy.new(@_config_fetcher, @_config_cache, _get_cache_key())
end
end
end
Expand All @@ -59,7 +61,11 @@ def get_all_keys()
if config === nil
return []
end
return config.keys
feature_flags = config.fetch(FEATURE_FLAGS, nil)
if feature_flags === nil
return []
end
return feature_flags.keys
end

def get_variation_id(key, default_variation_id, user=nil)
Expand Down Expand Up @@ -92,22 +98,29 @@ def get_key_and_value(variation_id)
ConfigCat.logger.warn("Evaluating get_variation_id('%s') failed. Cache is empty. Returning nil." % variation_id)
return nil
end
for key, value in config
if variation_id == value.fetch(RolloutEvaluator::VARIATION_ID, nil)
return KeyValue.new(key, value[RolloutEvaluator::VALUE])

feature_flags = config.fetch(FEATURE_FLAGS, nil)
if feature_flags === nil
ConfigCat.logger.warn("Evaluating get_key_and_value('%s') failed. Cache is empty. Returning None." % variation_id)
return nil
end

for key, value in feature_flags
if variation_id == value.fetch(VARIATION_ID, nil)
return KeyValue.new(key, value[VALUE])
end

rollout_rules = value.fetch(RolloutEvaluator::ROLLOUT_RULES, [])
rollout_rules = value.fetch(ROLLOUT_RULES, [])
for rollout_rule in rollout_rules
if variation_id == rollout_rule.fetch(RolloutEvaluator::VARIATION_ID, nil)
return KeyValue.new(key, rollout_rule[RolloutEvaluator::VALUE])
if variation_id == rollout_rule.fetch(VARIATION_ID, nil)
return KeyValue.new(key, rollout_rule[VALUE])
end
end

rollout_percentage_items = value.fetch(RolloutEvaluator::ROLLOUT_PERCENTAGE_ITEMS, [])
rollout_percentage_items = value.fetch(ROLLOUT_PERCENTAGE_ITEMS, [])
for rollout_percentage_item in rollout_percentage_items
if variation_id == rollout_percentage_item.fetch(RolloutEvaluator::VARIATION_ID, nil)
return KeyValue.new(key, rollout_percentage_item[RolloutEvaluator::VALUE])
if variation_id == rollout_percentage_item.fetch(VARIATION_ID, nil)
return KeyValue.new(key, rollout_percentage_item[VALUE])
end
end
end
Expand All @@ -122,5 +135,11 @@ def stop()
@_config_fetcher.close()
end

private

def _get_cache_key()
return Digest::SHA1.hexdigest("ruby_" + CONFIG_FILE_NAME + "_" + @_sdk_key)
end

end
end
Loading

0 comments on commit 101f613

Please sign in to comment.