kwhitley / itty-durable

Cloudflare Durable Objects + Itty Router = shorter code
MIT License
251 stars 18 forks source link

Use `blockConcurrencyWhile` when constructor loads from storage #42

Closed mattfysh closed 1 year ago

mattfysh commented 1 year ago

According to the docs it's recommended to use blockConcurrencyWhile, e.g.

this.state.blockConcurrencyWhile(async () => {
  await this.loadFromStorage()
});

This ensures that while the object is being constructed, simultaneous requests don't come into the DO's methods - which may assume the memory has been hydrated from state storage

lmaccherone commented 1 year ago

I believe the example and guidance on that particular page of docs is superceded by the addition of input gates to the Durable Object system. See: https://blog.cloudflare.com/durable-objects-easy-fast-correct-choose-three/

You should still use it (or an equivalent) for hydrating from an external source using fetch() but it is no longer necessary for fetching from DO storage.

mattfysh commented 1 year ago

Oh good to know, thanks! Seem like they should update the docs to remove the recommendation, or at least highlight a valid use case for using it.