Class: Featureflow::PollingClient
- Inherits:
-
Object
- Object
- Featureflow::PollingClient
- Defined in:
- lib/featureflow/polling_client.rb
Constant Summary collapse
- DEFAULT_OPTIONS =
{ poll_interval: 30, timeout: 30 }.freeze
- LOCK =
Mutex.new
Instance Method Summary collapse
- #feature(key) ⇒ Object
- #finish ⇒ Object
-
#initialize(url, api_key, options = {}) ⇒ PollingClient
constructor
A new instance of PollingClient.
- #load_features ⇒ Object
Constructor Details
#initialize(url, api_key, options = {}) ⇒ PollingClient
Returns a new instance of PollingClient.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/featureflow/polling_client.rb', line 12 def initialize(url, api_key, = {}) @etag = '' @url = url @api_key = api_key = DEFAULT_OPTIONS.merge() @features = {} load_features @thread = Thread.new do loop do sleep [:poll_interval] load_features end end end |
Instance Method Details
#feature(key) ⇒ Object
33 34 35 |
# File 'lib/featureflow/polling_client.rb', line 33 def feature(key) LOCK.synchronize { @features[key] } end |
#finish ⇒ Object
29 30 31 |
# File 'lib/featureflow/polling_client.rb', line 29 def finish @thread.exit end |
#load_features ⇒ Object
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/featureflow/polling_client.rb', line 37 def load_features response = Excon.get(@url + + '/api/sdk/v1/features', headers: { 'Authorization' => "Bearer #{@api_key}", 'Accept' => 'Application/Json', 'If-None-Match' => @etag, 'X-Featureflow-Client' => 'RubyClient/' + Featureflow::VERSION }, omit_default_port: true, read_timeout: [:timeout]) if response.status == 200 Featureflow.logger.debug "updating features" @etag = response.headers['ETag'] features = JSON.parse(response.body) LOCK.synchronize { @features = features } elsif response.status >= 400 Featureflow.logger.error "request for features failed with response status #{response.status}" Featureflow.logger.error response.to_s end rescue => e Featureflow.logger.error e.inspect end |