Skip to content

Commit

Permalink
Merge 56933ae into 00d810b
Browse files Browse the repository at this point in the history
  • Loading branch information
ConfigCat committed Nov 18, 2019
2 parents 00d810b + 56933ae commit 3f04b5f
Show file tree
Hide file tree
Showing 14 changed files with 311 additions and 65 deletions.
1 change: 1 addition & 0 deletions configcat.gemspec
Expand Up @@ -19,6 +19,7 @@ Gem::Specification.new do |spec|
spec.required_ruby_version = "~> 2.2"

spec.add_dependency "concurrent-ruby"
spec.add_dependency "semantic"

spec.add_development_dependency "rspec", "~> 3.0"
spec.add_development_dependency "rake"
Expand Down
60 changes: 53 additions & 7 deletions lib/configcat.rb
Expand Up @@ -5,7 +5,7 @@

module ConfigCat

@logger = Logger.new(STDOUT, level: Logger::INFO)
@logger = Logger.new(STDOUT, level: Logger::WARN)
class << self
attr_accessor :logger
end
Expand All @@ -19,7 +19,16 @@ def ConfigCat.create_client(api_key)
return create_client_with_auto_poll(api_key)
end

def ConfigCat.create_client_with_auto_poll(api_key, poll_interval_seconds: 60, max_init_wait_time_seconds: 5, on_configuration_changed_callback: nil, config_cache_class: nil, base_url: nil)
def ConfigCat.create_client_with_auto_poll(api_key,
poll_interval_seconds: 60,
max_init_wait_time_seconds: 5,
on_configuration_changed_callback: nil,
config_cache_class: nil,
base_url: nil,
proxy_address:nil,
proxy_port:nil,
proxy_user:nil,
proxy_pass:nil)
#
# Create an instance of ConfigCatClient and setup Auto Poll mode with custom options
#
Expand All @@ -30,6 +39,10 @@ def ConfigCat.create_client_with_auto_poll(api_key, poll_interval_seconds: 60, m
# :param config_cache_class: If you want to use custom caching instead of the client's default InMemoryConfigCache,
# You can provide an implementation of ConfigCache.
# :param base_url: You can set a base_url if you want to use a proxy server between your application and ConfigCat
# :param proxy_address: Proxy address
# :param proxy_port: Proxy port
# :param proxy_user: username for proxy authentication
# :param proxy_pass: password for proxy authentication
#
if api_key === nil
raise ConfigCatClientException, "API Key is required."
Expand All @@ -46,10 +59,21 @@ def ConfigCat.create_client_with_auto_poll(api_key, poll_interval_seconds: 60, m
on_configuration_changed_callback: on_configuration_changed_callback,
cache_time_to_live_seconds: 0,
config_cache_class: config_cache_class,
base_url: base_url)
base_url: base_url,
proxy_address: proxy_address,
proxy_port: proxy_port,
proxy_user: proxy_user,
proxy_pass: proxy_pass)
end

def ConfigCat.create_client_with_lazy_load(api_key, cache_time_to_live_seconds: 60, config_cache_class: nil, base_url: nil)
def ConfigCat.create_client_with_lazy_load(api_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)
#
# Create an instance of ConfigCatClient and setup Lazy Load mode with custom options
#
Expand All @@ -58,6 +82,10 @@ def ConfigCat.create_client_with_lazy_load(api_key, cache_time_to_live_seconds:
# :param config_cache_class: If you want to use custom caching instead of the client's default InMemoryConfigCache,
# You can provide an implementation of ConfigCache.
# :param base_url: You can set a base_url if you want to use a proxy server between your application and ConfigCat
# :param proxy_address: Proxy address
# :param proxy_port: Proxy port
# :param proxy_user: username for proxy authentication
# :param proxy_pass: password for proxy authentication
#
if api_key === nil
raise ConfigCatClientException, "API Key is required."
Expand All @@ -71,17 +99,31 @@ def ConfigCat.create_client_with_lazy_load(api_key, cache_time_to_live_seconds:
on_configuration_changed_callback: nil,
cache_time_to_live_seconds: cache_time_to_live_seconds,
config_cache_class: config_cache_class,
base_url: base_url)
base_url: base_url,
proxy_address: proxy_address,
proxy_port: proxy_port,
proxy_user: proxy_user,
proxy_pass: proxy_pass)
end

def ConfigCat.create_client_with_manual_poll(api_key, config_cache_class: nil, base_url: nil)
def ConfigCat.create_client_with_manual_poll(api_key,
config_cache_class: nil,
base_url: nil,
proxy_address:nil,
proxy_port:nil,
proxy_user:nil,
proxy_pass:nil)
#
# Create an instance of ConfigCatClient and setup Manual Poll mode with custom options
#
# :param api_key: ConfigCat ApiKey to access your configuration.
# :param config_cache_class: If you want to use custom caching instead of the client's default InMemoryConfigCache,
# You can provide an implementation of ConfigCache.
# :param base_url: You can set a base_url if you want to use a proxy server between your application and ConfigCat
# :param proxy_address: Proxy address
# :param proxy_port: Proxy port
# :param proxy_user: username for proxy authentication
# :param proxy_pass: password for proxy authentication
#
if api_key === nil
raise ConfigCatClientException, "API Key is required."
Expand All @@ -92,7 +134,11 @@ def ConfigCat.create_client_with_manual_poll(api_key, config_cache_class: nil, b
on_configuration_changed_callback: nil,
cache_time_to_live_seconds: 0,
config_cache_class: config_cache_class,
base_url: base_url)
base_url: base_url,
proxy_address: proxy_address,
proxy_port: proxy_port,
proxy_user: proxy_user,
proxy_pass: proxy_pass)
end

