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

    kenton

    09/23/2021, 3:01 PM
    Fun fact... the problem turned out to be due to a change in the Go standard libraries. https://github.com/golang/go/issues/45789
  • w

    wes

    09/23/2021, 3:46 PM
    any clue why I see this and how to fix or clear the old namespace?
    Copy code
    workers.api.error.durable_object_namespace_name_taken [API code: 10065]
  • b

    brett

    09/23/2021, 3:55 PM
    @User Hmm, you can use the HTTP API (Auth details at the top here, you'll want an API Token that can read workers): https://api.cloudflare.com/ And hit
    https://api.cloudflare.com/client/v4/accounts/$ACCOUNT/workers/durable_objects/namespaces
    to list your namespaces -- you must have one with the same name from before? You can delete it via that API too by sending a
    DELETE
    to
    https://api.cloudflare.com/client/v4/accounts/$ACCOUNT/workers/durable_objects/namespaces/$NAMESPACE_ID
  • w

    wes

    09/23/2021, 4:33 PM
    you sir are my favorite person! 🙂 THANKS
  • k

    kalepail

    09/23/2021, 4:49 PM
    Huh interesting. Looking forward
  • w

    wes

    09/23/2021, 5:05 PM
    one more quick question. I'm using this Counter constructor and it seems like the number resets after an indeterminate period of time. Any insight into why?
    Copy code
    constructor(state, env) {
        this.state = state
        // `blockConcurrencyWhile()` ensures no requests are delivered until
        // initialization completes.
        this.state.blockConcurrencyWhile(async () => {
          let stored = await this.state.storage.get('value')
          this.value = stored || 10
        })
      }
  • b

    brett

    09/23/2021, 5:11 PM
    I think we'd need to see more of the script, how are you incrementing the counter? Are you persisting it in storage?
  • b

    brett

    09/23/2021, 5:11 PM
    Without knowing more it sounds like you're only incrementing it in memory, and so it resets if the object is evicted
  • w

    wes

    09/23/2021, 5:15 PM
    Copy code
    module.exports = class Counter {
      constructor(state, env) {
        this.state = state
        // `blockConcurrencyWhile()` ensures no requests are delivered until
        // initialization completes.
        this.state.blockConcurrencyWhile(async () => {
          let stored = await this.state.storage.get('value')
          this.value = stored || 10
        })
      }
    
      // Handle HTTP requests from clients.
      async fetch(request, options) {
        // Apply requested action.
        let url = new URL(request.url)
        let currentValue = this.value
    
        const pathname = url.pathname.split('/')
        const path = pathname[1]
        const amount = pathname[2]
    
        switch (path) {
          case 'increment':
            this.value += amount
            currentValue = this.value
            await this.state.storage.put('value', this.value)
            break
          case 'decrement':
            this.value -= amount
            currentValue = this.value
            await this.state.storage.put('value', this.value)
            break
          case '':
            // Just serve the current value. No storage calls needed!
            break
          default:
            return new Response('Not found', { status: 404 })
        }
    
        // Return `currentValue`. Note that `this.value` may have been
        // incremented or decremented by a concurrent request when we
        // yielded the event loop to `await` the `storage.put` above!
        // That's why we stored the counter value created by this
        // request in `currentValue` before we used `await`.
        return new Response(currentValue)
      }
    }
  • w

    wes

    09/23/2021, 5:18 PM
    sometimes just resets to 10 after a while
  • b

    brett

    09/23/2021, 5:20 PM
    Is that the demo counter unaltered?
  • b

    brett

    09/23/2021, 5:20 PM
    How are you accessing the object, are you sure it's the same one every time?
  • w

    wes

    09/23/2021, 5:27 PM
    roughly yeah
  • w

    wes

    09/23/2021, 5:27 PM
    Copy code
    async function handleCount(url, env) {
      let id = env.COUNTER.idFromName('A');
      let obj = env.COUNTER.get(id);
      let resp = await obj.fetch(url);
      let count = parseInt(await resp.text());
      return count;
    }
  • w

    wes

    09/23/2021, 5:27 PM
    then this
  • w

    wes

    09/23/2021, 5:28 PM
    I'm wondering if deploying my worker somehow resets it?
  • b

    brett

    09/23/2021, 5:49 PM
    It definitely shouldn't, unless part of your deploy is to delete and recreate the DO namespace
  • b

    brett

    09/23/2021, 5:55 PM
    @User What happens when your
    value
    is undefined because you don't send a proper number. It seems like you depend on JS converting a string to a number, but
    3 + undefined
    gives
    NaN
    which could be persisted to storage, and then on the next constructor call you might fall into the
    || 10
    , I think? Or at least it feels like something fuzzy around that. I would start by being more strict about what
    value
    is
  • w

    wes

    09/23/2021, 6:04 PM
    Is the constructor called on every DO fetch?
  • h

    HardAtWork

    09/23/2021, 6:09 PM
    No, the constructor is only called when a DO is initialized. After a certain amount of time without a fetch(or the isolate/colo goes down), a DO might be evicted, and will need to be initialized again, but anything is storage should persist between sessions.
  • w

    wes

    09/23/2021, 6:10 PM
    very good to know
  • w

    wes

    09/23/2021, 6:10 PM
    will help me narrow things down
  • w

    wes

    09/23/2021, 6:18 PM
    This discord is awesome btw. Can’t wait to get more experienced with this and help others 🙂
  • d

    Deleted User

    09/23/2021, 7:41 PM
    does anyone have any reading recommendations for migrating from DynamoDB to DO?
  • d

    Deleted User

    09/23/2021, 7:41 PM
    (is that even possible?)
  • h

    HardAtWork

    09/23/2021, 7:47 PM
    It isn't a 1-1 translation, but depending on your specific needs, it is probably doable. What does your data structure look like?
  • d

    Deleted User

    09/23/2021, 8:05 PM
    tbh, it's not that far off from a sql situation we've got customer entities, end user entities, and transactions -- each of these is its own table in DDB
  • h

    HardAtWork

    09/23/2021, 8:51 PM
    Currently, there isn't a good way to simulate a SQL database in DO, not to say that you can't write your own wrapper, but it is definitely possible.
  • d

    Deleted User

    09/23/2021, 8:51 PM
    we currently use GraphQL to build queries across DDB tables
  • r

    ronan(wighawag)

    09/24/2021, 9:15 AM
    Is there any way to just fetch the keys matching a prefix, limit or is the performance delta between fetching only the keys vs keys + values so small that it would not make much difference ?
1...184185186...567Latest