Class: Freno::Client
- Inherits:
-
Object
- Object
- Freno::Client
- Defined in:
- lib/freno/client.rb,
lib/freno/client/result.rb,
lib/freno/client/request.rb,
lib/freno/client/version.rb,
lib/freno/client/preconditions.rb,
lib/freno/client/requests/check.rb,
lib/freno/client/requests/check_read.rb,
lib/freno/client/requests/replication_delay.rb
Defined Under Namespace
Modules: Preconditions, Requests Classes: DecorationError, Request, Result
Constant Summary collapse
- REQUESTS =
{ check: Requests::Check, check_read: Requests::CheckRead, replication_delay: Requests::ReplicationDelay, }
- VERSION =
"0.7.0"
Instance Attribute Summary collapse
-
#decorated_requests ⇒ Object
readonly
Returns the value of attribute decorated_requests.
-
#decorators ⇒ Object
readonly
Returns the value of attribute decorators.
-
#default_app ⇒ Object
Returns the value of attribute default_app.
-
#default_store_name ⇒ Object
Returns the value of attribute default_store_name.
-
#default_store_type ⇒ Object
Returns the value of attribute default_store_type.
-
#faraday ⇒ Object
readonly
Returns the value of attribute faraday.
-
#options ⇒ Object
Returns the value of attribute options.
Instance Method Summary collapse
-
#check(app: default_app, store_type: default_store_type, store_name: default_store_name, options: self.options) ⇒ Object
Provides an interface to Freno“s check request.
-
#check?(app: default_app, store_type: default_store_type, store_name: default_store_name, options: self.options) ⇒ Boolean
Determines whether Freno considers it“s OK to write to masters.
-
#check_read(threshold:, app: default_app, store_type: default_store_type, store_name: default_store_name, options: self.options) ⇒ Object
Provides an interface to Freno“s check-read request.
-
#check_read?(threshold:, app: default_app, store_type: default_store_type, store_name: default_store_name, options: self.options) ⇒ Boolean
Determines whether it“s OK to read from replicas as replication delay is below the given threshold.
-
#decorate(request, with:) ⇒ Object
Configures the client to extend the functionality of part or all the API by means of decorators.
-
#initialize(faraday) {|_self| ... } ⇒ Client
constructor
Creates a new instance of the client, that uses faraday to make http calls.
-
#replication_delay(app: default_app, store_type: default_store_type, store_name: default_store_name, options: self.options) ⇒ Object
Implements a specific check request to retrieve the consolidated replication delay.
Constructor Details
#initialize(faraday) {|_self| ... } ⇒ Client
Creates a new instance of the client, that uses faraday to make http calls.
If most of the times you are going to ask Freno about the same app and/or storage name, you can tell the client to use some defaults, and override them as necessary.
Examples a ruby client that by default asks Freno for throttling information about ‘:my_app` accessing `:my_cluster` storage.
```ruby
freno = Freno::Client.new(faraday) do |client|
client.default_store_name = :my_cluster
client.default_app = :my_app
end
```
34 35 36 37 38 39 40 41 42 |
# File 'lib/freno/client.rb', line 34 def initialize(faraday) @faraday = faraday @default_store_type = :mysql @options = {} @decorators = {} @decorated_requests = {} yield self if block_given? end |
Instance Attribute Details
#decorated_requests ⇒ Object (readonly)
Returns the value of attribute decorated_requests.
16 17 18 |
# File 'lib/freno/client.rb', line 16 def decorated_requests @decorated_requests end |
#decorators ⇒ Object (readonly)
Returns the value of attribute decorators.
16 17 18 |
# File 'lib/freno/client.rb', line 16 def decorators @decorators end |
#default_app ⇒ Object
Returns the value of attribute default_app.
17 18 19 |
# File 'lib/freno/client.rb', line 17 def default_app @default_app end |
#default_store_name ⇒ Object
Returns the value of attribute default_store_name.
17 18 19 |
# File 'lib/freno/client.rb', line 17 def default_store_name @default_store_name end |
#default_store_type ⇒ Object
Returns the value of attribute default_store_type.
17 18 19 |
# File 'lib/freno/client.rb', line 17 def default_store_type @default_store_type end |
#faraday ⇒ Object (readonly)
Returns the value of attribute faraday.
16 17 18 |
# File 'lib/freno/client.rb', line 16 def faraday @faraday end |
#options ⇒ Object
Returns the value of attribute options.
17 18 19 |
# File 'lib/freno/client.rb', line 17 def @options end |
Instance Method Details
#check(app: default_app, store_type: default_store_type, store_name: default_store_name, options: self.options) ⇒ Object
Provides an interface to Freno“s check request
See github.com/github/freno/blob/master/doc/http.md#check-request
Returns Result
50 51 52 |
# File 'lib/freno/client.rb', line 50 def check(app: default_app, store_type: default_store_type, store_name: default_store_name, options: self.) perform :check, app: app, store_type: store_type, store_name: store_name, options: end |
#check?(app: default_app, store_type: default_store_type, store_name: default_store_name, options: self.options) ⇒ Boolean
Determines whether Freno considers it“s OK to write to masters
Returns true or false.
80 81 82 |
# File 'lib/freno/client.rb', line 80 def check?(app: default_app, store_type: default_store_type, store_name: default_store_name, options: self.) check(app: app, store_type: store_type, store_name: store_name, options: ).ok? end |
#check_read(threshold:, app: default_app, store_type: default_store_type, store_name: default_store_name, options: self.options) ⇒ Object
Provides an interface to Freno“s check-read request
See github.com/github/freno/blob/master/doc/http.md#specialized-requests
Returns Result
60 61 62 |
# File 'lib/freno/client.rb', line 60 def check_read(threshold:, app: default_app, store_type: default_store_type, store_name: default_store_name, options: self.) perform :check_read, app: app, store_type: store_type, store_name: store_name, threshold: threshold, options: end |
#check_read?(threshold:, app: default_app, store_type: default_store_type, store_name: default_store_name, options: self.options) ⇒ Boolean
Determines whether it“s OK to read from replicas as replication delay is below the given threshold.
Returns true or false.
89 90 91 |
# File 'lib/freno/client.rb', line 89 def check_read?(threshold:, app: default_app, store_type: default_store_type, store_name: default_store_name, options: self.) check_read(threshold: threshold, app: app, store_type: store_type, store_name: store_name, options: ).ok? end |
#decorate(request, with:) ⇒ Object
Configures the client to extend the functionality of part or all the API by means of decorators.
A decorator is any object that has a ‘:request` accessor and can forward the execution of `perform` to it.
Examples:
The following is a decorator implementing a read-trough cache.
“‘ruby class Cache
attr_accessor :request
def initialize(cache, ttl)
@cache = cache
@ttl = ttl
end
def perform(**kwargs)
@cache.fetch("freno:client:v1:#{args.hash}", ttl: @ttl) do
request.perform(kwargs)
end
end
end “‘
You can use it to decorate a single kind of request to freno:
“‘ruby freno = Freno::Client.new(faraday) do |client|
client.decorate :replication_delay, with: Cache.new(App.cache, App.config.ttl)
end “‘
Or every kind of request:
“‘ruby freno = Freno::Client.new(faraday) do |client|
client.decorate :all, with: Cache.new(App.cache, App.config.ttl)
end “‘
Additionally, decorators can be composed in multiple ways. The following client applies logging and instrumentation to all the requests, and it also applies caching, before the previous concerns, to ‘replication_delay` requests.
“‘ruby freno = Freno::Client.new(faraday) do |client|
client.decorate :replication_delay, with: caching
client.decorate :all, with: [logging, instrumentation]
end “‘
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/freno/client.rb', line 147 def decorate(request, with:) if request == :all requests = REQUESTS.keys else requests = Array(request) end validate!(with) requests.each do |request| decorators[request] ||= [] decorators[request] += Array(with) decorated_requests[request] = nil end end |
#replication_delay(app: default_app, store_type: default_store_type, store_name: default_store_name, options: self.options) ⇒ Object
Implements a specific check request to retrieve the consolidated replication delay
See github.com/github/freno/blob/master/doc/http.md#get-method
Returns Float indicating the replication delay in seconds as reported by Freno.
71 72 73 |
# File 'lib/freno/client.rb', line 71 def replication_delay(app: default_app, store_type: default_store_type, store_name: default_store_name, options: self.) perform :replication_delay, app: app, store_type: store_type, store_name: store_name, options: end |