-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
200 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# frozen_string_literal: true | ||
|
||
# MIT License | ||
# | ||
# Copyright (c) 2024 Zerocracy | ||
# | ||
# Permission is hereby granted, free of charge, to any person obtaining a copy | ||
# of this software and associated documentation files (the "Software"), to deal | ||
# in the Software without restriction, including without limitation the rights | ||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
# copies of the Software, and to permit persons to whom the Software is | ||
# furnished to do so, subject to the following conditions: | ||
# | ||
# The above copyright notice and this permission notice shall be included in all | ||
# copies or substantial portions of the Software. | ||
# | ||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
# SOFTWARE. | ||
|
||
# The module. | ||
module Fbe::Middleware | ||
# empty | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# frozen_string_literal: true | ||
|
||
# MIT License | ||
# | ||
# Copyright (c) 2024 Zerocracy | ||
# | ||
# Permission is hereby granted, free of charge, to any person obtaining a copy | ||
# of this software and associated documentation files (the "Software"), to deal | ||
# in the Software without restriction, including without limitation the rights | ||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
# copies of the Software, and to permit persons to whom the Software is | ||
# furnished to do so, subject to the following conditions: | ||
# | ||
# The above copyright notice and this permission notice shall be included in all | ||
# copies or substantial portions of the Software. | ||
# | ||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
# SOFTWARE. | ||
|
||
require 'faraday' | ||
|
||
# Faraday Middleware that monitors GitHub API rate limits. | ||
class Fbe::Middleware::Quota < Faraday::Middleware | ||
def initialize(app, logger: Loog::NULL, pause: 60, limit: 100, rate: 5) | ||
super(app) | ||
@limit = limit | ||
@requests = 0 | ||
@app = app | ||
@logger = logger | ||
@pause = pause | ||
@rate = rate | ||
end | ||
|
||
def call(env) | ||
@requests += 1 | ||
response = @app.call(env) | ||
if out_of_limit?(env) | ||
@logger.info( | ||
"Too much GitHub API quota consumed, pausing for #{@pause} seconds" | ||
) | ||
sleep(@pause) | ||
@requests = 0 | ||
end | ||
response | ||
end | ||
|
||
private | ||
|
||
def out_of_limit?(env) | ||
remaining = env.response_headers['x-ratelimit-remaining'].to_i | ||
(@requests % @limit).zero? && remaining < @rate | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
# frozen_string_literal: true | ||
|
||
# MIT License | ||
# | ||
# Copyright (c) 2024 Zerocracy | ||
# | ||
# Permission is hereby granted, free of charge, to any person obtaining a copy | ||
# of this software and associated documentation files (the "Software"), to deal | ||
# in the Software without restriction, including without limitation the rights | ||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
# copies of the Software, and to permit persons to whom the Software is | ||
# furnished to do so, subject to the following conditions: | ||
# | ||
# The above copyright notice and this permission notice shall be included in all | ||
# copies or substantial portions of the Software. | ||
# | ||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
# SOFTWARE. | ||
|
||
require 'minitest/autorun' | ||
require 'faraday' | ||
require 'logger' | ||
require_relative '../../../lib/fbe/middleware' | ||
|
||
class QuotaTest < Minitest::Test | ||
class FakeApp | ||
def initialize | ||
@calls = 0 | ||
end | ||
|
||
def call(env) | ||
@calls += 1 | ||
response_headers = { | ||
'x-ratelimit-remaining' => (100 - @calls).to_s | ||
} | ||
env[:response_headers] = response_headers | ||
env | ||
end | ||
end | ||
|
||
def test_quota_middleware_pauses_when_quota_low | ||
loog = Loog::NULL | ||
pause = 0 | ||
app = FakeApp.new | ||
middleware = Fbe::Middleware::Quota.new(app, logger: loog, pause:) | ||
start_time = Time.now | ||
105.times do | ||
env = Judges::Options.new( | ||
'method' => :get, | ||
'url' => 'http://example.com', | ||
'request_headers' => {}, | ||
'response_headers' => {} | ||
) | ||
middleware.call(env) | ||
end | ||
assert_in_delta pause, Time.now - start_time, 0.4 | ||
end | ||
|
||
def test_quota_middleware_logs_when_quota_low | ||
pause = 1 | ||
log_output = StringIO.new | ||
loog = Logger.new(log_output) | ||
app = FakeApp.new | ||
middleware = Fbe::Middleware::Quota.new(app, logger: loog, pause:) | ||
105.times do | ||
env = Judges::Options.new( | ||
'method' => :get, | ||
'url' => 'http://example.com', | ||
'request_headers' => {}, | ||
'response_headers' => {} | ||
) | ||
middleware.call(env) | ||
end | ||
assert_match(/Too much GitHub API quota/, log_output.string) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters