Class: XeroKiwi::Throttle::Middleware

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
lib/xero_kiwi/throttle/middleware.rb

Overview

Faraday request middleware. On every outbound call it reads the ‘Xero-Tenant-Id` header and asks the limiter for a token. Requests without a tenant header (e.g. `/connections`, OAuth endpoints) pass straight through — they have no tenant bucket to check.

Placement matters: this sits below faraday-retry in the stack, so every retry attempt also re-enters the limiter and consumes a token.

Constant Summary collapse

TENANT_HEADER =
"Xero-Tenant-Id"

Instance Method Summary collapse

Constructor Details

#initialize(app, limiter) ⇒ Middleware

Returns a new instance of Middleware.



17
18
19
20
# File 'lib/xero_kiwi/throttle/middleware.rb', line 17

def initialize(app, limiter)
  super(app)
  @limiter = limiter
end

Instance Method Details

#on_request(env) ⇒ Object



22
23
24
25
26
27
# File 'lib/xero_kiwi/throttle/middleware.rb', line 22

def on_request(env)
  tenant_id = env.request_headers[TENANT_HEADER]
  return if tenant_id.nil? || tenant_id.empty?

  @limiter.acquire(tenant_id)
end