syncguard
Version:
Functional TypeScript library for distributed locking across microservices. Prevents race conditions with Redis, PostgreSQL, Firestore, and custom backends. Features automatic lock management, timeout handling, and extensible architecture.
29 lines (28 loc) • 1.19 kB
TypeScript
/**
* Canonical time authority predicates for cross-backend consistency.
* ALL backends MUST use these functions - custom time logic is forbidden.
* @see docs/specs/interface.md
*/
/**
* Canonical liveness check used by all backends.
* Formula: `expiresAtMs > nowMs - toleranceMs` handles clock skew gracefully.
*
* @param expiresAtMs - Lock expiration timestamp from storage
* @param nowMs - Current time from backend's authority (server/client)
* @param toleranceMs - Clock skew tolerance in ms
* @returns true if lock is still live
*/
export declare function isLive(expiresAtMs: number, nowMs: number, toleranceMs: number): boolean;
/**
* Converts Redis TIME command output to milliseconds.
*
* @param redisTime - redis.call('TIME') returns [seconds, microseconds]
* @returns server time in ms
*/
export declare function calculateRedisServerTimeMs(redisTime: [string, string]): number;
/**
* Fixed 1000ms tolerance for all backends (ADR-005).
* Accommodates network delays and clock skew while ensuring predictable cross-backend behavior.
* Not user-configurable to prevent semantic drift between Redis/Firestore.
*/
export declare const TIME_TOLERANCE_MS = 1000;