Effect CommunityEC
Effect Community9mo ago
8 replies
wewell

Handling Interruptions for Mutating Endpoints in Effect Typescript Library

Hi there, I have noticed that all request handlers are interruptible by default, and return a status 499 when the request is aborted by the client.

This can be an issue for requests that perform side-effects, like 1) saves something to a database, then 2) emits an event. We don't want the request to interrupt in the middle of both operations.

While trying to solve this issue, I finally wrote this very simple middleware that uses the HTTP method to determine if the request can be interrupted:

import { HttpMiddleware, HttpServerRequest } from '@effect/platform';
import { Effect } from 'effect';

export const UninterruptibleMutationsMiddleware = HttpMiddleware.make((app) =>
  Effect.gen(function* () {
    const { method } = yield* HttpServerRequest.HttpServerRequest;

    if (method === 'POST' || method === 'PUT' || method === 'PATCH' || method === 'DELETE') {
      return yield* Effect.uninterruptible(app);
    }

    return yield* app;
  })
);


But now I'm thinking: should it not be the default behaviour of the platform library ?
I mean it makes a lot of sense to prevent interruptions on mutating endpoints, right ?
Was this page helpful?