Skip to content

Commit 7db30e4

Browse files
committed
minor fixes
1 parent 00297ad commit 7db30e4

File tree

1 file changed

+26
-30
lines changed
  • src/content/docs/queues/tutorials/handle-rate-limits

1 file changed

+26
-30
lines changed

src/content/docs/queues/tutorials/handle-rate-limits/index.mdx

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,21 @@ description: Example of how to use Queues to handle rate limits of external APIs
2020

2121
import { Render, PackageManagers } from "~/components";
2222

23-
This tutorial explains how to use Queues to handle rate limits of external APIs. In this tutorial, you will build an application that sends email notifications using [Resend](https://www.resend.com/), but you can use this pattern to handle rate limits of any external API.
23+
This tutorial explains how to use Queues to handle rate limits of external APIs by building an application that sends email notifications using [Resend](https://www.resend.com/). However, you can use this pattern to handle rate limits of any external API.
2424

25-
Resend is a service that allows you to send emails from your application. It provides a simple API that you can use to send emails. Resend has a default [rate limit](https://resend.com/docs/api-reference/introduction#rate-limit) of 2 requests per second. You will use Queues to handle the rate limit of Resend.
25+
Resend is a service that allows you to send emails from your application via an API. Resend has a default [rate limit](https://resend.com/docs/api-reference/introduction#rate-limit) of two requests per second. You will use Queues to handle the rate limit of Resend.
2626

2727
## Prerequisites
2828

2929
<Render file="prereqs" product="workers" />
3030

31-
4. Sign up for Resend and generate an API key by following the instructions on the [Resend documentation](https://resend.com/docs/dashboard/api-keys/introduction).
31+
4. Sign up for [Resend](https://resend.com/) and generate an API key by following the guide on the [Resend documentation](https://resend.com/docs/dashboard/api-keys/introduction).
3232

33-
### Queues access
34-
35-
Additionally, you will need access to Cloudflare Queues.
33+
5. Additionally, you will need access to Cloudflare Queues.
3634

3735
<Render file="enable-queues" />
3836

39-
## 1. Create new Workers application
37+
## 1. Create a new Workers application
4038

4139
To get started, create a Worker application using the [`create-cloudflare` CLI](https://github.com/cloudflare/workers-sdk/tree/main/packages/create-cloudflare). Open a terminal window and run the following command:
4240

@@ -58,28 +56,28 @@ To get started, create a Worker application using the [`create-cloudflare` CLI](
5856
}}
5957
/>
6058

61-
Then, move into your newly created directory:
59+
Then, go to your newly created directory:
6260

6361
```sh frame="none"
6462
cd resend-rate-limit-queue
6563
```
6664

6765
## 2. Set up a Queue
6866

69-
You need to create a Queue and a binding to your Worker. Run the following command to create a Queue named `rate-limit-queue`.
67+
You need to create a Queue and a binding to your Worker. Run the following command to create a Queue named `rate-limit-queue`:
7068

7169
```sh title="Create a Queue"
7270
npx wrangler queues create rate-limit-queue
7371
```
7472

75-
```txt title="Output"
73+
```sh output
7674
Creating queue rate-limit-queue.
7775
Created queue rate-limit-queue.
7876
```
7977

80-
### Add Queue bindings to wrangler.toml
78+
### Add Queue bindings to `wrangler.toml`
8179

82-
Next, in your `wrangler.toml` file, add the following:
80+
In your `wrangler.toml` file, add the following:
8381

8482
```toml
8583
[[queues.producers]]
@@ -93,9 +91,9 @@ max_batch_timeout = 10
9391
max_retries = 3
9492
```
9593

96-
Adding the `max_batch_size` of 2 to the consumer queue is important because the Resend API has a default rate limit of 2 requests per second. This batch size allows the queue to process the message in the batch size of 2. If the batch size is less than 2, the queue will wait for 10 seconds to collect the next message. If no more messages are available, the queue will process the message in the batch. For more information, refer to the [Batching, Retries and Delays documentation](/queues/configuration/batching-retries)
94+
It is important to include the `max_batch_size` of two to the consumer queue is important because the Resend API has a default rate limit of two requests per second. This batch size allows the queue to process the message in the batch size of two. If the batch size is less than two, the queue will wait for 10 seconds to collect the next message. If no more messages are available, the queue will process the message in the batch. For more information, refer to the [Batching, Retries and Delays documentation](/queues/configuration/batching-retries)
9795

98-
Your final `wrangler.toml` file should look similar to the one below.
96+
Your final `wrangler.toml` file should look similar to the example below.
9997

10098
```toml title="wrangler.toml"
10199
#:schema node_modules/wrangler/config-schema.json
@@ -117,7 +115,7 @@ max_retries = 3
117115

118116
## 3. Add bindings to environment
119117

120-
Add the bindings to the environment interface in `worker-configuration.d.ts`, so TypeScript correctly types the bindings. Type the queue as `Queue<any>`. The following step will show you how to change this type.
118+
Add the bindings to the environment interface in `worker-configuration.d.ts`, so TypeScript correctly types the bindings. Type the queue as `Queue<any>`. Refer to the following step for instructions on how to change this type.
121119

122120
```ts title="worker-configuration.d.ts"
123121
interface Env {
@@ -127,7 +125,7 @@ interface Env {
127125

128126
## 4. Send message to the queue
129127

130-
The application will send a message to the queue when this Worker receives a request. For simplicity, you will send the email address as a message to the queue. A new message will be sent to the queue with a delay of 1 second.
128+
The application will send a message to the queue when the Worker receives a request. For simplicity, you will send the email address as a message to the queue. A new message will be sent to the queue with a delay of one second.
131129

132130
```ts title="src/index.ts"
133131
export default {
@@ -145,15 +143,15 @@ export default {
145143
};
146144
```
147145

148-
This will accept requests to any subpath and forwards the request's body. It expects that the request body only contains an email. In production, you should check that the request was a `POST` request. Moreover, you should avoid sending such sensitive information (email) directly to the queue. Instead, you can send a message to the queue that contains a unique identifier for the user. Then, your consumer queue can use the unique identifier to look up the email address in a database and use that to send the email.
146+
This will accept requests to any subpath and forwards the request's body. It expects that the request body to contain only an email. In production, you should check that the request was a `POST` request. You should also avoid sending such sensitive information (email) directly to the queue. Instead, you can send a message to the queue that contains a unique identifier for the user. Then, your consumer queue can use the unique identifier to look up the email address in a database and use that to send the email.
149147

150148
## 5. Process the messages in the queue
151149

152150
After the message is sent to the queue, it will be processed by the consumer Worker. The consumer Worker will process the message and send the email.
153151

154152
Since you have not configured Resend yet, you will log the message to the console. After you configure Resend, you will use it to send the email.
155153

156-
Add the `queue()` handler as shown below.
154+
Add the `queue()` handler as shown below:
157155

158156
```ts title="src/index.ts" ins={1-3,17-28}
159157
interface Message {
@@ -187,23 +185,21 @@ export default {
187185
};
188186
```
189187

190-
The above `queue()` handler will log the email address to the console and send the email. It will also retry the message if the email sending fails. The `delaySeconds` is set to 5 seconds to avoid sending the email too quickly.
188+
The above `queue()` handler will log the email address to the console and send the email. It will also retry the message if sending the email fails. The `delaySeconds` is set to five seconds to avoid sending the email too quickly.
191189

192-
To test the application, run the following command.
190+
To test the application, run the following command:
193191

194192
```sh title="Start the development server"
195193
npm run dev
196194
```
197195

198-
Use the following cURL command to send a request to the application.
196+
Use the following cURL command to send a request to the application:
199197

200198
```sh title="Test with a cURL request"
201199
curl -X POST -d "[email protected]" http://localhost:8787/
202200
```
203201

204-
On success, you should see the following output in the console.
205-
206-
```txt title="Output"
202+
```sh output
207203
[wrangler:inf] POST / 200 OK (2ms)
208204
QueueMessage {
209205
attempts: 1,
@@ -232,7 +228,7 @@ interface Env {
232228
}
233229
```
234230
235-
Lastly, install the [`resend` package](https://www.npmjs.com/package/resend) using the following command
231+
Lastly, install the [`resend` package](https://www.npmjs.com/package/resend) using the following command:
236232
237233
```sh title="Install Resend"
238234
npm install resend
@@ -295,9 +291,9 @@ export default {
295291
};
296292
```
297293
298-
The `queue()` handler will now send the email using the Resend API. It also checks if the email sending failed and retries the message if it did.
294+
The `queue()` handler will now send the email using the Resend API. It also checks if sending the email failed and will retry the message.
299295
300-
The final script is included below.
296+
The final script is included below:
301297
302298
```ts title="src/index.ts"
303299
import { Resend } from "resend";
@@ -348,13 +344,13 @@ export default {
348344
};
349345
```
350346
351-
To test the application, start the development server using the following command.
347+
To test the application, start the development server using the following command:
352348
353349
```sh title="Start the development server"
354350
npm run dev
355351
```
356352
357-
Use the following cURL command to send a request to the application.
353+
Use the following cURL command to send a request to the application:
358354
359355
```sh title="Test with a cURL request"
360356
curl -X POST -d "[email protected]" http://localhost:8787/
@@ -370,7 +366,7 @@ To deploy your Worker, run the following command:
370366
npx wrangler deploy
371367
```
372368
373-
Lastly, add the Resend API key using the following command.
369+
Lastly, add the Resend API key using the following command:
374370
375371
```sh title="Add the Resend API key"
376372
npx wrangler secret put RESEND_API_KEY

0 commit comments

Comments
 (0)