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
Returns a new instance of ReplayableRequestMiddleware.
43 44 45 |
# File 'lib/fly-ruby/regional_database.rb', line 43 def initialize(app) @app = app end |
Instance Method Details
#call(env) ⇒ Object
59 60 61 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 |
# File 'lib/fly-ruby/regional_database.rb', line 59 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
55 56 57 |
# File 'lib/fly-ruby/regional_database.rb', line 55 def replay_request_state(header_value) header_value&.scan(/(.*?)=(.*?)($|;)/)&.detect { |v| v[0] == "state" }&.at(1) end |
#replayable_http_method?(http_method) ⇒ Boolean
51 52 53 |
# File 'lib/fly-ruby/regional_database.rb', line 51 def replayable_http_method?(http_method) Fly.configuration.replay_http_methods.include?(http_method) end |
#within_replay_threshold?(threshold) ⇒ Boolean
47 48 49 |
# File 'lib/fly-ruby/regional_database.rb', line 47 def within_replay_threshold?(threshold) threshold && (threshold.to_i - Time.now.to_i) > 0 end |