Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce context and request options for Durable Objects and modules #67

Merged
merged 2 commits into from
May 1, 2021

Conversation

robertcepa
Copy link
Owner

Introducing 2 new options: context and request.

  • context: Any object that contains waitUntil and (optionally) request. This option is the most universal way to instantiate toucan-js in any kind of Cloudflare Worker. It can be FetchEvent, ScheduledEvent, DurableObjectState, or context in modular Workers.
  • request: Because DurableObjectState and .mjs worker's context don't include request, you need to use this option to set it separately. You don't need to use this with FetchEvent or ScheduledEvent, as request is already included in these.

Durable Objects DOs and DON'Ts
It might be tempting to instantiate toucan-js in Durable Object's class constructor and set it as a class property. This is not recommended, as it will introduce race conditions and so you'd risk sending metadata of interleaved fetch events to Sentry. The strategy also wouldn't let you set request, which is not available in the constructor (and I intentionally didn't expose a setter :)).
toucan-js is lightweight and it's fine to instantiate it in fetch handler.

DON'T

import Toucan from 'toucan-js';

export class DurableObject {
  toucan: Toucan;
  state: DurableObjectState;

  constructor(state, env){
    // ...
    this.state = state;
    this.toucan= new Toucan({ context: state });
  }

  async fetch(request) {
    try {
      // RACE CONDITIONS
      // Because we have a single Toucan instance, interleaved requests will race to overwrite 'requestUrl' extra
      // fetch event A's requestUrl could be overwritten by fetch event B before it gets sent to Sentry
      this.toucan.setExtra({requestUrl: request.url});
      
      // ... do stuff
    } catch (e) {
      this.toucan.captureException(e);
    }
  }
}

DO

import Toucan from 'toucan-js';

export class DurableObject {
  state: DurableObjectState;

  constructor(state, env){
    // ...
    this.state = state;
  }

  async fetch(request) {
    const toucan= new Toucan({ context: this.state, request })
    try {
       // NO RACE CONDITIONS
       // Because we have an isolated toucan instance
      toucan.setExtra({requestUrl: request.url});
      
      // ... do stuff
    } catch (e) {
      toucan.captureException(e);
    }
  }
}

@robertcepa robertcepa merged commit 44478b2 into master May 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant