Class: Freno::Client

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

Instance Method Summary collapse

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
```

Yields:

  • (_self)

Yield Parameters:

  • _self (Freno::Client)

    the object that the method was called on



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_requestsObject (readonly)

Returns the value of attribute decorated_requests.



16
17
18
# File 'lib/freno/client.rb', line 16

def decorated_requests
  @decorated_requests
end

#decoratorsObject (readonly)

Returns the value of attribute decorators.



16
17
18
# File 'lib/freno/client.rb', line 16

def decorators
  @decorators
end

#default_appObject

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_nameObject

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_typeObject

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

#faradayObject (readonly)

Returns the value of attribute faraday.



16
17
18
# File 'lib/freno/client.rb', line 16

def faraday
  @faraday
end

#optionsObject

Returns the value of attribute options.



17
18
19
# File 'lib/freno/client.rb', line 17

def options
  @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.options)
  perform :check, app: app, store_type: store_type, store_name: store_name, options: 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.

Returns:

  • (Boolean)


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.options)
  check(app: app, store_type: store_type, store_name: store_name, options: 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.options)
  perform :check_read, app: app, store_type: store_type, store_name: store_name, threshold: threshold, options: 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.

Returns:

  • (Boolean)


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.options)
  check_read(threshold: threshold, app: app, store_type: store_type, store_name: store_name, options: 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.options)
  perform :replication_delay, app: app, store_type: store_type, store_name: store_name, options: options
end