Skip to content

Commit

Permalink
chore: refining README for rate-limter as per comments
Browse files Browse the repository at this point in the history
  • Loading branch information
pratik151192 committed Nov 2, 2023
1 parent c5048da commit 312f271
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 20 deletions.
15 changes: 11 additions & 4 deletions examples/nodejs/rate-limiter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Incorporating the `MomentoRateLimiter` class into your application is a straight

To get started with the rate-limiter:
- You will need a Momento API key. You can obtain one from the [Momento Console](https://console.gomomento.com).
- You will need to create a cache called `rate-limiter` from the console as well!
- You will need to create a cache called `rate-limiter` from the console as well! You can choose a different cache name and pass it to the rate-limiter constructor if you'd like.

Once you have the key and the cache created, you can begin integration! Remember to store your API key in an environment variable named `MOMENTO_API_KEY`.

Expand All @@ -39,7 +39,8 @@ const momento = await CacheClient.create({
});

const tpmLimit = 10;
const rateLimiter = new MomentoRateLimiter(momento, tpmLimit);
const cacheName = "rate-limier";
const rateLimiter = new MomentoRateLimiter(momento, tpmLimit, cacheName);

// test rate limiter
const limitExceeded : boolean = await rateLimiter.isLimitExceeded(`id`);
Expand Down Expand Up @@ -88,7 +89,7 @@ All tasks complete!

```

There are three additional arguments that you can provide to the rate-limiter if you want to experiment with different configurations:
There are three additional arguments that you can provide to the simulator if you want to experiment with the rate-limiter:

- totalRequests: The total number of requests that the example will simulate, defaulted to 1000.
- randomDelayUpperBound: The simulation adds a random delay between 0 and the randomDelayUpperBound, defaulted to 60 seconds.
Expand All @@ -97,7 +98,13 @@ There are three additional arguments that you can provide to the rate-limiter if
To override totalRequests to 10, randomDelayUpperBound to 60, and tpmLimit to 1, the command will look like:

```bash
MOMENTO_API_KEY="yourApiKey" npm run rate-limiter 10 60 1
MOMENTO_API_KEY="yourApiKey" npm run rate-limiter -- 10 60 1
```

The displayed output above indicates a 100% success rate. To observe throttles, modify the configuration as below, which results in approximately half of the requests being throttled. The rate limit is set to 10 requests per user, and we distribute 20 requests among 5 users (totaling 100 requests), introducing a random delay ranging from 0 to 500 milliseconds between each request.

```bash
MOMENTO_API_KEY="yourApiKey" npm run rate-limiter -- 100 500 10
```

## Analysis
Expand Down
14 changes: 6 additions & 8 deletions examples/nodejs/rate-limiter/momento-rate-limiter.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
import { CacheClient, CacheIncrement, CacheUpdateTtl } from "@gomomento/sdk";

Check warning on line 1 in examples/nodejs/rate-limiter/momento-rate-limiter.ts

View workflow job for this annotation

GitHub Actions / Test examples on node 14

Strings must use singlequote

Check warning on line 1 in examples/nodejs/rate-limiter/momento-rate-limiter.ts

View workflow job for this annotation

GitHub Actions / Test examples on node 16

Strings must use singlequote

Check warning on line 1 in examples/nodejs/rate-limiter/momento-rate-limiter.ts

View workflow job for this annotation

GitHub Actions / Test examples on node 18

Strings must use singlequote
import {
AbstractRateLimiter,
RATE_LIMITER_CACHE_NAME,
RATE_LIMITER_TTL_MILLIS,
} from "./rate-limiter";
import { AbstractRateLimiter, RATE_LIMITER_TTL_MILLIS } from "./rate-limiter";

Check warning on line 2 in examples/nodejs/rate-limiter/momento-rate-limiter.ts

View workflow job for this annotation

GitHub Actions / Test examples on node 14

Strings must use singlequote

Check warning on line 2 in examples/nodejs/rate-limiter/momento-rate-limiter.ts

View workflow job for this annotation

GitHub Actions / Test examples on node 16

Strings must use singlequote

Check warning on line 2 in examples/nodejs/rate-limiter/momento-rate-limiter.ts

View workflow job for this annotation

GitHub Actions / Test examples on node 18

Strings must use singlequote

export class MomentoRateLimiter extends AbstractRateLimiter {
_client: CacheClient;
_limit: number;
_cacheName: string;

constructor(client: CacheClient, limit: number) {
constructor(client: CacheClient, limit: number, cacheName: string) {
super();
this._client = client;
this._limit = limit;
this._cacheName = cacheName;
}

public async isLimitExceeded(id: string): Promise<boolean> {
const currentMinuteKey = this.generateMinuteKey(id);
// we do not pass a TTL to this; we don't know if the key for this user was present or not
const resp = await this._client.increment(
RATE_LIMITER_CACHE_NAME,
this._cacheName,
currentMinuteKey
);

Expand All @@ -29,7 +27,7 @@ export class MomentoRateLimiter extends AbstractRateLimiter {
// we set the TTL for this minute's key to 60 seconds now.
if (resp.value() === 1) {
const updateTTLResp = await this._client.updateTtl(
RATE_LIMITER_CACHE_NAME,
this._cacheName,
currentMinuteKey,
RATE_LIMITER_TTL_MILLIS
);
Expand Down
1 change: 0 additions & 1 deletion examples/nodejs/rate-limiter/rate-limiter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ export interface RateLimiter {

// since our rate limiting buckets are per minute, we expire keys every minute
export const RATE_LIMITER_TTL_MILLIS = 60000;
export const RATE_LIMITER_CACHE_NAME = "rate-limiter";

export abstract class AbstractRateLimiter implements RateLimiter {
abstract isLimitExceeded(id: string): Promise<boolean>;
Expand Down
16 changes: 9 additions & 7 deletions examples/nodejs/rate-limiter/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
} from "@gomomento/sdk";

Check warning on line 6 in examples/nodejs/rate-limiter/worker.ts

View workflow job for this annotation

GitHub Actions / Test examples on node 14

Strings must use singlequote

Check warning on line 6 in examples/nodejs/rate-limiter/worker.ts

View workflow job for this annotation

GitHub Actions / Test examples on node 16

Strings must use singlequote

Check warning on line 6 in examples/nodejs/rate-limiter/worker.ts

View workflow job for this annotation

GitHub Actions / Test examples on node 18

Strings must use singlequote
import { DummyService } from "./service";

Check warning on line 7 in examples/nodejs/rate-limiter/worker.ts

View workflow job for this annotation

GitHub Actions / Test examples on node 14

Strings must use singlequote

Check warning on line 7 in examples/nodejs/rate-limiter/worker.ts

View workflow job for this annotation

GitHub Actions / Test examples on node 16

Strings must use singlequote

Check warning on line 7 in examples/nodejs/rate-limiter/worker.ts

View workflow job for this annotation

GitHub Actions / Test examples on node 18

Strings must use singlequote
import { MomentoRateLimiter } from "./momento-rate-limiter";

Check warning on line 8 in examples/nodejs/rate-limiter/worker.ts

View workflow job for this annotation

GitHub Actions / Test examples on node 14

Strings must use singlequote

Check warning on line 8 in examples/nodejs/rate-limiter/worker.ts

View workflow job for this annotation

GitHub Actions / Test examples on node 16

Strings must use singlequote

Check warning on line 8 in examples/nodejs/rate-limiter/worker.ts

View workflow job for this annotation

GitHub Actions / Test examples on node 18

Strings must use singlequote
import { RATE_LIMITER_CACHE_NAME, RateLimiter } from "./rate-limiter";
import { RateLimiter } from "./rate-limiter";

Check warning on line 9 in examples/nodejs/rate-limiter/worker.ts

View workflow job for this annotation

GitHub Actions / Test examples on node 14

Strings must use singlequote

Check warning on line 9 in examples/nodejs/rate-limiter/worker.ts

View workflow job for this annotation

GitHub Actions / Test examples on node 16

Strings must use singlequote

Check warning on line 9 in examples/nodejs/rate-limiter/worker.ts

View workflow job for this annotation

GitHub Actions / Test examples on node 18

Strings must use singlequote
import { Metrics } from "./metrics";

async function main() {
Expand All @@ -18,11 +18,6 @@ async function main() {
defaultTtlSeconds: 6000,
});

const resp = await momento.createCache(RATE_LIMITER_CACHE_NAME);
if (resp instanceof CreateCache.Error) {
throw new Error(`Failed to create cache ${RATE_LIMITER_CACHE_NAME}`);
}

// default values
let totalRequests = 1000;
let randomDelayUpperBound = 60000;
Expand All @@ -42,7 +37,14 @@ async function main() {

const service = new DummyService();
const rateLimiterMetrics = new Metrics();
const rateLimiter = new MomentoRateLimiter(momento, tpmLimit);
const cacheName = `rate-limiter`;

const resp = await momento.createCache(cacheName);
if (resp instanceof CreateCache.Error) {
throw new Error(`Failed to create cache ${cacheName}`);
}

const rateLimiter = new MomentoRateLimiter(momento, tpmLimit, cacheName);

const userIDs = ["user1", "user2", "user3", "user4", "user5"];
const tasks = [];
Expand Down

0 comments on commit 312f271

Please sign in to comment.