effect-flock.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. import path from "path"
  2. import os from "os"
  3. import { randomUUID } from "crypto"
  4. import { Context, Effect, Function, Layer, Option, Schedule, Schema } from "effect"
  5. import type { FileSystem, Scope } from "effect"
  6. import type { PlatformError } from "effect/PlatformError"
  7. import { AppFileSystem } from "../filesystem"
  8. import { Global } from "../global"
  9. import { Hash } from "./hash"
  10. export namespace EffectFlock {
  11. // ---------------------------------------------------------------------------
  12. // Errors
  13. // ---------------------------------------------------------------------------
  14. export class LockTimeoutError extends Schema.TaggedErrorClass<LockTimeoutError>()("LockTimeoutError", {
  15. key: Schema.String,
  16. }) {}
  17. export class LockCompromisedError extends Schema.TaggedErrorClass<LockCompromisedError>()("LockCompromisedError", {
  18. detail: Schema.String,
  19. }) {}
  20. class ReleaseError extends Schema.TaggedErrorClass<ReleaseError>()("ReleaseError", {
  21. detail: Schema.String,
  22. cause: Schema.optional(Schema.Defect),
  23. }) {
  24. override get message() {
  25. return this.detail
  26. }
  27. }
  28. /** Internal: signals "lock is held, retry later". Never leaks to callers. */
  29. class NotAcquired extends Schema.TaggedErrorClass<NotAcquired>()("NotAcquired", {}) {}
  30. export type LockError = LockTimeoutError | LockCompromisedError
  31. // ---------------------------------------------------------------------------
  32. // Timing (baked in — no caller ever overrides these)
  33. // ---------------------------------------------------------------------------
  34. const STALE_MS = 60_000
  35. const TIMEOUT_MS = 5 * 60_000
  36. const BASE_DELAY_MS = 100
  37. const MAX_DELAY_MS = 2_000
  38. const HEARTBEAT_MS = Math.max(100, Math.floor(STALE_MS / 3))
  39. const retrySchedule = Schedule.exponential(BASE_DELAY_MS, 1.7).pipe(
  40. Schedule.either(Schedule.spaced(MAX_DELAY_MS)),
  41. Schedule.jittered,
  42. Schedule.while((meta) => meta.elapsed < TIMEOUT_MS),
  43. )
  44. // ---------------------------------------------------------------------------
  45. // Lock metadata schema
  46. // ---------------------------------------------------------------------------
  47. const LockMetaJson = Schema.fromJsonString(
  48. Schema.Struct({
  49. token: Schema.String,
  50. pid: Schema.Number,
  51. hostname: Schema.String,
  52. createdAt: Schema.String,
  53. }),
  54. )
  55. const decodeMeta = Schema.decodeUnknownSync(LockMetaJson)
  56. const encodeMeta = Schema.encodeSync(LockMetaJson)
  57. // ---------------------------------------------------------------------------
  58. // Service
  59. // ---------------------------------------------------------------------------
  60. export interface Interface {
  61. readonly acquire: (key: string, dir?: string) => Effect.Effect<void, LockError, Scope.Scope>
  62. readonly withLock: {
  63. (key: string, dir?: string): <A, E, R>(body: Effect.Effect<A, E, R>) => Effect.Effect<A, E | LockError, R>
  64. <A, E, R>(body: Effect.Effect<A, E, R>, key: string, dir?: string): Effect.Effect<A, E | LockError, R>
  65. }
  66. }
  67. export class Service extends Context.Service<Service, Interface>()("EffectFlock") {}
  68. // ---------------------------------------------------------------------------
  69. // Layer
  70. // ---------------------------------------------------------------------------
  71. function wall() {
  72. return performance.timeOrigin + performance.now()
  73. }
  74. const mtimeMs = (info: FileSystem.File.Info) => Option.getOrElse(info.mtime, () => new Date(0)).getTime()
  75. const isPathGone = (e: PlatformError) => e.reason._tag === "NotFound" || e.reason._tag === "Unknown"
  76. export const layer: Layer.Layer<Service, never, Global.Service | AppFileSystem.Service> = Layer.effect(
  77. Service,
  78. Effect.gen(function* () {
  79. const global = yield* Global.Service
  80. const fs = yield* AppFileSystem.Service
  81. const lockRoot = path.join(global.state, "locks")
  82. const hostname = os.hostname()
  83. const ensuredDirs = new Set<string>()
  84. // -- helpers (close over fs) --
  85. const safeStat = (file: string) =>
  86. fs.stat(file).pipe(
  87. Effect.catchIf(isPathGone, () => Effect.void),
  88. Effect.orDie,
  89. )
  90. const forceRemove = (target: string) => fs.remove(target, { recursive: true }).pipe(Effect.ignore)
  91. /** Atomic mkdir — returns true if created, false if already exists, dies on other errors. */
  92. const atomicMkdir = (dir: string) =>
  93. fs.makeDirectory(dir, { mode: 0o700 }).pipe(
  94. Effect.as(true),
  95. Effect.catchIf(
  96. (e) => e.reason._tag === "AlreadyExists",
  97. () => Effect.succeed(false),
  98. ),
  99. Effect.orDie,
  100. )
  101. /** Write with exclusive create — compromised error if file already exists. */
  102. const exclusiveWrite = (filePath: string, content: string, lockDir: string, detail: string) =>
  103. fs.writeFileString(filePath, content, { flag: "wx" }).pipe(
  104. Effect.catch(() =>
  105. Effect.gen(function* () {
  106. yield* forceRemove(lockDir)
  107. return yield* new LockCompromisedError({ detail })
  108. }),
  109. ),
  110. )
  111. const cleanStaleBreaker = Effect.fnUntraced(function* (breakerPath: string) {
  112. const bs = yield* safeStat(breakerPath)
  113. if (bs && wall() - mtimeMs(bs) > STALE_MS) yield* forceRemove(breakerPath)
  114. return false
  115. })
  116. const ensureDir = Effect.fnUntraced(function* (dir: string) {
  117. if (ensuredDirs.has(dir)) return
  118. yield* fs.makeDirectory(dir, { recursive: true }).pipe(Effect.orDie)
  119. ensuredDirs.add(dir)
  120. })
  121. const isStale = Effect.fnUntraced(function* (lockDir: string, heartbeatPath: string, metaPath: string) {
  122. const now = wall()
  123. const hb = yield* safeStat(heartbeatPath)
  124. if (hb) return now - mtimeMs(hb) > STALE_MS
  125. const meta = yield* safeStat(metaPath)
  126. if (meta) return now - mtimeMs(meta) > STALE_MS
  127. const dir = yield* safeStat(lockDir)
  128. if (!dir) return false
  129. return now - mtimeMs(dir) > STALE_MS
  130. })
  131. // -- single lock attempt --
  132. type Handle = { token: string; metaPath: string; heartbeatPath: string; lockDir: string }
  133. const tryAcquireLockDir = Effect.fn("EffectFlock.tryAcquire")(function* (lockDir: string) {
  134. const token = randomUUID()
  135. const metaPath = path.join(lockDir, "meta.json")
  136. const heartbeatPath = path.join(lockDir, "heartbeat")
  137. // Atomic mkdir — the POSIX lock primitive
  138. const created = yield* atomicMkdir(lockDir)
  139. if (!created) {
  140. if (!(yield* isStale(lockDir, heartbeatPath, metaPath))) return yield* new NotAcquired()
  141. // Stale — race for breaker ownership
  142. const breakerPath = lockDir + ".breaker"
  143. const claimed = yield* fs.makeDirectory(breakerPath, { mode: 0o700 }).pipe(
  144. Effect.as(true),
  145. Effect.catchIf(
  146. (e) => e.reason._tag === "AlreadyExists",
  147. () => cleanStaleBreaker(breakerPath),
  148. ),
  149. Effect.catchIf(isPathGone, () => Effect.succeed(false)),
  150. Effect.orDie,
  151. )
  152. if (!claimed) return yield* new NotAcquired()
  153. // We own the breaker — double-check staleness, nuke, recreate
  154. const recreated = yield* Effect.gen(function* () {
  155. if (!(yield* isStale(lockDir, heartbeatPath, metaPath))) return false
  156. yield* forceRemove(lockDir)
  157. return yield* atomicMkdir(lockDir)
  158. }).pipe(Effect.ensuring(forceRemove(breakerPath)))
  159. if (!recreated) return yield* new NotAcquired()
  160. }
  161. // We own the lock dir — write heartbeat + meta with exclusive create
  162. yield* exclusiveWrite(heartbeatPath, "", lockDir, "heartbeat already existed")
  163. const metaJson = encodeMeta({ token, pid: process.pid, hostname, createdAt: new Date().toISOString() })
  164. yield* exclusiveWrite(metaPath, metaJson, lockDir, "meta.json already existed")
  165. return { token, metaPath, heartbeatPath, lockDir } satisfies Handle
  166. })
  167. // -- retry wrapper (preserves Handle type) --
  168. const acquireHandle = (lockfile: string, key: string): Effect.Effect<Handle, LockError> =>
  169. tryAcquireLockDir(lockfile).pipe(
  170. Effect.retry({
  171. while: (err) => err._tag === "NotAcquired",
  172. schedule: retrySchedule,
  173. }),
  174. Effect.catchTag("NotAcquired", () => Effect.fail(new LockTimeoutError({ key }))),
  175. )
  176. // -- release --
  177. const release = (handle: Handle) =>
  178. Effect.gen(function* () {
  179. const raw = yield* fs.readFileString(handle.metaPath).pipe(
  180. Effect.catch((err) => {
  181. if (isPathGone(err)) return Effect.die(new ReleaseError({ detail: "metadata missing" }))
  182. return Effect.die(err)
  183. }),
  184. )
  185. const parsed = yield* Effect.try({
  186. try: () => decodeMeta(raw),
  187. catch: (cause) => new ReleaseError({ detail: "metadata invalid", cause }),
  188. }).pipe(Effect.orDie)
  189. if (parsed.token !== handle.token) return yield* Effect.die(new ReleaseError({ detail: "token mismatch" }))
  190. yield* forceRemove(handle.lockDir)
  191. })
  192. // -- build service --
  193. const acquire = Effect.fn("EffectFlock.acquire")(function* (key: string, dir?: string) {
  194. const lockDir = dir ?? lockRoot
  195. yield* ensureDir(lockDir)
  196. const lockfile = path.join(lockDir, Hash.fast(key) + ".lock")
  197. // acquireRelease: acquire is uninterruptible, release is guaranteed
  198. const handle = yield* Effect.acquireRelease(acquireHandle(lockfile, key), (handle) => release(handle))
  199. // Heartbeat fiber — scoped, so it's interrupted before release runs
  200. yield* fs
  201. .utimes(handle.heartbeatPath, new Date(), new Date())
  202. .pipe(Effect.ignore, Effect.repeat(Schedule.spaced(HEARTBEAT_MS)), Effect.forkScoped)
  203. })
  204. const withLock: Interface["withLock"] = Function.dual(
  205. (args) => Effect.isEffect(args[0]),
  206. <A, E, R>(body: Effect.Effect<A, E, R>, key: string, dir?: string): Effect.Effect<A, E | LockError, R> =>
  207. Effect.scoped(
  208. Effect.gen(function* () {
  209. yield* acquire(key, dir)
  210. return yield* body
  211. }),
  212. ),
  213. )
  214. return Service.of({ acquire, withLock })
  215. }),
  216. )
  217. export const defaultLayer = layer.pipe(Layer.provide(AppFileSystem.defaultLayer), Layer.provide(Global.layer))
  218. }