From 813ec1ab96687c02d1b10f93ca6c1de94b367d9f Mon Sep 17 00:00:00 2001 From: lyc8503 Date: Tue, 13 May 2025 16:13:35 +0800 Subject: [PATCH] fix #109: kill DO to avoid extra usage --- worker/src/index.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/worker/src/index.ts b/worker/src/index.ts index d4bb455..dbc2715 100644 --- a/worker/src/index.ts +++ b/worker/src/index.ts @@ -94,6 +94,12 @@ export default { locationHint: doLoc as DurableObjectLocationHint, }) resp = await doStub.getLocationAndStatus(monitor) + try { + // Kill the DO instance after use, to avoid extra resource usage + await doStub.kill() + } catch (err) { + // An error here is expected, ignore it + } } else { resp = await ( await fetch(monitor.checkProxy, { @@ -339,4 +345,12 @@ export class RemoteChecker extends DurableObject { status: status, } } + + async kill() { + // Throwing an error in `blockConcurrencyWhile` will terminate the Durable Object instance + // https://developers.cloudflare.com/durable-objects/api/state/#blockconcurrencywhile + this.ctx.blockConcurrencyWhile(async () => { + throw 'killed' + }) + } }