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

Direct Port RateLimiter from Rezilience #2083

Merged
merged 1 commit into from
Feb 9, 2024
Merged

Direct Port RateLimiter from Rezilience #2083

merged 1 commit into from
Feb 9, 2024

Conversation

mikearnaldi
Copy link
Member

@mikearnaldi mikearnaldi commented Feb 9, 2024

Add Ratelimiter which limits the number of calls to a resource within a time window using the token bucket algorithm.

Usage Example:

import { Effect, RateLimiter } from "effect";

// we need a scope because the rate limiter needs to allocate a state and a background job
const program = Effect.scoped(
  Effect.gen(function* ($) {
    // create a rate limiter that executes up to 10 requests within 2 seconds
    const rateLimit = yield* $(RateLimiter.make(10, "2 seconds"));
    // simulate repeated calls
    for (let n = 0; n < 100; n++) {
      // wrap the effect we want to limit with rateLimit
      yield* $(rateLimit(Effect.log("Calling RateLimited Effect")));
    }
  })
);

// will print 10 calls immediately and then throttle
program.pipe(Effect.runFork);

closes #2031 & #1685, Thanks @hsubra89

Copy link

changeset-bot bot commented Feb 9, 2024

🦋 Changeset detected

Latest commit: ba2f2c9

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

This PR includes changesets to release 15 packages
Name Type
effect Patch
@effect/cli Patch
@effect/experimental Patch
@effect/opentelemetry Patch
@effect/platform-browser Patch
@effect/platform-bun Patch
@effect/platform-node-shared Patch
@effect/platform-node Patch
@effect/platform Patch
@effect/printer-ansi Patch
@effect/printer Patch
@effect/rpc-http Patch
@effect/rpc Patch
@effect/schema Patch
@effect/typeclass Patch

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

@github-actions github-actions bot changed the base branch from main to next-minor February 9, 2024 06:23
@mikearnaldi mikearnaldi force-pushed the rate-limiter branch 2 times, most recently from d03b5e4 to e0ce828 Compare February 9, 2024 06:27
@mikearnaldi mikearnaldi changed the base branch from next-minor to main February 9, 2024 06:28
@mikearnaldi mikearnaldi merged commit be19ce0 into main Feb 9, 2024
12 checks passed
@mikearnaldi mikearnaldi deleted the rate-limiter branch February 9, 2024 06:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

None yet

2 participants