fix #109: kill DO to avoid extra usage

pull/120/head
lyc8503 2025-05-13 16:13:35 +08:00
parent 4bcf4173ea
commit 813ec1ab96
1 changed files with 14 additions and 0 deletions

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'
})
}
}