Class: Fly::RegionalDatabase::ReplayableRequestMiddleware
- Inherits:
-
Object
- Object
- Fly::RegionalDatabase::ReplayableRequestMiddleware
- Defined in:
- lib/fly-ruby/regional_database.rb
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app) ⇒ ReplayableRequestMiddleware
constructor
A new instance of ReplayableRequestMiddleware.
- #replay_request_state(header_value) ⇒ Object
- #replayable_http_method?(http_method) ⇒ Boolean
- #within_replay_threshold?(threshold) ⇒ Boolean
Constructor Details
#initialize(app) ⇒ ReplayableRequestMiddleware
46 47 48 |
# File 'lib/fly-ruby/regional_database.rb', line 46 def initialize(app) @app = app end |
Instance Method Details
#call(env) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/fly-ruby/regional_database.rb', line 62 def call(env) request = Rack::Request.new(env) # Does this request satisfiy a condition for replaying in the primary region? # # 1. Its HTTP method matches those configured for automatic replay # 2. It arrived before the threshold defined by the last write request. # This threshold helps avoid the same client from missing its own # write due to replication lag. if Fly.configuration.in_secondary_region? if replayable_http_method?(request.request_method) return RegionalDatabase.replay_in_primary_region!(state: "http_method") elsif within_replay_threshold?(request.[Fly.configuration.]) return RegionalDatabase.replay_in_primary_region!(state: "threshold") end end status, headers, body = @app.call(env) response = Rack::Response.new(body, status, headers) replay_state = replay_request_state(request.get_header("HTTP_FLY_REPLAY_SRC")) # Request was replayed, but not by a threshold, so set a threshold within which # all requests should be replayed to the primary region if replay_state && replay_state != "threshold" response.( Fly.configuration., Time.now.to_i + Fly.configuration.replay_threshold_in_seconds ) end response.finish end |
#replay_request_state(header_value) ⇒ Object
58 59 60 |
# File 'lib/fly-ruby/regional_database.rb', line 58 def replay_request_state(header_value) header_value&.slice(/(?:^|;)state=([^;]*)/, 1) end |
#replayable_http_method?(http_method) ⇒ Boolean
54 55 56 |
# File 'lib/fly-ruby/regional_database.rb', line 54 def replayable_http_method?(http_method) Fly.configuration.replay_http_methods.include?(http_method) end |
#within_replay_threshold?(threshold) ⇒ Boolean
50 51 52 |
# File 'lib/fly-ruby/regional_database.rb', line 50 def within_replay_threshold?(threshold) threshold && (threshold.to_i - Time.now.to_i) > 0 end |