Skip to content

binding-http: per-route affinity extraction #1744

Closed

Description

@jfallows

Overview

Add a generic mechanism for the binding-http server kind to extract a routing key from an inbound request (header or query parameter, with optional regex extraction) and use it to dispatch the resulting application stream to a consistent worker thread within the gateway instance.

Motivation

Today the http-server binding propagates whatever affinity flows up from the network layer (TCP/TLS connection-level). Multiple HTTP requests for the same logical session can land on different worker threads within an instance, breaking per-worker session state assumptions held by upstream bindings.

A fresh HTTP request for an existing session-affined flow may not carry the original session header, but does carry an opaque token (e.g., a state query parameter) from which the session identifier can be derived. Routing it correctly requires extracting the key from the request content rather than connection metadata.

Configuration schema

Per-route with: affinity: block:

bindings:
  http_server0:
    type: http
    kind: server
    routes:
      - when:
          - path: /example/auth/callback
        with:
          affinity:
            query: state
            match: "[^.]+"
        exit: example_server0
      - when:
          - path: /example
          - path: /example/*
        with:
          affinity:
            header: x-session-id
        exit: example_server0

with: affinity:

  • Exactly one source: header: xor query: .
  • match: (optional) — regex applied to the source value. If the regex has capture groups, group 1 is used; otherwise the full match. If the regex doesn't match, the rule yields no key and the route falls through.
  • match: omitted — the full source value is used as the key.
  • Source value absent (header / query parameter not present) — rule yields no key, route falls through.
  • Route without with: affinity: — no extraction, network-level affinity propagates as today.

Behavior

For each inbound request, after route matching:

  1. Resolve with: affinity: for the matched route; extract the key string per the rules above. If no key is extracted, fall through to existing behavior.
  2. Hash the key to a long and emit it as the affinity on the outgoing application BEGIN. The engine routes the application stream to the matching worker.

Implementation notes

  • HttpServerFactory already builds the outgoing application BEGIN with the inbound network affinity. The new logic compiles the per-route affinity rules at config load and applies them when the route matches, overriding the affinity field on the outgoing BEGIN with the hashed extracted key.

Spec coverage

New scenarios under specs/binding-http.spec/src/main/scripts/.../streams/network/:

  • request.with.header.affinity — header-based extraction, no regex.
  • request.with.query.affinity — query-based extraction with regex, gated by path when.
  • request.with.affinity.regex.captured — regex with capture group.
  • request.with.affinity.no.match — source present but regex does not match → falls through to network-level affinity.
  • request.with.affinity.source.absent — header / query parameter absent → falls through.

Each scenario gets a NetworkIT method.

Acceptance criteria

  • Routes with with: affinity: extract the configured key; outgoing application BEGIN carries the hashed key as its affinity field.
  • Routes without with: affinity: are unchanged in behavior.
  • Configuration schema validates: incompatible / unsupported combinations rejected at startup.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions