-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: better organise the idempotent request public interface (#10)
* refactor: better organise the idempotent request public interface * refactor: remove request statuses * refactor: add get and set template methods * chore: bump Ruby min version to 2.6 and add Redis as dep * refactor: rescue Redis::BaseError * refactor: extract magic number into DEFAULT_EXPIRATION * refactor: add thread-safety through mutex for memory store * refactor: extract handle_request! --------- Co-authored-by: Matteo Rossi <[email protected]>
- Loading branch information
1 parent
033aedd
commit 0f07fb2
Showing
16 changed files
with
167 additions
and
64 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
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 |
---|---|---|
|
@@ -18,7 +18,7 @@ jobs: | |
strategy: | ||
fail-fast: false | ||
matrix: | ||
ruby: [2.5, 2.6, 2.7, '3.0', 3.1, 3.2, 3.3, 3.4, head] | ||
ruby: [2.6, 2.7, '3.0', 3.1, 3.2, 3.3, 3.4, head] | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- uses: ruby/setup-ruby@v1 | ||
|
@@ -34,7 +34,7 @@ jobs: | |
- uses: ruby/setup-ruby@v1 | ||
with: | ||
bundler-cache: true | ||
ruby-version: 2.5 | ||
ruby-version: 2.6 | ||
- run: bundle install | ||
- name: Test & publish code coverage | ||
uses: paambaati/[email protected] | ||
|
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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec| | |
spec.email = ["[email protected]"] | ||
spec.summary = "A Rack Middleware implementing the idempotency principle" | ||
spec.homepage = "https://github.com/matteoredz/rack-idempotency_key" | ||
spec.required_ruby_version = Gem::Requirement.new(">= 2.5.0") | ||
spec.required_ruby_version = Gem::Requirement.new(">= 2.6.0") | ||
spec.metadata["homepage_uri"] = spec.homepage | ||
spec.metadata["source_code_uri"] = "https://github.com/matteoredz/rack-idempotency_key" | ||
spec.metadata["changelog_uri"] = "https://github.com/matteoredz/rack-idempotency_key/CHANGELOG.md" | ||
|
@@ -24,4 +24,6 @@ Gem::Specification.new do |spec| | |
spec.bindir = "exe" | ||
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } | ||
spec.require_paths = ["lib"] | ||
|
||
spec.add_runtime_dependency "redis", "~> 5.2" | ||
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 |
---|---|---|
@@ -1,42 +1,45 @@ | ||
# frozen_string_literal: true | ||
|
||
require "rack/idempotency_key/version" | ||
require "rack/idempotency_key/error" | ||
|
||
# Stores | ||
require "rack/idempotency_key/memory_store" | ||
require "rack/idempotency_key/redis_store" | ||
|
||
# Collaborators | ||
require "rack/idempotency_key/idempotent_request" | ||
require "rack/idempotency_key/request" | ||
|
||
module Rack | ||
class IdempotencyKey | ||
Error = Class.new(StandardError) | ||
|
||
def initialize(app, routes: [], store: MemoryStore.new) | ||
@app = app | ||
@routes = routes | ||
@store = store | ||
end | ||
|
||
def call(env) | ||
request = IdempotentRequest.new(Rack::Request.new(env), routes) | ||
request = Request.new(Rack::Request.new(env), routes, store) | ||
return app.call(env) unless request.allowed? | ||
|
||
cached_response = store.get(request.idempotency_key) | ||
|
||
if cached_response | ||
cached_response[1]["Idempotent-Replayed"] = true | ||
return cached_response | ||
end | ||
|
||
app.call(env).tap do |response| | ||
store.set(request.idempotency_key, response) if response[0] != 400 | ||
end | ||
handle_request!(request, env) | ||
rescue Request::ConflictError | ||
[409, { "Content-Type" => "text/plain" }, ["Conflict"]] | ||
rescue Store::Error => e | ||
[503, { "Content-Type" => "text/plain" }, [e.message]] | ||
end | ||
|
||
private | ||
|
||
attr_reader :app, :store, :routes | ||
|
||
def handle_request!(request, env) | ||
request.with_lock! do | ||
cached_response = request.cached_response! | ||
return cached_response unless cached_response.nil? | ||
|
||
app.call(env).tap { |response| request.cache!(response) } | ||
end | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# frozen_string_literal: true | ||
|
||
module Rack | ||
class IdempotencyKey | ||
# Base error class for all IdempotencyKey errors | ||
Error = Class.new(StandardError) | ||
|
||
class Request | ||
# Error raised for general idempotent request failures | ||
Error = Class.new(Error) | ||
# Error raised when a conflicting idempotent request is detected | ||
ConflictError = Class.new(Error) | ||
end | ||
|
||
class Store | ||
Error = Class.new(Error) | ||
end | ||
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
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,18 @@ | ||
# frozen_string_literal: true | ||
|
||
module Rack | ||
class IdempotencyKey | ||
class Store | ||
DEFAULT_EXPIRATION = 86_400 # 24 hours in seconds | ||
|
||
def initialize(store, expires_in: DEFAULT_EXPIRATION) | ||
@store = store | ||
@expires_in = expires_in | ||
end | ||
|
||
protected | ||
|
||
attr_reader :store, :expires_in | ||
end | ||
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
Oops, something went wrong.