https://discord.cloudflare.com logo
Join Discord
Powered by
# durable-objects
  • u

    Unsmart | Tech debt

    01/22/2023, 7:07 PM
    But afaik if you have any async code in between storage ops there could be issues with concurrency. So like:
    Copy code
    ts
    const val = await storage.get('something')
    
    await someCode()
    
    await storage.put('something', val + 1)
    Could have issues
  • g

    Giggiux

    01/22/2023, 7:09 PM
    Essentially, yes. That's it. In the blog post they don't show this, because they wrap the async code and the last
    put
    in a wrapper
    async
    function
  • u

    Unsmart | Tech debt

    01/22/2023, 7:10 PM
    Basically the gates that block only apply to storage operations and not other async code
  • g

    Giggiux

    01/22/2023, 7:10 PM
    But as far as I can get, by just reading the blog post and the documentation's
    put
    notice, it would seems as it would be "smarter" and understand that the concurrency between the
    get
    and
    put
    should be avoided and/or let the following `get`s wait for the last
    put
    of the previous operation
  • g

    Giggiux

    01/22/2023, 7:11 PM
    yup, I wanna test if I can kinda cheat the system tho by just wrapping the
    await someCode()
    between
    blockConcurrencyWhile
  • u

    Unsmart | Tech debt

    01/22/2023, 7:17 PM
    Interesting, blockConcurrencyWhile seems to work pretty strange.
    Copy code
    ts
    this.app.post('/test/concurrency', async c => {
        console.log('Received request in DO', new Date().toISOString())
    
        await this.state.blockConcurrencyWhile(() => this.timeout(10000))
    
        console.log('Finished waiting in DO', new Date().toISOString())
    
        return c.json({ timeout: true })
    })
    With 2 concurrent requests made both requests take 20s to return, and the console.log for finished was put into the 2nd request for both
  • g

    Giggiux

    01/22/2023, 7:24 PM
    Random question: I know
    blockConcurrencyWhile
    returns a promise, but... do you need to await it to make it work? I am (mistakenly probably) using it without the
    await
    and seems to work exactly as expected Regarding the logs... Yeah it's pretty strange behaviour
  • u

    Unsmart | Tech debt

    01/22/2023, 7:26 PM
    ill test
  • u

    Unsmart | Tech debt

    01/22/2023, 7:28 PM
    Looks like you would have to await it. Both my console.logs have the same timestamp if I dont await it. But it does make it return response faster which is interesting
  • u

    Unsmart | Tech debt

    01/22/2023, 7:28 PM
    or well I guess it depends
  • u

    Unsmart | Tech debt

    01/22/2023, 7:29 PM
    Because if you only want the following code to run after the code in blockConcurrency
  • u

    Unsmart | Tech debt

    01/22/2023, 7:33 PM
    what would probably be best is:
    Copy code
    ts
    this.app.post('/test/concurrency', async c => {
        this.state.blockConcurrencyWhile(() => {
            console.log('Received request in DO', new Date().toISOString())
    
            await this.timeout(10000)
    
            console.log('Finished waiting in DO', new Date().toISOString())
        })
    
        return c.json({ timeout: true })
    })
  • g

    Giggiux

    01/22/2023, 7:36 PM
    Yup that's probably how I will end up doing it, btw what framework are you using?
  • u

    Unsmart | Tech debt

    01/22/2023, 7:36 PM
    https://github.com/honojs/hono
  • u

    Unsmart | Tech debt

    01/22/2023, 7:40 PM
    This is how I do the DO also if youre curious:
    Copy code
    ts
    export class TestDO implements DurableObject {
        private app = new Hono<Bindings>()
    
        constructor(private state: DurableObjectState, private env: Bindings) {
            this.app.get('/', async c => c.text('Hello!'))
    
            this.app.post('/test/concurrency', async c => {
                this.state.blockConcurrencyWhile(async () => {
                    console.log('Received request in DO', new Date().toISOString())
            
                    await scheduler.wait(10000)
            
                    console.log('Finished waiting in DO', new Date().toISOString())
                })
            
                return c.json({ timeout: true })
            })
        }
    
        async fetch(request: Request) {
            return this.app.fetch(request, this.env)
        }
    }
  • g

    Giggiux

    01/22/2023, 7:40 PM
    thanks! 😄
  • s

    steeldragon

    01/23/2023, 6:03 PM
    what is
    Copy code
    name
    supposed to be in
    Copy code
    DurableObjectId
    , is it supposed to be the name that i generated the id from?
  • h

    HardAtWork

    01/23/2023, 6:06 PM
    Yes
  • s

    steeldragon

    01/23/2023, 6:06 PM
    hmmm, it seems to only give me undefined... ill look at it again tomorrow
  • h

    HardAtWork

    01/23/2023, 6:08 PM
    Actually, I'm not 100% sure. I know that in some cases the name of a DO ID is unrecoverable.
  • h

    HardAtWork

    01/23/2023, 6:08 PM
    So maybe that's why?
  • s

    steeldragon

    01/23/2023, 6:09 PM
    i feel like it wasn't there before? maybe it's new an my DO is on an old version?
  • h

    HardAtWork

    01/23/2023, 6:11 PM
    Maybe. But I do know that it won't always be there(like when you use
    newUniqueId()
    ), so I'm not 100% how it is generated/persisted.
  • u

    Unsmart | Tech debt

    01/23/2023, 6:12 PM
    name was definitely not a thing before, and I thought they said it wouldnt be supported so you'd have to pass the name into the DO yourself but interesting if they added it 🤔
  • s

    steeldragon

    01/23/2023, 6:13 PM
    well thanks anyway, ill look at it more tomorrow
  • c

    ckoeninger

    01/23/2023, 7:41 PM
    name will be defined on the id you get back from idFromName in the eyeball worker
  • c

    ckoeninger

    01/23/2023, 7:41 PM
    name will not be defined on the id you get back from this.state.id in the durable object
  • c

    ckoeninger

    01/23/2023, 7:42 PM
    name will (obviously) not be defined on the id you get back from newUniqueId in the eyeball worker
  • c

    ckoeninger

    01/23/2023, 7:43 PM
    tldr if you need the name in the DO, pass it in the request. this hasn't changed
  • e

    ehesp

    01/24/2023, 1:24 PM
    If I'm using
    addEventListener
    , do I access the environment from global...
    Copy code
    export interface Env {
      MY_DURABLE_OBJECT: DurableObjectNamespace;
    }
    
    addEventListener('fetch', (event) => {
      MY_DURABLE_OBJECT.foo
    });
    How do I correctly handle that for TypeScript? is this the correct way?
    Copy code
    declare global {
      MY_DURABLE_OBJECT: DurableObjectNamespace;
    }
1...479480481...567Latest