How the Magnitude SDK Handles Fixed-Endpoint Admission and Recovery
The Magnitude SDK implements fixed-endpoint admission and recovery through a single-permit semaphore that serializes connection attempts paired with a recovering JIT-RPC transport layer that automatically re-establishes interrupted streams.
The magnitudedev/magnitude SDK provides deterministic connection management for its ACN daemon through robust fixed-endpoint admission and recovery mechanisms. These systems ensure that client operations only proceed after confirming endpoint availability while gracefully handling transient network failures without resource leaks.
Understanding Fixed-Endpoint Admission
The Admission Semaphore Pattern
At the core of the admission system lies a single-permit semaphore created via Effect.makeSemaphore(1) in packages/sdk/src/client.ts (lines 134–141). This concurrency primitive guarantees that only one admission attempt can execute at any given time, preventing race conditions when multiple concurrent calls target the same fixed endpoint.
The SDK wraps all critical connection logic within admission.withPermits(1) blocks (lines 210–341 in the same file). This exclusive access pattern ensures that endpoint validation, health checks, and connection establishment occur atomically before any RPC method proceeds.
Service Starter and Endpoint Validation
The packages/sdk/src/service-starter.ts module orchestrates the actual admission workflow. When initializing a connection to a concrete service endpoint, the starter first acquires the admission semaphore, then performs health validation against the target daemon. If the endpoint is temporarily unavailable, the starter holds the lock and retries until the service confirms availability, ensuring the client only proceeds with verified connectivity.
Automatic Recovery Mechanisms
Recovering JIT-RPC Protocol
When streams fail mid-operation, the SDK’s recovering transport layer takes over. Located in packages/sdk/src/jit-rpc/recovering-protocol.ts, this protocol detects transport interruptions and automatically initiates recovery sequences. Unlike naive retry logic, this system coordinates with the admission semaphore to ensure recovery attempts respect the same serialization guarantees as initial connections.
Re-Issuing Demands After Stream Failure
The recovery system demonstrates its behavior in packages/sdk/src/jit-rpc/recovering-protocol.test.ts at line 325, where the test suite validates demand re-issuance after stream retirement. When a connection drops, the protocol captures the pending demand and re-issues it through recoverUnary once admission succeeds again. This pattern prevents message loss during transient failures while maintaining exactly-once semantics for the caller.
Error Handling When Admission Fails
For permanent failures such as timeouts or unreachable endpoints, the SDK emits descriptive errors from packages/sdk/src/connection-errors.ts. The ConnectionError type propagates to higher-level client code, surfacing clear "admission failed" messages without infinite retry loops. This deterministic failure mode allows applications to implement circuit breakers or fallback logic based on explicit error states.
Implementation Examples
The following patterns demonstrate how to interact with the admission and recovery systems:
// Example: obtaining an SDK client that guarantees admission
import { makeClient } from "@magnitudedev/sdk";
const client = await makeClient({
// The endpoint is a fixed URL for the ACN daemon
endpoint: "http://localhost:8080",
});
// All calls are wrapped in the admission semaphore automatically
const result = await client.someRpcMethod({ foo: "bar" });
// Inside the SDK – admission semaphore usage (simplified)
const admission = yield* Effect.makeSemaphore(1);
yield* admission.withPermits(1)(
Effect.gen(function* () {
// Attempt to open the fixed endpoint
const conn = yield* acquireConnection(endpoint);
// If the connection is lost, the recovering protocol will retry
return conn;
})
);
// Recovering JIT-RPC – re-issuing a demand after a stream retires
import { recoverUnary } from "./recovering-protocol";
const response = yield* recoverUnary({
demand: () => client.unaryCall(request),
admission,
});
Summary
- Semaphore-guarded admission: A single-permit semaphore in
client.tsensures atomic endpoint validation and prevents concurrent admission races. - Fixed-endpoint validation: The service starter performs health checks before releasing the admission lock to calling code.
- Automatic stream recovery: The recovering JIT-RPC protocol re-issues pending demands after reconnecting, as demonstrated in the test suite.
- Deterministic error propagation: Permanent failures emit
ConnectionErrortypes fromconnection-errors.tsrather than hanging indefinitely. - Resource safety: All recovery mechanisms coordinate with the admission lock to prevent resource leaks during reconnection storms.
Frequently Asked Questions
What triggers fixed-endpoint admission recovery in the Magnitude SDK?
Recovery triggers when the JIT-RPC transport detects a stream interruption, such as network partition or daemon restart. The recovering-protocol.ts module captures the pending demand and waits for the admission semaphore before re-issuing the request, ensuring serialized access to the recovering endpoint.
How does the semaphore prevent race conditions during admission?
The Effect.makeSemaphore(1) creates a single-permit semaphore that allows only one concurrent admission attempt across all client operations. By wrapping connection logic in admission.withPermits(1), the SDK ensures that health checks and connection establishment occur atomically, preventing multiple threads from simultaneously initializing the same fixed endpoint.
What happens when admission fails permanently?
When the service starter cannot establish connectivity within timeout constraints, it raises a ConnectionError defined in packages/sdk/src/connection-errors.ts. This error propagates to the caller without further retry attempts, allowing application-level error handling rather than indefinite blocking.
Where is the recovering protocol implemented?
The recovering protocol resides in packages/sdk/src/jit-rpc/recovering-protocol.ts, with comprehensive test coverage in packages/sdk/src/jit-rpc/recovering-protocol.test.ts. Line 325 of the test file specifically validates that the system re-issues unary demands after admission succeeds following a stream failure.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →