faraday-hedge 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.github/workflows/ci.yml +19 -0
- data/.gitignore +15 -0
- data/.rspec +3 -0
- data/CHANGELOG.md +4 -0
- data/CODE_OF_CONDUCT.md +26 -0
- data/CONTRIBUTING.md +10 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +47 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/faraday-hedge.gemspec +37 -0
- data/lib/faraday/hedge/version.rb +5 -0
- data/lib/faraday/hedge.rb +47 -0
- metadata +134 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: a9e3815ee4e3c7b0138fbb6281257e326acde2e95e6b3a6df9d18fb19b86d658
|
|
4
|
+
data.tar.gz: c7b5347f8bfdfc69361509631093e2794c779afd3804dbd29bffa60a82b7847a
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 522cc67bbd621d2ffcdac40780d79ce2f0cf6fef9b9ba5eb46ac6cc31e7581ed848b10a6dfa08d430477417a79d382f9792330c0349df276d2c1b2f5dcf5cdfa
|
|
7
|
+
data.tar.gz: 188ed7b0722350c3d45dd1f0ca8fd112db4504eda410950275085d36e9c1d7568db45f77612975f600aabb99bbf28727a11f7b0359e99e521109c1e5b2c360bb
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
name: CI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
pull_request:
|
|
6
|
+
|
|
7
|
+
jobs:
|
|
8
|
+
test:
|
|
9
|
+
runs-on: ubuntu-latest
|
|
10
|
+
strategy:
|
|
11
|
+
matrix:
|
|
12
|
+
ruby: ["3.0", "3.1", "3.2", "3.3"]
|
|
13
|
+
steps:
|
|
14
|
+
- uses: actions/checkout@v4
|
|
15
|
+
- uses: ruby/setup-ruby@v1
|
|
16
|
+
with:
|
|
17
|
+
ruby-version: ${{ matrix.ruby }}
|
|
18
|
+
bundler-cache: true
|
|
19
|
+
- run: bundle exec rspec
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
|
2
|
+
|
|
3
|
+
## Our Pledge
|
|
4
|
+
We pledge to make participation in our community a harassment-free experience for everyone.
|
|
5
|
+
|
|
6
|
+
## Our Standards
|
|
7
|
+
Examples of behavior that contributes to a positive environment include:
|
|
8
|
+
- Using welcoming and inclusive language
|
|
9
|
+
- Respecting differing viewpoints and experiences
|
|
10
|
+
- Gracefully accepting constructive criticism
|
|
11
|
+
|
|
12
|
+
Examples of unacceptable behavior include:
|
|
13
|
+
- Trolling, insulting/derogatory comments, and personal or political attacks
|
|
14
|
+
- Public or private harassment
|
|
15
|
+
|
|
16
|
+
## Enforcement Responsibilities
|
|
17
|
+
Project maintainers are responsible for clarifying and enforcing standards of acceptable behavior.
|
|
18
|
+
|
|
19
|
+
## Scope
|
|
20
|
+
This Code of Conduct applies within all project spaces and in public spaces when representing the project.
|
|
21
|
+
|
|
22
|
+
## Enforcement
|
|
23
|
+
Instances of abusive behavior may be reported to the maintainer listed in the gemspec.
|
|
24
|
+
|
|
25
|
+
## Attribution
|
|
26
|
+
This Code of Conduct is adapted from the Contributor Covenant, version 1.4.
|
data/CONTRIBUTING.md
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 MounirGaiby
|
|
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.
|
data/README.md
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
# Faraday Hedge
|
|
2
|
+
|
|
3
|
+
Hedged requests middleware for Faraday to reduce tail latency on idempotent methods.
|
|
4
|
+
|
|
5
|
+
## About
|
|
6
|
+
Faraday Hedge issues a backup request after a small delay and returns the first response. This reduces tail latency when occasional slow requests occur, while keeping overall load bounded.
|
|
7
|
+
|
|
8
|
+
The middleware defaults to idempotent methods and can be configured to hedge only specific HTTP verbs.
|
|
9
|
+
|
|
10
|
+
## Use Cases
|
|
11
|
+
- Reduce p99 latency for flaky upstream APIs
|
|
12
|
+
- Protect against tail latency spikes on critical read paths
|
|
13
|
+
- Improve UX for latency-sensitive services
|
|
14
|
+
|
|
15
|
+
## Compatibility
|
|
16
|
+
- Ruby 3.0+
|
|
17
|
+
- Faraday 1.0+
|
|
18
|
+
|
|
19
|
+
## Installation
|
|
20
|
+
```ruby
|
|
21
|
+
# Gemfile
|
|
22
|
+
|
|
23
|
+
gem "faraday-hedge"
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Usage
|
|
27
|
+
```ruby
|
|
28
|
+
conn = Faraday.new do |f|
|
|
29
|
+
f.request :hedge, delay: 0.05
|
|
30
|
+
f.adapter :net_http
|
|
31
|
+
end
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Options
|
|
35
|
+
- `delay` (Float) seconds before firing a backup request
|
|
36
|
+
- `max_hedges` (Integer) number of backup requests to allow
|
|
37
|
+
- `methods` (Array) methods eligible for hedging
|
|
38
|
+
- `idempotent_only` (Boolean) restrict hedging to idempotent methods
|
|
39
|
+
|
|
40
|
+
## Notes
|
|
41
|
+
- Hedging uses background threads.
|
|
42
|
+
- Use conservative delays to avoid excessive duplicate work.
|
|
43
|
+
|
|
44
|
+
## Release
|
|
45
|
+
```bash
|
|
46
|
+
bundle exec rake release
|
|
47
|
+
```
|
data/Rakefile
ADDED
data/bin/console
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
|
|
3
|
+
require "bundler/setup"
|
|
4
|
+
require "faraday/hedge"
|
|
5
|
+
|
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
|
8
|
+
|
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
|
10
|
+
# require "pry"
|
|
11
|
+
# Pry.start
|
|
12
|
+
|
|
13
|
+
require "irb"
|
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
lib = File.expand_path("lib", __dir__)
|
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
5
|
+
require "faraday/hedge/version"
|
|
6
|
+
|
|
7
|
+
Gem::Specification.new do |spec|
|
|
8
|
+
spec.name = "faraday-hedge"
|
|
9
|
+
spec.version = Faraday::Hedge::VERSION
|
|
10
|
+
spec.authors = ["MounirGaiby"]
|
|
11
|
+
spec.email = ["mounirgaiby@gmail.com"]
|
|
12
|
+
|
|
13
|
+
spec.summary = "Hedged requests middleware for Faraday to reduce tail latency."
|
|
14
|
+
spec.description = "Issue a backup request after a delay and return the first response."
|
|
15
|
+
spec.homepage = "https://github.com/elysium-arc/faraday-hedge"
|
|
16
|
+
spec.license = "MIT"
|
|
17
|
+
spec.required_ruby_version = ">= 3.0"
|
|
18
|
+
|
|
19
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
|
20
|
+
spec.metadata["source_code_uri"] = "https://github.com/elysium-arc/faraday-hedge"
|
|
21
|
+
spec.metadata["changelog_uri"] = "https://github.com/elysium-arc/faraday-hedge/blob/main/CHANGELOG.md"
|
|
22
|
+
spec.metadata["rubygems_mfa_required"] = "true"
|
|
23
|
+
|
|
24
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
|
25
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
|
26
|
+
end
|
|
27
|
+
spec.bindir = "bin"
|
|
28
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
|
29
|
+
spec.require_paths = ["lib"]
|
|
30
|
+
|
|
31
|
+
spec.add_dependency "faraday", ">= 1.0"
|
|
32
|
+
|
|
33
|
+
spec.add_development_dependency "bundler", ">= 1.17"
|
|
34
|
+
spec.add_development_dependency "rake", ">= 13.0"
|
|
35
|
+
spec.add_development_dependency "rspec", "~> 3.12"
|
|
36
|
+
spec.add_development_dependency "simplecov", "~> 0.22"
|
|
37
|
+
end
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "faraday"
|
|
4
|
+
require "faraday/hedge/version"
|
|
5
|
+
|
|
6
|
+
module Faraday
|
|
7
|
+
class Hedge
|
|
8
|
+
DEFAULT_DELAY = 0.1
|
|
9
|
+
DEFAULT_MAX_HEDGES = 1
|
|
10
|
+
|
|
11
|
+
def initialize(app, options = {})
|
|
12
|
+
@app = app
|
|
13
|
+
@delay = options.fetch(:delay, DEFAULT_DELAY)
|
|
14
|
+
@max_hedges = options.fetch(:max_hedges, DEFAULT_MAX_HEDGES)
|
|
15
|
+
@methods = Array(options.fetch(:methods, %i[get head])).map(&:to_sym)
|
|
16
|
+
@idempotent_only = options.fetch(:idempotent_only, true)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def call(env)
|
|
20
|
+
return @app.call(env) unless hedgeable?(env)
|
|
21
|
+
|
|
22
|
+
responses = Queue.new
|
|
23
|
+
primary_thread = Thread.new { responses << @app.call(env.dup) }
|
|
24
|
+
|
|
25
|
+
hedge_thread = Thread.new do
|
|
26
|
+
sleep @delay
|
|
27
|
+
# :nocov:
|
|
28
|
+
return if primary_thread.join(0)
|
|
29
|
+
# :nocov:
|
|
30
|
+
responses << @app.call(env.dup)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
response = responses.pop
|
|
34
|
+
[primary_thread, hedge_thread].each { |t| t.kill if t.alive? }
|
|
35
|
+
response
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
private
|
|
39
|
+
|
|
40
|
+
def hedgeable?(env)
|
|
41
|
+
return false if @idempotent_only && !@methods.include?(env.method)
|
|
42
|
+
@max_hedges > 0
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
Request.register_middleware(hedge: Faraday::Hedge)
|
|
47
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: faraday-hedge
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- MounirGaiby
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2026-02-03 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: faraday
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - ">="
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '1.0'
|
|
20
|
+
type: :runtime
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - ">="
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '1.0'
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: bundler
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - ">="
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '1.17'
|
|
34
|
+
type: :development
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - ">="
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '1.17'
|
|
41
|
+
- !ruby/object:Gem::Dependency
|
|
42
|
+
name: rake
|
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - ">="
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: '13.0'
|
|
48
|
+
type: :development
|
|
49
|
+
prerelease: false
|
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - ">="
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: '13.0'
|
|
55
|
+
- !ruby/object:Gem::Dependency
|
|
56
|
+
name: rspec
|
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
|
58
|
+
requirements:
|
|
59
|
+
- - "~>"
|
|
60
|
+
- !ruby/object:Gem::Version
|
|
61
|
+
version: '3.12'
|
|
62
|
+
type: :development
|
|
63
|
+
prerelease: false
|
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
65
|
+
requirements:
|
|
66
|
+
- - "~>"
|
|
67
|
+
- !ruby/object:Gem::Version
|
|
68
|
+
version: '3.12'
|
|
69
|
+
- !ruby/object:Gem::Dependency
|
|
70
|
+
name: simplecov
|
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
|
72
|
+
requirements:
|
|
73
|
+
- - "~>"
|
|
74
|
+
- !ruby/object:Gem::Version
|
|
75
|
+
version: '0.22'
|
|
76
|
+
type: :development
|
|
77
|
+
prerelease: false
|
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
79
|
+
requirements:
|
|
80
|
+
- - "~>"
|
|
81
|
+
- !ruby/object:Gem::Version
|
|
82
|
+
version: '0.22'
|
|
83
|
+
description: Issue a backup request after a delay and return the first response.
|
|
84
|
+
email:
|
|
85
|
+
- mounirgaiby@gmail.com
|
|
86
|
+
executables:
|
|
87
|
+
- console
|
|
88
|
+
- setup
|
|
89
|
+
extensions: []
|
|
90
|
+
extra_rdoc_files: []
|
|
91
|
+
files:
|
|
92
|
+
- ".github/workflows/ci.yml"
|
|
93
|
+
- ".gitignore"
|
|
94
|
+
- ".rspec"
|
|
95
|
+
- CHANGELOG.md
|
|
96
|
+
- CODE_OF_CONDUCT.md
|
|
97
|
+
- CONTRIBUTING.md
|
|
98
|
+
- Gemfile
|
|
99
|
+
- LICENSE.txt
|
|
100
|
+
- README.md
|
|
101
|
+
- Rakefile
|
|
102
|
+
- bin/console
|
|
103
|
+
- bin/setup
|
|
104
|
+
- faraday-hedge.gemspec
|
|
105
|
+
- lib/faraday/hedge.rb
|
|
106
|
+
- lib/faraday/hedge/version.rb
|
|
107
|
+
homepage: https://github.com/elysium-arc/faraday-hedge
|
|
108
|
+
licenses:
|
|
109
|
+
- MIT
|
|
110
|
+
metadata:
|
|
111
|
+
homepage_uri: https://github.com/elysium-arc/faraday-hedge
|
|
112
|
+
source_code_uri: https://github.com/elysium-arc/faraday-hedge
|
|
113
|
+
changelog_uri: https://github.com/elysium-arc/faraday-hedge/blob/main/CHANGELOG.md
|
|
114
|
+
rubygems_mfa_required: 'true'
|
|
115
|
+
post_install_message:
|
|
116
|
+
rdoc_options: []
|
|
117
|
+
require_paths:
|
|
118
|
+
- lib
|
|
119
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
120
|
+
requirements:
|
|
121
|
+
- - ">="
|
|
122
|
+
- !ruby/object:Gem::Version
|
|
123
|
+
version: '3.0'
|
|
124
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
125
|
+
requirements:
|
|
126
|
+
- - ">="
|
|
127
|
+
- !ruby/object:Gem::Version
|
|
128
|
+
version: '0'
|
|
129
|
+
requirements: []
|
|
130
|
+
rubygems_version: 3.3.3
|
|
131
|
+
signing_key:
|
|
132
|
+
specification_version: 4
|
|
133
|
+
summary: Hedged requests middleware for Faraday to reduce tail latency.
|
|
134
|
+
test_files: []
|