Class: Featureflow::PollingClient

Inherits:
Object
  • Object
show all
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

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, options = {})
  @etag = ''
  @url = url
  @api_key = api_key
  @options = DEFAULT_OPTIONS.merge(options)
  @features = {}

  load_features

  @thread = Thread.new do
    loop do
      sleep @options[: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

#finishObject



29
30
31
# File 'lib/featureflow/polling_client.rb', line 29

def finish
  @thread.exit
end

#load_featuresObject



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: @options[: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