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

    kenton

    04/22/2021, 7:03 PM
    idFromName()
    always returns instantly. It computes the ID using an algorithm. There's no need to do any network I/O. So there's no performance difference between using
    idFromName()
    vs.
    idFromString()
    (which takes the raw hex ID). With all that said,
    newUniqueId()
    produces IDs that are faster to look up than those produced by
    idFromName()
    .
  • k

    kenton

    04/22/2021, 7:04 PM
    (The reason for the performance difference is because
    idFromName()
    needs to check whether the same name is used anywhere else in the world.
    newUniqueId()
    doesn't need to do any such check because it knows what it returns is unique.)
  • v

    vans163

    04/22/2021, 8:12 PM
    Sounds very (walltime) expensive like a 500ms global lock?
  • v

    vans163

    04/22/2021, 8:12 PM
    Il try to start using the internal id
  • k

    kenton

    04/22/2021, 8:13 PM
    It doesn't require a global lock, but it does require choosing one location in the world to be the "source of truth" about where that object lives, and if you're unlucky the chosen location might be on the other side of the world.
  • k

    kenton

    04/22/2021, 8:13 PM
    but it'll then be cached so it really only affects the first request
  • v

    vans163

    04/22/2021, 8:13 PM
    Like I could do idFromName, then inside the DO do state.id.tostring, then do idfromstring on another DO, to get back to the named one?
  • v

    vans163

    04/22/2021, 8:14 PM
    Hum
  • k

    kenton

    04/22/2021, 8:14 PM
    Sure but that doesn't accomplish anything vs. just using the name everywhere.
  • k

    kenton

    04/22/2021, 8:15 PM
    to be clear, the difference in behavior is encoded into the ID itself. The system knows the difference between an ID originally created using
    idFromName()
    vs. one created using
    newUniqueId()
  • v

    vans163

    04/22/2021, 8:16 PM
    as long as the idFromName id WONT change if the DO shutsdown and starts again (but has same name) il be good
  • k

    kenton

    04/22/2021, 8:18 PM
    that's correct, it never changes
  • v

    vans163

    04/22/2021, 8:19 PM
    to be clear
    Copy code
    javascript
      let id = env.USER.idFromName("username:bob")
      let obj = env.USER.get(id)
      return obj.fetch(request.url, init);
    // inside USER bob DO
       internal_id = state.id.toString()
       let id = env.RING.idFromName("north_america")
       let obj = env.RING.get(id)
       return obj.fetch(request.url, {internal_id: internal_id});
    // inside RING do
       env.USER.idFromString(internal_id_we_got_from_USER)
        ..
  • v

    vans163

    04/22/2021, 8:19 PM
    assuming USER bob DO and RING do can restart multiple times
  • v

    vans163

    04/22/2021, 8:20 PM
    and RING should not lose the mapping (stores it in storage API)
  • k

    kenton

    04/22/2021, 8:22 PM
    env.USER.idFromName("username:bob")
    will always return exactly the same value forever, regardless of whether the object is running or even exists.
  • v

    vans163

    04/22/2021, 8:22 PM
    that id == to state.id inside the DO?
  • k

    kenton

    04/22/2021, 8:22 PM
    (however, the same name for a different class will produce a different ID)
  • k

    kenton

    04/22/2021, 8:22 PM
    yes
  • v

    vans163

    04/22/2021, 8:23 PM
    perfect then, i could even store the state.id as state.id for 1 less operation right (into Storage API)? i dont need to toString() it?
  • k

    kenton

    04/22/2021, 8:23 PM
    I think you do need to
    toString()
    it
  • v

    vans163

    04/22/2021, 8:23 PM
    only toString for sending it over the fetch
  • k

    kenton

    04/22/2021, 8:23 PM
    and then you'd use
    idFromString(stringifiedId)
    to parse it
  • v

    vans163

    04/22/2021, 8:24 PM
    kk il keep it as string just incase
  • v

    vans163

    04/22/2021, 8:24 PM
    actually itl be easy to test
  • k

    kenton

    04/22/2021, 8:24 PM
    we might fix that sometime, it just requires that we extend the "structured clone" serialization mechanism to support the ID type
  • v

    vans163

    04/22/2021, 8:25 PM
    not a big deal tbh. so far liking DOs, my biggest pain point is debugging whats going on but made a few utility funcs (since I have a tick inside 1 of my DOs, that processes work indirectly on all the connected websockets, just made it broadcast a msg to all if it throws)
  • m

    molmorg

    04/22/2021, 10:29 PM
    it originally was
    ./index.mjs
    but I played I added ./dist to test if that was the cause but no. I just got it working... by adding the
    --new-class LogTopic
    switch.... that's a pretty misleading error - sure it used to be different and more specific if you didn't have that switch and hadn't previously deployed the class.
  • d

    Deleted User

    04/23/2021, 1:40 AM
    is there a way to set some kind of expiry on a durable object?
  • d

    Deleted User

    04/23/2021, 1:40 AM
    something like expiry for kv, except that i need that strong consistency
1...697071...567Latest