Publication fence
For publishers whose ownership moves between processes. Its holder is the
WorkManager Runtime: the fence is a started worker's lease, keyed by the
worker id, claimed before the worker starts and checked before every outbox
batch (openspec worker-management, Worker Lease). A
WebSocketFeed worker, one WebSocket
connection, publishes its frames under it. Spec: openspec api-gateway,
Publication Fence (introduced by the change 2026-10-15-fenced-publication).
It runs on Dapr building blocks only: any Dapr state store with the ETAG and
TRANSACTIONAL capabilities (the same ones an actor state store needs). The
gateway checks them through the sidecar's metadata and answers
FAILED_PRECONDITION if they are missing. PublicationStateStore names the
store (default statestore). Every change is one ETag-guarded write of a
single key, the one thing all such stores make atomic.
Fence
ChangePublicationFence manages a per-holder fence (per worker): token, active flag
and lease (lease_seconds).
- A fresh
new_tokenclaims the fence whenexpected_tokenis current, the fence is inactive or absent, or its lease expired; otherwiseABORTED. new_token == expected_tokenrenews (active) or revokes (!active), for the current holder only, and never reactivates an inactive fence.
Lease times use the gateway's UTC clock. A sweep every 30 seconds removes fences that are inactive or expired and older than two leases.
Heartbeat fencing
Frames are not checked one by one: the holder publishes with the regular
PublishEvent and keeps the fence by renewing it (the Runtime: every
second, lease 10 seconds).
- When a successor claims the fence, the previous holder's next renewal is
rejected with
ABORTEDand it stops the worker (a WebSocketFeed closes its socket). A stale owner therefore publishes for at most one renewal interval after the claim; during that window frames of both owners can interleave. - A holder that cannot renew stops publishing once its lease lapses.
- A holder that crashed without revoking delays a successor by at most one lease.
Cost: one renewal (one read and one guarded write) per connection per
interval, independent of the event rate; nothing per event. Measured with the
opt-in benchmark (FENCE_BENCH=true FENCE_DAPR_TESTS=true, local daprd), a
renewal takes about 0.55 ms on Redis and PostgreSQL.
Holders publish with the regular PublishEvent/PublishEvents.
Keys
From the virtufin.service.api naming fragment:
sys.api.publication.fence.<id> (the fragment still calls the identity
connection_id; the Runtime passes the worker id) with the index
sys.api.publication.fence.index. FencedPublicationSweeper removes fences
abandoned for two leases.
Limits
This is publication safety within one renewal interval, not physical WebSocket exclusivity, nor caller authorization beyond the gateway key model. Loss or rollback of the state store loses fences.