freno-client 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ad998991ce9e268582944b14bcad1f95b3f256ae
4
+ data.tar.gz: 858a1e5eab82112e90e44559cc64f14a57eef065
5
+ SHA512:
6
+ metadata.gz: d550f31be8158f7ec916d3b76475c8ec15e04e6039cc34473049ad276097397df72c1139bfd421175b1792eba997763fffadcd9023d4860eef6096f23040d024
7
+ data.tar.gz: c6c3826a47449958d2030fc8f8904eeba9cee2aeb72890fa6803d0c287542c13814df6934b0b47d03cb418544cef7a856623591700b4d149ca5ba4df0edd261b
@@ -0,0 +1,13 @@
1
+ *.gem
2
+ /.bundle/
3
+ /.yardoc
4
+ /Gemfile.lock
5
+ /_yardoc/
6
+ /coverage/
7
+ /doc/
8
+ /pkg/
9
+ /spec/reports/
10
+ /tmp/
11
+ /bin/
12
+ /vendor/gems
13
+ .ruby-version
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.1
5
+ before_install: gem install bundler -v 1.14.6
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at opensource+freno-client@github.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
@@ -0,0 +1,32 @@
1
+ ## Contributing
2
+
3
+ [fork]: https://github.com/github/freno-client/fork
4
+ [pr]: https://github.com/github/freno-client/compare
5
+ [style]: https://github.com/styleguide/ruby
6
+ [code-of-conduct]: CODE_OF_CONDUCT.md
7
+
8
+ Hi there! We're thrilled that you'd like to contribute to this project. Your help is essential for keeping it great.
9
+
10
+ Please note that this project is released with a [Contributor Code of Conduct][code-of-conduct]. By participating in this project you agree to abide by its terms.
11
+
12
+ ## Submitting a pull request
13
+
14
+ 0. [Fork][fork] and clone the freno-clientsitory
15
+ 0. Configure and install the dependencies: `script/bootstrap`
16
+ 0. Make sure the tests pass on your machine: `rake`
17
+ 0. Create a new branch: `git checkout -b my-branch-name`
18
+ 0. Make your change, add tests, and make sure the tests still pass
19
+ 0. Push to your fork and [submit a pull request][pr]
20
+ 0. Pat your self on the back and wait for your pull request to be reviewed and merged.
21
+
22
+ Here are a few things you can do that will increase the likelihood of your pull request being accepted:
23
+
24
+ - Write tests.
25
+ - Keep your change as focused as possible. If there are multiple changes you would like to make that are not dependent upon each other, consider submitting them as separate pull requests.
26
+ - Write a [good commit message](http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html).
27
+
28
+ ## Resources
29
+
30
+ - [How to Contribute to Open Source](https://opensource.guide/how-to-contribute/)
31
+ - [Using Pull Requests](https://help.github.com/articles/about-pull-requests/)
32
+ - [GitHub Help](https://help.github.com)
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 GitHub Inc.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,202 @@
1
+ # Freno::Client
2
+
3
+ A ruby client for [Freno](https://github.com/github/freno): the cooperative, highly available throttler service.
4
+
5
+ ## Current status
6
+
7
+ `Freno::Client`, as [Freno](https://github.com/github/freno) itself, is in active development and its API can still change.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem "freno-client"
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install freno-client
24
+
25
+ ## Usage
26
+
27
+ `Freno::Client` uses [faraday](https://github.com/lostisland/faraday) to abstract the http client of your choice:
28
+
29
+ To start using the client, give it a faraday instance pointing to Freno's base URL.
30
+
31
+ ```ruby
32
+ require 'freno/client'
33
+
34
+ FRENO_URL = "http://freno.domain.com:8111"
35
+ faraday = Faraday.new(FRENO_URL)
36
+ freno = Freno::Client.new(faraday)
37
+
38
+ freno.check?(app: :my_app, store_name: :my_cluster)
39
+ # => true
40
+
41
+ freno.replication_delay(app: :my_app, store_name: :my_cluster)
42
+ # => 0.125
43
+ ```
44
+
45
+ ### Providing sensible defaults
46
+
47
+ 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.
48
+
49
+ ```ruby
50
+ freno = Freno::Client.new(faraday) do |client|
51
+ client.default_store_name = :my_cluster
52
+ client.default_app = :my_app
53
+ end
54
+
55
+ freno.check?
56
+ # => true (Freno thinks that `my_app` can write to `main` storage)
57
+
58
+ freno.check?(app: :another_app, store_name: :another_storage)
59
+ # => false (Freno thinks that `another_app` should not write to `another_storage`)
60
+ ```
61
+
62
+ ### What can I do with the client?
63
+
64
+ #### Asking whether an app can write to a certain storage. ([`check` requests](https://github.com/github/freno/blob/master/doc/http.md#check-requests))
65
+
66
+ If we want to get a deep sense on why freno allowed or not, writing to a certain storage.
67
+
68
+ ```ruby
69
+ result = freno.check(app: :my_app, store_name: :my_cluster)
70
+ # => #<Freno::Client::Requests::Result ...>
71
+
72
+ result.ok?
73
+ # => false
74
+
75
+ result.failed?
76
+ # => true
77
+
78
+ result.code
79
+ # => 429
80
+
81
+ result.meaning
82
+ # => :too_many_requests
83
+ ```
84
+
85
+ Or if we only want to know if we can write:
86
+
87
+ ```ruby
88
+ result = freno.check?(app: :my_app, store_name: :my_cluster)
89
+ # => true or false (a shortcut for `check.ok?`)
90
+ ```
91
+
92
+ #### Asking whether replication delay is below a certain threshold. ([`check-read` requests](https://github.com/github/freno/blob/master/doc/http.md#specialized-requests))
93
+
94
+ ```ruby
95
+ result = freno.check_read(threshold: 0.5, app: :my_app, store_name: :my_cluster)
96
+ # => #<Freno::Client::Requests::Result ...>
97
+
98
+ result.ok?
99
+ # => true
100
+
101
+ result.failed?
102
+ # => false
103
+
104
+ result.code
105
+ # => 200
106
+
107
+ result.meaning
108
+ # => :ok
109
+ ```
110
+
111
+ Or if we only want to know if we can read:
112
+
113
+ ```ruby
114
+ freno.check?(threshold: 0.5, app: :my_app, store_name: :my_cluster)
115
+ # => true or false (a shortcut for `check_read.ok?`)
116
+ ```
117
+
118
+ #### Asking what's the replication delay
119
+
120
+ Freno's response to [`GET /check`](https://github.com/github/freno/blob/master/doc/http.md#get-method) includes the replication delay value in seconds. The `replication_delay` method in the client returns this information.
121
+
122
+ ```ruby
123
+ freno.replication_delay(app: :my_app, store_name: :my_cluster)
124
+ # => 0.125
125
+ ```
126
+
127
+ #### Cross-cutting concerns with decorators
128
+
129
+ Decorators can be used augment the client with custom features.
130
+
131
+ A decorator is anything that has a `:request` accessor and can forward the execution of `perform` to it.
132
+
133
+ The following is an example of a decorator implementing a read-trough cache.
134
+
135
+ ```ruby
136
+ class Cache
137
+ attr_accessor :request
138
+
139
+ def initialize(cache, ttl)
140
+ @cache = cache
141
+ @ttl = ttl
142
+ end
143
+
144
+ def perform(**kwargs)
145
+ @cache.fetch("freno:client:v1:#{args.hash}", ttl: @ttl) do
146
+ request.perform(kwargs)
147
+ end
148
+ end
149
+ end
150
+ ```
151
+
152
+ You can use it to decorate a single kind of request to freno:
153
+
154
+ ```ruby
155
+ freno = Freno::Client.new(faraday) do |client|
156
+ client.decorate :replication_delay, with: Cache.new(App.cache, App.config.ttl)
157
+ end
158
+ ```
159
+
160
+ Or every kind of request:
161
+
162
+ ```ruby
163
+ freno = Freno::Client.new(faraday) do |client|
164
+ client.decorate :all, with: Cache.new(App.cache, App.config.ttl)
165
+ end
166
+ ```
167
+
168
+ Additionally, decorators can be composed in multiple ways. The following client
169
+ applies logging and instrumentation to all the requests, and it also applies caching, **before** the previous concerns, to `replication_delay` requests.
170
+
171
+ ```ruby
172
+ freno = Freno::Client.new(faraday) do |client|
173
+ client.decorate :replication_delay, with: caching
174
+ client.decorate :all, with: [logging, instrumentation]
175
+ end
176
+ ```
177
+
178
+
179
+ ## Development
180
+
181
+ After checking out the repo, run `script/bootstrap` to install dependencies. Then, run `script/test` to run the tests. You can also run `script/console` for an interactive prompt that will allow you to experiment.
182
+
183
+ ## Contributing
184
+
185
+ This repository is open to [contributions](CONTRIBUTING.md). Contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
186
+
187
+ ## Releasing
188
+
189
+ If you are the current maintainer of this gem:
190
+
191
+ 1. Create a branch for the release: `git checkout -b cut-release-vx.y.z`
192
+ 1. Make sure your local dependencies are up to date: `script/bootstrap`
193
+ 1. Ensure that tests are green: `bundle exec rake test`
194
+ 1. Bump gem version in `lib/freno/client/version.rb`
195
+ 1. Merge a PR to github/freno-client containing the changes in the version file
196
+ 1. Tag and push: `git tag vx.xx.xx; git push --tags`
197
+ 1. Build the gem: `gem build freno-client`
198
+ 1. Push to rubygems.org: `gem push freno-client-x.y.z.gem`
199
+
200
+ ## License
201
+
202
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,38 @@
1
+ # -- encoding: utf-8 --
2
+ $:.unshift File.expand_path('../lib', __FILE__)
3
+ require_relative 'lib/freno/client/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "freno-client"
7
+ spec.version = Freno::Client::VERSION
8
+ spec.authors = ["Miguel Fernández"]
9
+ spec.email = ["opensource+freno-client@github.com"]
10
+
11
+ spec.summary = %q{A library for interacting with freno, the throttler service}
12
+ spec.description = %q{freno-client is a ruby library that interacts with
13
+ Freno using HTTP. Freno is a throttling service and its
14
+ source code is available at https://github.com/github/freno}
15
+ spec.homepage = "https://github.com/github/freno-client"
16
+ spec.license = "MIT"
17
+ spec.required_ruby_version = ">= 2.0.0"
18
+
19
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
20
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
21
+ if spec.respond_to?(:metadata)
22
+ spec.metadata['allowed_push_host'] = "https://rubygems.org"
23
+ else
24
+ raise "RubyGems 2.0 or newer is required to protect against " \
25
+ "public gem pushes."
26
+ end
27
+
28
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
29
+ f.match(%r{^(test|spec|features)/})
30
+ end
31
+ spec.executables = []
32
+ spec.require_paths = ["lib"]
33
+
34
+ spec.add_development_dependency "bundler", "~> 1.14"
35
+ spec.add_development_dependency "rake", "~> 10.0"
36
+ spec.add_development_dependency "minitest", "~> 5.0"
37
+ spec.add_development_dependency "faraday", "~> 0"
38
+ end
@@ -0,0 +1,182 @@
1
+ require "freno/client/version"
2
+ require "freno/client/requests/check_read"
3
+ require "freno/client/requests/check"
4
+ require "freno/client/requests/replication_delay"
5
+
6
+ module Freno
7
+ class Client
8
+
9
+ REQUESTS = {
10
+ check: Requests::Check,
11
+ check_read: Requests::CheckRead,
12
+ replication_delay: Requests::ReplicationDelay,
13
+ }
14
+
15
+ attr_reader :faraday, :decorators, :decorated_requests
16
+ attr_accessor :default_app, :default_store_name, :default_store_type, :options
17
+
18
+ # Creates a new instance of the client, that uses faraday to make http calls.
19
+ #
20
+ # If most of the times you are going to ask Freno about the same app and/or storage name,
21
+ # you can tell the client to use some defaults, and override them as necessary.
22
+ #
23
+ # Examples a ruby client that by default asks Freno for throttling information
24
+ # about `:my_app` accessing `:my_cluster` storage.
25
+ #
26
+ # ```ruby
27
+ # freno = Freno::Client.new(faraday) do |client|
28
+ # client.default_store_name = :my_cluster
29
+ # client.default_app = :my_app
30
+ # end
31
+ # ```
32
+ #
33
+ def initialize(faraday)
34
+ @faraday = faraday
35
+ @default_store_type = :mysql
36
+ @options = {}
37
+ @decorators = {}
38
+ @decorated_requests = {}
39
+
40
+ yield self if block_given?
41
+ end
42
+
43
+ # Provides an interface to Freno's check request
44
+ #
45
+ # See https://github.com/github/freno/blob/master/doc/http.md#check-request
46
+ #
47
+ # Returns Result
48
+ #
49
+ def check(app: default_app, store_type: default_store_type, store_name: default_store_name, options: self.options)
50
+ perform :check, app: app, store_type: store_type, store_name: store_name, options: options
51
+ end
52
+
53
+ # Provides an interface to Freno's check-read request
54
+ #
55
+ # See https://github.com/github/freno/blob/master/doc/http.md#specialized-requests
56
+ #
57
+ # Returns Result
58
+ #
59
+ def check_read(threshold:, app: default_app, store_type: default_store_type, store_name: default_store_name, options: self.options)
60
+ perform :check_read, app: app, store_type: store_type, store_name: store_name, threshold: threshold, options: options
61
+ end
62
+
63
+ # Implements a specific check request to retrieve the consolidated replication
64
+ # delay
65
+ #
66
+ # See https://github.com/github/freno/blob/master/doc/http.md#get-method
67
+ #
68
+ # Returns Float indicating the replication delay in seconds as reported by Freno.
69
+ #
70
+ def replication_delay(app: default_app, store_type: default_store_type, store_name: default_store_name, options: self.options)
71
+ perform :replication_delay, app: app, store_type: store_type, store_name: store_name, options: options
72
+ end
73
+
74
+
75
+ # Determines whether Freno considers it's OK to write to masters
76
+ #
77
+ # Returns true or false.
78
+ #
79
+ def check?(app: default_app, store_type: default_store_type, store_name: default_store_name, options: self.options)
80
+ check(app: app, store_type: store_type, store_name: store_name, options: options).ok?
81
+ end
82
+
83
+ # Determines whether it's OK to read from replicas as replication delay is below
84
+ # the given threshold.
85
+ #
86
+ # Returns true or false.
87
+ #
88
+ def check_read?(threshold:, app: default_app, store_type: default_store_type, store_name: default_store_name, options: self.options)
89
+ check_read(threshold: threshold, app: app, store_type: store_type, store_name: store_name, options: options).ok?
90
+ end
91
+
92
+ # Configures the client to extend the functionality of part or all the API
93
+ # by means of decorators.
94
+ #
95
+ # A decorator is any object that has a `:request` accessor and can forward
96
+ # the execution of `perform` to it.
97
+ #
98
+ # Examples:
99
+ #
100
+ # The following is a decorator implementing a read-trough cache.
101
+ #
102
+ # ```ruby
103
+ # class Cache
104
+ # attr_accessor :request
105
+ #
106
+ # def initialize(cache, ttl)
107
+ # @cache = cache
108
+ # @ttl = ttl
109
+ # end
110
+ #
111
+ # def perform(**kwargs)
112
+ # @cache.fetch("freno:client:v1:#{args.hash}", ttl: @ttl) do
113
+ # request.perform(kwargs)
114
+ # end
115
+ # end
116
+ # end
117
+ # ```
118
+ #
119
+ # You can use it to decorate a single kind of request to freno:
120
+ #
121
+ # ```ruby
122
+ # freno = Freno::Client.new(faraday) do |client|
123
+ # client.decorate :replication_delay, with: Cache.new(App.cache, App.config.ttl)
124
+ # end
125
+ # ```
126
+ #
127
+ # Or every kind of request:
128
+ #
129
+ # ```ruby
130
+ # freno = Freno::Client.new(faraday) do |client|
131
+ # client.decorate :all, with: Cache.new(App.cache, App.config.ttl)
132
+ # end
133
+ # ```
134
+ #
135
+ # Additionally, decorators can be composed in multiple ways. The following client
136
+ # applies logging and instrumentation to all the requests, and it also applies caching,
137
+ # **before** the previous concerns, to `replication_delay` requests.
138
+ #
139
+ # ```ruby
140
+ # freno = Freno::Client.new(faraday) do |client|
141
+ # client.decorate :replication_delay, with: caching
142
+ # client.decorate :all, with: [logging, instrumentation]
143
+ # end
144
+ # ```
145
+ #
146
+ def decorate(request, with:)
147
+ if request == :all
148
+ requests = REQUESTS.keys
149
+ else
150
+ requests = Array(request)
151
+ end
152
+
153
+ requests.each do |request|
154
+ decorators[request] ||= []
155
+ decorators[request] += Array(with)
156
+ decorated_requests[request] = nil
157
+ end
158
+ end
159
+
160
+ private
161
+
162
+ def perform(request, **kwargs)
163
+ decorated(request).perform(kwargs.merge(faraday: faraday))
164
+ end
165
+
166
+ def decorated(request)
167
+ decorated_requests[request] ||= begin
168
+ to_decorate = Array(decorators[request]) + Array(REQUESTS[request])
169
+
170
+ outermost = to_decorate[0]
171
+ current = outermost
172
+
173
+ (to_decorate[1..-1]).each do |decorator|
174
+ current.request = decorator
175
+ current = current.request
176
+ end
177
+
178
+ outermost
179
+ end
180
+ end
181
+ end
182
+ end
@@ -0,0 +1,35 @@
1
+ module Freno
2
+ class Client
3
+ module Preconditions
4
+ extend self
5
+
6
+ PreconditionNotMet = Class.new(ArgumentError)
7
+
8
+ class Checker
9
+ attr_reader :errors
10
+
11
+ def initialize
12
+ @errors = []
13
+ end
14
+
15
+ def present(args = {})
16
+ args.each do |arg, value|
17
+ unless value
18
+ errors << "#{arg} should be present"
19
+ end
20
+ end
21
+ end
22
+
23
+ def report
24
+ raise PreconditionNotMet.new(errors.join("\n")) unless errors.empty?
25
+ end
26
+ end
27
+
28
+ def check(&block)
29
+ checker = Checker.new
30
+ checker.instance_eval(&block)
31
+ checker.report
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,60 @@
1
+ require_relative 'preconditions'
2
+ require_relative 'result'
3
+
4
+ module Freno
5
+ class Client
6
+ class Request
7
+
8
+ include Freno::Client::Preconditions
9
+
10
+ attr_reader :faraday, :args, :options
11
+ attr_reader :raise_on_timeout
12
+
13
+ def self.perform(**kwargs)
14
+ new(kwargs).perform
15
+ end
16
+
17
+ def initialize(**kwargs)
18
+ @args = kwargs
19
+ @faraday = kwargs.delete(:faraday) || nil
20
+ @options = kwargs.delete(:options) || Hash.new
21
+
22
+ @raise_on_timeout = options.fetch(:raise_on_timeout, true)
23
+ @verb = options.fetch(:verb, :head)
24
+ end
25
+
26
+ def perform
27
+ begin
28
+ response = request(verb, path)
29
+ rescue Faraday::TimeoutError => ex
30
+ raise ex if raise_on_timeout
31
+ Result.from_meaning(:request_timeout)
32
+ else
33
+ process_response(response)
34
+ end
35
+ end
36
+
37
+ protected
38
+
39
+ def request(verb, path)
40
+ faraday.send(verb, path)
41
+ end
42
+
43
+ def path
44
+ @path || begin
45
+ raise NotImplementedError("must be overriden in specific requests, or memoized in @path")
46
+ end
47
+ end
48
+
49
+ def verb
50
+ @verb || begin
51
+ raise NotImplementedError("must be overriden in specific requests, or memoized in @verb")
52
+ end
53
+ end
54
+
55
+ def process_response(response)
56
+ Result.from_faraday_response(response)
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,24 @@
1
+ require_relative '../request'
2
+
3
+ module Freno
4
+ class Client
5
+ module Requests
6
+ class Check < Request
7
+
8
+ def initialize(**kwargs)
9
+ super
10
+
11
+ app = kwargs.fetch(:app)
12
+ store_type = kwargs.fetch(:store_type)
13
+ store_name = kwargs.fetch(:store_name)
14
+
15
+ check do
16
+ present app: app, store_type: store_type, store_name: store_name
17
+ end
18
+
19
+ @path = "check/#{app}/#{store_type}/#{store_name}"
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,25 @@
1
+ require_relative '../request'
2
+
3
+ module Freno
4
+ class Client
5
+ module Requests
6
+ class CheckRead < Request
7
+
8
+ def initialize(**kwargs)
9
+ super
10
+
11
+ app = kwargs.fetch(:app)
12
+ store_type = kwargs.fetch(:store_type)
13
+ store_name = kwargs.fetch(:store_name)
14
+ threshold = kwargs.fetch(:threshold)
15
+
16
+ check do
17
+ present app: app, store_type: store_type, store_name: store_name, threshold: threshold
18
+ end
19
+
20
+ @path = "check-read/#{app}/#{store_type}/#{store_name}/#{threshold.to_f.round(3)}"
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,21 @@
1
+ require_relative '../request'
2
+
3
+ module Freno
4
+ class Client
5
+ module Requests
6
+ class ReplicationDelay < Check
7
+
8
+ protected
9
+
10
+ def process_response(*)
11
+ response = super
12
+ response.body["Value"]
13
+ end
14
+
15
+ def verb
16
+ :get
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,62 @@
1
+ require 'json'
2
+
3
+ module Freno
4
+ class Client
5
+ class Result
6
+
7
+ # https://github.com/github/freno/blob/master/doc/http.md#status-codes
8
+ FRENO_STATUS_CODE_MEANINGS = {
9
+ 200 => :ok,
10
+ 404 => :not_found,
11
+ 417 => :expectation_failed,
12
+ 429 => :too_many_requests,
13
+ 500 => :internal_server_error
14
+ }.freeze
15
+
16
+ # these are included to add resiliency to freno-client
17
+ ADDITIONAL_STATUS_CODE_MEANINGS = {
18
+ 408 => :request_timeout
19
+ }.freeze
20
+
21
+ CODE_MEANINGS = FRENO_STATUS_CODE_MEANINGS.merge(ADDITIONAL_STATUS_CODE_MEANINGS).freeze
22
+ MEANING_CODES = CODE_MEANINGS.invert.freeze
23
+
24
+ def self.from_faraday_response(response)
25
+ new(response.status, response.body)
26
+ end
27
+
28
+ def self.from_meaning(meaning)
29
+ new(MEANING_CODES[meaning] || 0)
30
+ end
31
+
32
+ attr_reader :code, :meaning, :raw_body
33
+
34
+ def initialize(code, body = nil)
35
+ @code = code
36
+ @meaning = CODE_MEANINGS[code] || :unknown
37
+ @raw_body = body
38
+ end
39
+
40
+ def ok?
41
+ meaning == :ok
42
+ end
43
+
44
+ def failed?
45
+ !ok?
46
+ end
47
+
48
+ def unkown?
49
+ meaning == :unkown
50
+ end
51
+
52
+ def body
53
+ @body ||= JSON.parse(raw_body) if raw_body
54
+ end
55
+
56
+ def ==(other)
57
+ return meaning == other if other.is_a? Symbol
58
+ code == other
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,5 @@
1
+ module Freno
2
+ class Client
3
+ VERSION = "0.3.0"
4
+ end
5
+ end
@@ -0,0 +1,9 @@
1
+ #!/bin/sh
2
+
3
+ set -e
4
+
5
+ cd "$(dirname "$0")/.."
6
+
7
+ bundle config --local path vendor/gems
8
+
9
+ bundle check > /dev/null 2>&1 || bundle install
@@ -0,0 +1,19 @@
1
+ #!/bin/sh
2
+
3
+ set -eu
4
+
5
+ test -d "/usr/share/rbenv/shims" && {
6
+ export PATH="/usr/share/rbenv/shims:$PATH"
7
+ export RBENV_VERSION="2.4.1"
8
+ }
9
+
10
+ set -x
11
+ git log -n 1 HEAD | cat
12
+ ruby -v
13
+ bundle -v
14
+ set +x
15
+
16
+ git clean -fd
17
+
18
+ script/bootstrap
19
+ bundle exec rake
@@ -0,0 +1,2 @@
1
+ #!/bin/sh
2
+ exec bundle exec irb -r "freno/client" "$@"
metadata ADDED
@@ -0,0 +1,126 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: freno-client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.0
5
+ platform: ruby
6
+ authors:
7
+ - Miguel Fernández
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-07-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.14'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.14'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: faraday
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: |-
70
+ freno-client is a ruby library that interacts with
71
+ Freno using HTTP. Freno is a throttling service and its
72
+ source code is available at https://github.com/github/freno
73
+ email:
74
+ - opensource+freno-client@github.com
75
+ executables: []
76
+ extensions: []
77
+ extra_rdoc_files: []
78
+ files:
79
+ - ".gitignore"
80
+ - ".travis.yml"
81
+ - CODE_OF_CONDUCT.md
82
+ - CONTRIBUTING.md
83
+ - Gemfile
84
+ - LICENSE.txt
85
+ - README.md
86
+ - Rakefile
87
+ - bin/console
88
+ - bin/setup
89
+ - freno-client.gemspec
90
+ - lib/freno/client.rb
91
+ - lib/freno/client/preconditions.rb
92
+ - lib/freno/client/request.rb
93
+ - lib/freno/client/requests/check.rb
94
+ - lib/freno/client/requests/check_read.rb
95
+ - lib/freno/client/requests/replication_delay.rb
96
+ - lib/freno/client/result.rb
97
+ - lib/freno/client/version.rb
98
+ - script/bootstrap
99
+ - script/cibuild
100
+ - script/console
101
+ homepage: https://github.com/github/freno-client
102
+ licenses:
103
+ - MIT
104
+ metadata:
105
+ allowed_push_host: https://rubygems.org
106
+ post_install_message:
107
+ rdoc_options: []
108
+ require_paths:
109
+ - lib
110
+ required_ruby_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: 2.0.0
115
+ required_rubygems_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ requirements: []
121
+ rubyforge_project:
122
+ rubygems_version: 2.6.11
123
+ signing_key:
124
+ specification_version: 4
125
+ summary: A library for interacting with freno, the throttler service
126
+ test_files: []