fix #109: kill DO to avoid extra usage

This commit is contained in:
lyc8503 committed 2025-05-13 16:13:35 +08:00
1 parent 4bcf4173ea
commit 813ec1ab96
1 file changed
+14
+14
View File
@@ -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'
})
}
}