kwhitley / itty-durable

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

Use `blockConcurrencyWhile` when constructor loads from storage #42

Closed mattfysh closed 10 months ago

mattfysh commented 10 months 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 10 months 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 10 months 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.