Class: Insika::Router::App

Inherits:
Object
  • Object
show all
Defined in:
lib/insika/router/app.rb

Overview

The standalone Rack/Async app: session-key extraction → consistent-hash pick → proxy, on the same Async/Falcon stack the engine already runs on. A request whose key has no live owner (nothing found, or the ring itself is empty) round-robins; a request whose chosen backend is unreachable answers the retry envelope — it is NOT retried against a different backend (§3.5): that backend may already hold a durable, at-most-once claim on the task this request names.

Constant Summary collapse

DEFAULT_BODY_MAX_BYTES =

256 KiB — small JSON control payloads, never uploads (§5)

262_144
RETRY_AFTER_SECONDS =
1
HOP_BY_HOP =
%w[connection keep-alive proxy-connection transfer-encoding upgrade host content-length].freeze

Instance Method Summary collapse

Constructor Details

#initialize(pool:, body_max_bytes: DEFAULT_BODY_MAX_BYTES, backend_timeout: 10, logger: $stdout, client_factory: DEFAULT_CLIENT_FACTORY) ⇒ App

client_factory: (backend_url, timeout) -> an object answering #call(request) -> Protocol::HTTP::Response. Defaults to a real Async::HTTP::Client; a spec injects a fake instead of opening real sockets.



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/insika/router/app.rb', line 28

def initialize(pool:, body_max_bytes: DEFAULT_BODY_MAX_BYTES, backend_timeout: 10, logger: $stdout,
               client_factory: DEFAULT_CLIENT_FACTORY)
  @pool = pool
  @body_max_bytes = body_max_bytes
  @backend_timeout = backend_timeout
  @logger = logger
  @client_factory = client_factory
  @clients = {} # backend address -> memoized client (persistent connections)
  @clients_mutex = Mutex.new
  @rr_index = -1
end

Instance Method Details

#call(env) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/insika/router/app.rb', line 45

def call(env)
  req = Rack::Request.new(env)
  segments = req.path_info.split("/").reject(&:empty?)

  return health_response if req.request_method == "GET" && segments == ["up"]

  raw_body = read_body(env)
  backend = pick_backend(req.request_method, segments, raw_body)
  return unavailable_response if backend.nil?

  proxy(req, backend, raw_body)
end