Skip to content

Call the API through a service binding and forward the visitor #449

Closed

Description

@neuromechanist

Problem

Every server-side API call this site makes leaves from the same small pool of Cloudflare egress addresses, so the API's per-IP limiter counts all visitors as one client (nemar-cli#1709).
That is what produced the "Could not load datasets" errors on /discover, and it is also why the strict sign-in bucket is shared by every visitor (nemar-cli#1354).
The API side is nemar-cli#1710: a request with no CF-Connecting-IP (which is what a service-binding call is) is counted by the visitor in X-Forwarded-For.

Plan

  1. Code, inert without a binding: a single apiFetch helper that uses a service binding named API when the runtime provides one and the public network otherwise, and sets X-Forwarded-For to the visitor's CF-Connecting-IP (replacing whatever the visitor sent).
    Every server-side call to api.nemar.org, data.nemar.org and zarr.nemar.org goes through it.
    The request context (request-context.ts) carries the binding and the visitor address, so the catalog cache's background refresh is attributed to the visitor that triggered it.
  2. Wiring, after nemar-cli#1710 is deployed to the matching environment: [[services]] in wrangler.test.toml (to nemar-api-dev) first, then in wrangler.toml (to nemar-api).
    Wiring before the API change is deployed would put every call in the API's shared unattributed bucket, which is worse than today.
  3. Verify with the API's X-RateLimit-Bucket header and the [rate-limit] log lines named in nemar-cli ADR 0098.

Not in scope

The browser's own calls (they carry their own CF-Connecting-IP).

Activity

  1. neuromechanist commented on Oct 12, 2026

    @neuromechanist
    ContributorAuthor

    Status as of 2026-10-12.

    • API side: nemar-cli#1710 (a forwarded visitor is counted on their own, site-ip, fixed windows, anonymous cap 1000) merged to dev and deployed to nemar-api-dev; review follow-ups nemar-cli#1723 merged to dev (899ecb07).
    • Website code: apiFetch and the call sites (Call the API through a service binding that names the visitor #450, ADR 0026) and the wiring (Bind the API Worker for server-side calls #452) merged to staging.
    • test.nemar.org is bound to nemar-api-dev: its logs show the calls going over the binding with no errors. The development API bypasses its limiter, so the one warning there is expected ("carried no rate-limit headers"); staging proves the path, not the keying.
    • Production: needs nemar-cli 0.10.17 released to main and deployed to nemar-api first. The website release that carries [[services]] API -> nemar-api must not be promoted before that (AGENTS.md step 8, .context/handoff.md). Then check on nemar.org: X-RateLimit-Bucket: site-ip with a falling X-RateLimit-Remaining across two calls, and the [api-fetch] first-call line (site-ip, info) in the production deployment's tail.
    • Follow-ups: data.nemar.org and zarr.nemar.org through the binding (zone cache question); the browser-reachable API client modules (they cannot import apiFetch); website#451 (bound the detail calls a Discover search makes per page); nemar-cli#1712 to #1718 (review follow-ups filed from #1710).
  2. neuromechanist commented on Oct 13, 2026

    @neuromechanist
    ContributorAuthor

    Shipped in 0.2.31, promoted to production through #457 (merge commit 8468a2e, tag v0.2.31). The service binding (API to nemar-api), apiFetch and the converted call sites, including the news pages from #456, are live; the production site logs show calls counted per visitor in the API's site-ip bucket (nemar-cli#1709 has the evidence). Closing by hand, since a merge to staging does not close issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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