effect icon indicating copy to clipboard operation
effect copied to clipboard

Add a timeout option to HttpClientRequest

Open thewilkybarkid opened this issue 8 months ago • 4 comments

Type

  • [ ] Refactor
  • [x] Feature
  • [ ] Bug Fix
  • [ ] Optimization
  • [ ] Documentation Update

Description

Adds a timeout option to HttpClientRequest, which results in a Transport RequestError if it's exceeded. I've erred on adding to the request rather than having it alongside to cater for cases where the client has been composed.

I'm sure that some of the implementation could be improved. 😄

Related

  • Thread https://discord.com/channels/795981131316985866/1359836164005302392
  • Related Issue #
  • Closes #

thewilkybarkid avatar Apr 10 '25 20:04 thewilkybarkid

🦋 Changeset detected

Latest commit: 06917f30d74b84fb23579ac21d406576aa281487

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 26 packages
Name Type
@effect/platform Minor
@effect/cli Major
@effect/cluster Major
@effect/experimental Major
@effect/platform-browser Major
@effect/platform-bun Major
@effect/platform-node-shared Major
@effect/platform-node Major
@effect/rpc Major
@effect/sql-clickhouse Major
@effect/sql-d1 Major
@effect/sql-libsql Major
@effect/sql-mssql Major
@effect/sql-mysql2 Major
@effect/sql-pg Major
@effect/sql-sqlite-bun Major
@effect/sql-sqlite-node Major
@effect/sql Major
@effect/ai Major
@effect/ai-anthropic Major
@effect/ai-openai Major
@effect/sql-sqlite-do Major
@effect/sql-sqlite-react-native Major
@effect/sql-sqlite-wasm Major
@effect/sql-drizzle Major
@effect/sql-kysely Major

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

changeset-bot[bot] avatar Apr 10 '25 20:04 changeset-bot[bot]

why not using plain Effect.timeout?

mikearnaldi avatar Apr 12 '25 16:04 mikearnaldi

I'm looking to turn these into RequestErrors, which you can't (easily) with Effect.timeout*.

As mentioned in https://discord.com/channels/795981131316985866/1359836164005302392/1360164805025206322, composing the HttpClient with one that times out instead with an API like:

HttpClient.get('http://example.com/'),
HttpClient.withTimeout('1 second'),
Effect.andThen(...)

would work too.

thewilkybarkid avatar Apr 14 '25 08:04 thewilkybarkid

You can add timeouts with customs errors like this:

Effect.gen(function*() {
  const client = (yield* HttpClient.HttpClient).pipe(
    HttpClient.transform((effect, request) =>
      Effect.timeoutFail(effect, {
        duration: 1000,
        onTimeout: () => new Error(`${request.method} ${request.url} timed out`)
      })
    )
  )
})

tim-smart avatar Apr 22 '25 21:04 tim-smart