Class: Flipper::Poller

Inherits:
Object
  • Object
show all
Defined in:
lib/flipper/poller.rb

Constant Summary collapse

MINIMUM_POLL_INTERVAL =
10

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Poller

Returns a new instance of Poller.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/flipper/poller.rb', line 26

def initialize(options = {})
  @thread = nil
  @pid = Process.pid
  @mutex = Mutex.new
  @instrumenter = options.fetch(:instrumenter, Instrumenters::Noop)
  @remote_adapter = options.fetch(:remote_adapter)
  @last_synced_at = Concurrent::AtomicFixnum.new(0)
  @adapter = Adapters::Memory.new(nil, threadsafe: true)
  @shutdown_requested = Concurrent::AtomicBoolean.new(false)

  self.interval = options.fetch(:interval, 10)
  @initial_interval = @interval

  @start_automatically = options.fetch(:start_automatically, true)

  if options.fetch(:shutdown_automatically, true)
    at_exit { stop }
  end
end

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter.



9
10
11
# File 'lib/flipper/poller.rb', line 9

def adapter
  @adapter
end

#intervalObject

Returns the value of attribute interval.



9
10
11
# File 'lib/flipper/poller.rb', line 9

def interval
  @interval
end

#last_synced_atObject (readonly)

Returns the value of attribute last_synced_at.



9
10
11
# File 'lib/flipper/poller.rb', line 9

def last_synced_at
  @last_synced_at
end

#mutexObject (readonly)

Returns the value of attribute mutex.



9
10
11
# File 'lib/flipper/poller.rb', line 9

def mutex
  @mutex
end

#pidObject (readonly)

Returns the value of attribute pid.



9
10
11
# File 'lib/flipper/poller.rb', line 9

def pid
  @pid
end

#threadObject (readonly)

Returns the value of attribute thread.



9
10
11
# File 'lib/flipper/poller.rb', line 9

def thread
  @thread
end

Class Method Details

.get(key, options = {}) ⇒ Object



16
17
18
# File 'lib/flipper/poller.rb', line 16

def self.get(key, options = {})
  instances.compute_if_absent(key) { new(options) }
end

.resetObject



20
21
22
# File 'lib/flipper/poller.rb', line 20

def self.reset
  instances.each {|_, instance| instance.stop }.clear
end

Instance Method Details

#runObject



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/flipper/poller.rb', line 59

def run
  loop do
    sleep jitter

    begin
      sync
    rescue
      # you can instrument these using poller.flipper
    end

    sleep interval
  end
end

#startObject



46
47
48
49
50
# File 'lib/flipper/poller.rb', line 46

def start
  reset if forked?
  return if @shutdown_requested.true?
  ensure_worker_running
end

#stopObject



52
53
54
55
56
57
# File 'lib/flipper/poller.rb', line 52

def stop
  @instrumenter.instrument("poller.#{InstrumentationNamespace}", {
    operation: :stop,
  })
  @thread&.kill
end

#syncObject



73
74
75
76
77
78
79
80
81
82
# File 'lib/flipper/poller.rb', line 73

def sync
  @instrumenter.instrument("poller.#{InstrumentationNamespace}", operation: :poll) do
    begin
      @adapter.import @remote_adapter
      @last_synced_at.update { |time| Concurrent.monotonic_time }
    ensure
      apply_response_headers
    end
  end
end