Class: Flipper::Cloud::Configuration

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

Constant Summary collapse

VALID_SYNC_METHODS =

The set of valid ways that syncing can happpen.

Set[
  :poll,
  :webhook,
].freeze
DEFAULT_URL =
"https://www.flippercloud.io/adapter".freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Configuration

Returns a new instance of Configuration.



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/flipper/cloud/configuration.rb', line 76

def initialize(options = {})
  @token = options.fetch(:token) { ENV["FLIPPER_CLOUD_TOKEN"] }

  if @token.nil?
    raise ArgumentError, "Flipper::Cloud token is missing. Please set FLIPPER_CLOUD_TOKEN or provide the token (e.g. Flipper::Cloud.new(token: 'token'))."
  end

  @read_timeout = options.fetch(:read_timeout) { ENV.fetch("FLIPPER_CLOUD_READ_TIMEOUT", 5).to_f }
  @open_timeout = options.fetch(:open_timeout) { ENV.fetch("FLIPPER_CLOUD_OPEN_TIMEOUT", 5).to_f }
  @write_timeout = options.fetch(:write_timeout) { ENV.fetch("FLIPPER_CLOUD_WRITE_TIMEOUT", 5).to_f }
  @sync_interval = options.fetch(:sync_interval) { ENV.fetch("FLIPPER_CLOUD_SYNC_INTERVAL", 10).to_f }
  @sync_secret = options.fetch(:sync_secret) { ENV["FLIPPER_CLOUD_SYNC_SECRET"] }
  @local_adapter = options.fetch(:local_adapter) { Adapters::Memory.new }
  @debug_output = options[:debug_output]
  @adapter_block = ->(adapter) { adapter }
  self.url = options.fetch(:url) { ENV.fetch("FLIPPER_CLOUD_URL", DEFAULT_URL) }

  instrumenter = options.fetch(:instrumenter, Instrumenters::Noop)

  # This is alpha. Don't use this unless you are me. And you are not me.
  cloud_instrument = options.fetch(:cloud_instrument) { ENV["FLIPPER_CLOUD_INSTRUMENT"] == "1" }
  @instrumenter = if cloud_instrument
    Instrumenter.new(brow: brow, instrumenter: instrumenter)
  else
    instrumenter
  end
end

Instance Attribute Details

#debug_outputObject

Public: IO stream to send debug output too. Off by default.

# for example, this would send all http request information to STDOUT
configuration = Flipper::Cloud::Configuration.new
configuration.debug_output = STDOUT


49
50
51
# File 'lib/flipper/cloud/configuration.rb', line 49

def debug_output
  @debug_output
end

#instrumenterObject

Public: Instrumenter to use for the Flipper instance returned by

       Flipper::Cloud.new (default: Flipper::Instrumenters::Noop).

# for example, to use active support notifications you could do:
configuration = Flipper::Cloud::Configuration.new
configuration.instrumenter = ActiveSupport::Notifications


57
58
59
# File 'lib/flipper/cloud/configuration.rb', line 57

def instrumenter
  @instrumenter
end

#local_adapterObject

Public: Local adapter that all reads should go to in order to ensure latency is low and resiliency is high. This adapter is automatically kept in sync with cloud.

# for example, to use active record you could do:
configuration = Flipper::Cloud::Configuration.new
configuration.local_adapter = Flipper::Adapters::ActiveRecord.new


66
67
68
# File 'lib/flipper/cloud/configuration.rb', line 66

def local_adapter
  @local_adapter
end

#open_timeoutObject

Public: net/http open timeout for all http requests (default: 5).



39
40
41
# File 'lib/flipper/cloud/configuration.rb', line 39

def open_timeout
  @open_timeout
end

#read_timeoutObject

Public: net/http read timeout for all http requests (default: 5).



36
37
38
# File 'lib/flipper/cloud/configuration.rb', line 36

def read_timeout
  @read_timeout
end

#sync_intervalObject

Public: The Integer or Float number of seconds between attempts to bring the local in sync with cloud (default: 10).



70
71
72
# File 'lib/flipper/cloud/configuration.rb', line 70

def sync_interval
  @sync_interval
end

#sync_secretObject

Public: The secret used to verify if syncs in the middleware should occur or not.



74
75
76
# File 'lib/flipper/cloud/configuration.rb', line 74

def sync_secret
  @sync_secret
end

#tokenObject

Public: The token corresponding to an environment on flippercloud.io.



29
30
31
# File 'lib/flipper/cloud/configuration.rb', line 29

def token
  @token
end

#urlObject

development work. Feel free to forget you ever saw this.



33
34
35
# File 'lib/flipper/cloud/configuration.rb', line 33

def url
  @url
end

#write_timeoutObject

Public: net/http write timeout for all http requests (default: 5).



42
43
44
# File 'lib/flipper/cloud/configuration.rb', line 42

def write_timeout
  @write_timeout
end

Class Method Details

.brow_instancesObject

Private: Keeps track of brow instances so they can be shared across threads.



24
25
26
# File 'lib/flipper/cloud/configuration.rb', line 24

def self.brow_instances
  @brow_instances ||= Concurrent::Map.new
end

Instance Method Details

#adapter(&block) ⇒ Object

Public: Read or customize the http adapter. Calling without a block will perform a read. Calling with a block yields the cloud adapter for customization.

# for example, to instrument the http calls, you can wrap the http
# adapter with the intsrumented adapter
configuration = Flipper::Cloud::Configuration.new
configuration.adapter do |adapter|
  Flipper::Adapters::Instrumented.new(adapter)
end


115
116
117
118
119
120
121
# File 'lib/flipper/cloud/configuration.rb', line 115

def adapter(&block)
  if block_given?
    @adapter_block = block
  else
    @adapter_block.call app_adapter
  end
end

#browObject



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/flipper/cloud/configuration.rb', line 132

def brow
  self.class.brow_instances.compute_if_absent(url + token) do
    uri = URI.parse(url)
    uri.path = "#{uri.path}/events".squeeze("/")

    Brow::Client.new({
      url: uri.to_s,
      headers: {
        "Accept" => "application/json",
        "Content-Type" => "application/json",
        "User-Agent" => "Flipper v#{VERSION} via Brow v#{Brow::VERSION}",
        "Flipper-Cloud-Token" => @token,
      }
    })
  end
end

#syncObject



126
127
128
129
130
# File 'lib/flipper/cloud/configuration.rb', line 126

def sync
  Flipper::Adapters::Sync::Synchronizer.new(local_adapter, http_adapter, {
    instrumenter: instrumenter,
  }).call
end

#sync_methodObject

Public: The method that will be used to synchronize local adapter with cloud. (default: :poll, will be :webhook if sync_secret is set).



151
152
153
# File 'lib/flipper/cloud/configuration.rb', line 151

def sync_method
  sync_secret ? :webhook : :poll
end