end
42 changes: 25 additions & 17 deletions lib/configcat/autopollingcachepolicy.rb
Expand Up @@ -47,30 +47,38 @@ def get()
end

def force_refresh()
begin
@_lock.acquire_read_lock()
old_configuration = @_config_cache.get()
ensure
@_lock.release_read_lock()
end
begin
configuration = @_config_fetcher.get_configuration_json()

begin
@_lock.acquire_write_lock()
@_config_cache.set(configuration)
@_initialized = true
@_lock.acquire_read_lock()
old_configuration = @_config_cache.get()
ensure
@_lock.release_write_lock()
@_lock.release_read_lock()
end
begin
if !@_on_configuration_changed_callback.equal?(nil) && configuration != old_configuration
@_on_configuration_changed_callback.call()

if configuration != old_configuration
begin
@_lock.acquire_write_lock()
@_config_cache.set(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
rescue Exception => e
ConfigCat.logger.error "threw exception #{e.class}:'#{e}'"
ConfigCat.logger.error "stacktrace: #{e.backtrace}"
end
rescue StandardError => e

if !@_initialized && !old_configuration.equal?(nil)
@_initialized = true
end
rescue Exception => e
ConfigCat.logger.error("Double-check your API KEY at https://app.configcat.com/apikey.")
ConfigCat.logger.error "threw exception #{e.class}:'#{e}'"
ConfigCat.logger.error "stacktrace: #{e.backtrace}"
end
Expand Down
12 changes: 8 additions & 4 deletions lib/configcat/configcatclient.rb
Expand Up @@ -14,7 +14,11 @@ def initialize(api_key,
on_configuration_changed_callback:nil,
cache_time_to_live_seconds:60,
config_cache_class:nil,
base_url:nil)
base_url:nil,
proxy_address:nil,
proxy_port:nil,
proxy_user:nil,
proxy_pass:nil)
if api_key === nil
raise ConfigCatClientException, "API Key is required."
end
Expand All @@ -27,14 +31,14 @@ def initialize(api_key,
end

if poll_interval_seconds > 0
@_config_fetcher = CacheControlConfigFetcher.new(api_key, "p", base_url)
@_config_fetcher = CacheControlConfigFetcher.new(api_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)
else
if cache_time_to_live_seconds > 0
@_config_fetcher = CacheControlConfigFetcher.new(api_key, "l", base_url)
@_config_fetcher = CacheControlConfigFetcher.new(api_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)
else
@_config_fetcher = CacheControlConfigFetcher.new(api_key, "m", base_url)
@_config_fetcher = CacheControlConfigFetcher.new(api_key, "m", base_url, proxy_address, proxy_port, proxy_user, proxy_pass)
@_cache_policy = ManualPollingCachePolicy.new(@_config_fetcher, @_config_cache)
end
end
Expand Down
6 changes: 3 additions & 3 deletions lib/configcat/configfetcher.rb
Expand Up @@ -7,10 +7,10 @@
module ConfigCat
BASE_URL = "https://cdn.configcat.com"
BASE_PATH = "configuration-files/"
BASE_EXTENSION = "/config_v2.json"
BASE_EXTENSION = "/config_v3.json"

class CacheControlConfigFetcher < ConfigFetcher
def initialize(api_key, mode, base_url=nil)
def initialize(api_key, mode, base_url=nil, proxy_address=nil, proxy_port=nil, proxy_user=nil, proxy_pass=nil)
@_api_key = api_key
@_etag = nil
@_headers = {"User-Agent" => ((("ConfigCat-Ruby/") + mode) + ("-")) + VERSION, "X-ConfigCat-UserAgent" => ((("ConfigCat-Ruby/") + mode) + ("-")) + VERSION, "Content-Type" => "application/json"}
Expand All @@ -20,7 +20,7 @@ def initialize(api_key, mode, base_url=nil)
@_base_url = BASE_URL
end
uri = URI.parse(@_base_url)
@_http = Net::HTTP.new(uri.host, uri.port)
@_http = Net::HTTP.new(uri.host, uri.port, proxy_address, proxy_port, proxy_user, proxy_pass)
@_http.use_ssl = true if uri.scheme == 'https'
@_http.open_timeout = 10 # in seconds
@_http.read_timeout = 30 # in seconds
Expand Down
1 change: 1 addition & 0 deletions lib/configcat/lazyloadingcachepolicy.rb
Expand Up @@ -49,6 +49,7 @@ def force_refresh()
@_lock.release_write_lock()
end
rescue StandardError => e
ConfigCat.logger.error("Double-check your API KEY at https://app.configcat.com/apikey.")
ConfigCat.logger.error "threw exception #{e.class}:'#{e}'"
ConfigCat.logger.error "stacktrace: #{e.backtrace}"
end
Expand Down
1 change: 1 addition & 0 deletions lib/configcat/manualpollingcachepolicy.rb
Expand Up @@ -29,6 +29,7 @@ def force_refresh()
@_lock.release_write_lock()
end
rescue StandardError => e
ConfigCat.logger.error("Double-check your API KEY at https://app.configcat.com/apikey.")
ConfigCat.logger.error "threw exception #{e.class}:'#{e}'"
ConfigCat.logger.error "stacktrace: #{e.backtrace}"
end
Expand Down

0 comments on commit 3f04b5f

Please sign in to comment.