Durable Objects for D1 transactions, ie: mutex

For Workers & Pages, what is the name of the domain?

na

What is the issue or error you’re encountering

na

What are the steps to reproduce the issue?

Can Durable Objects be used as a mutex for D1? Or is there some better way to implement transactions with D1?

Example (copied Counter example and modified):

export class Mutex extends DurableObject {

  async lock(someID) {
    let value = await this.ctx.storage.get(someID)
    if(value) {
        return false
    }
    await this.ctx.storage.put(someID, true)
    return true
  }

  async unlock(someID) {
    await this.ctx.storage.delete(someID)
    return true
  }
}

Then call lock until the lock for the specific ID frees up.

Of course I’m worried that unlock may never get called which would break this.

Also not sure if this would be better with a new durable objects for every lock ID or one durable objects with lock keys (like above).

Is that a reasonable use for Durable objects?

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.