Skip to content

Commit b708554

Browse files
Merge pull request #232730 from yogeshmo/email/429-no-retry
Adding documentation for making 429s non-retriable
2 parents 16808db + d261b61 commit b708554

File tree

4 files changed

+128
-1
lines changed

4 files changed

+128
-1
lines changed

articles/communication-services/quickstarts/email/includes/send-email-java.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,8 @@ Make these replacements in the code:
222222
mvn exec:java -D"exec.mainClass"="com.communication.quickstart.App" -D"exec.cleanupDaemonThreads"="false"
223223
```
224224

225+
If you see that your application is hanging it could be due to email sending being throttled. You can [handle this through logging or by implementing a custom policy](#throw-an-exception-when-email-sending-tier-limit-is-reached).
226+
225227
### Sample code
226228

227229
You can download the sample app from [GitHub](https://github.com/Azure-Samples/communication-services-java-quickstarts/tree/main/send-email)
@@ -300,3 +302,39 @@ System.out.println("Operation Id: " + response.getValue().getId());
300302
For more information on acceptable MIME types for email attachments, see the [allowed MIME types](../../../concepts/email/email-attachment-allowed-mime-types.md) documentation.
301303

302304
You can download the sample app demonstrating this action from [GitHub](https://github.com/Azure-Samples/communication-services-java-quickstarts/tree/main/send-email-advanced)
305+
306+
### Throw an exception when email sending tier limit is reached
307+
308+
The Email API has throttling with limitations on the number of email messages that you can send. Email sending has limits applied per minute and per hour as mentioned in [API Throttling and Timeouts](https://learn.microsoft.com/azure/communication-services/concepts/service-limits). When you have reached these limits, additional email sends with `beginSend` calls will receive an error response of “429: Too Many Requests”. By default, the SDK is configured to retry these requests after waiting a certain period of time. We recommend you [set up logging with the Azure SDK](https://learn.microsoft.com/azure/developer/java/sdk/logging-overview) to capture these response codes.
309+
310+
Alternatively, you can manually define a custom policy as shown below.
311+
312+
```java
313+
import com.azure.core.http.HttpResponse;
314+
import com.azure.core.http.policy.ExponentialBackoff;
315+
316+
public class CustomStrategy extends ExponentialBackoff {
317+
@Override
318+
public boolean shouldRetry(HttpResponse httpResponse) {
319+
int code = httpResponse.getStatusCode();
320+
321+
if (code == HTTP_STATUS_TOO_MANY_REQUESTS) {
322+
throw new RuntimeException(httpResponse);
323+
}
324+
else {
325+
return super.shouldRetry(httpResponse);
326+
}
327+
}
328+
}
329+
```
330+
331+
Add this retry policy to your email client. This will ensure that 429 response codes throw an exception rather than being retried.
332+
333+
```java
334+
import com.azure.core.http.policy.RetryPolicy;
335+
336+
EmailClient emailClient = new EmailClientBuilder()
337+
.connectionString(connectionString)
338+
.retryPolicy(new RetryPolicy(new CustomStrategy()))
339+
.buildClient();
340+
```

articles/communication-services/quickstarts/email/includes/send-email-js.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,9 @@ use the node command to run the code you added to the send-email.js file.
216216
```console
217217
node ./send-email.js
218218
```
219+
220+
If you see that your application is hanging it could be due to email sending being throttled. You can [handle this through logging or by implementing a custom policy](#throw-an-exception-when-email-sending-tier-limit-is-reached).
221+
219222
### Sample code
220223

221224
You can download the sample app from [GitHub](https://github.com/Azure-Samples/communication-services-javascript-quickstarts/tree/main/send-email)
@@ -308,3 +311,39 @@ const response = await emailClient.send(message);
308311
For more information on acceptable MIME types for email attachments, see the [allowed MIME types](../../../concepts/email/email-attachment-allowed-mime-types.md) documentation.
309312

310313
You can download the sample app demonstrating this action from [GitHub](https://github.com/Azure-Samples/communication-services-javascript-quickstarts/tree/main/send-email-advanced/send-email-attachments)
314+
315+
### Throw an exception when email sending tier limit is reached
316+
317+
The Email API has throttling with limitations on the number of email messages that you can send. Email sending has limits applied per minute and per hour as mentioned in [API Throttling and Timeouts](https://learn.microsoft.com/azure/communication-services/concepts/service-limits). When you have reached these limits, additional email sends with `send` calls will receive an error response of “429: Too Many Requests”. By default, the SDK is configured to retry these requests after waiting a certain period of time. We recommend you [set up logging with the Azure SDK](https://learn.microsoft.com/javascript/api/overview/azure/logger-readme) to capture these response codes.
318+
319+
There are per minute and per hour [limits to the amount of emails you can send using the Azure Communication Email Service](https://learn.microsoft.com/azure/communication-services/concepts/service-limits). When you have reached these limits, any further `beginSend` calls will recieve a `429: Too Many Requests` response. By default, the SDK is configured to retry these requests after waiting a certain period of time. We recommend you [set up logging with the Azure SDK](https://learn.microsoft.com/javascript/api/overview/azure/logger-readme) to capture these response codes.
320+
321+
Alternatively, you can manually define a custom policy as shown below.
322+
323+
```javascript
324+
const catch429Policy = {
325+
name: "catch429Policy",
326+
async sendRequest(request, next) {
327+
const response = await next(request);
328+
if (response.status === 429) {
329+
throw new Error(response);
330+
}
331+
return response;
332+
}
333+
};
334+
```
335+
336+
Add this policy to your email client. This will ensure that 429 response codes throw an exception rather than being retried.
337+
338+
```java
339+
const clientOptions = {
340+
additionalPolicies: [
341+
{
342+
policy: catch429Policy,
343+
position: "perRetry"
344+
}
345+
]
346+
}
347+
348+
const emailClient = new EmailClient(connectionString, clientOptions);
349+
```

articles/communication-services/quickstarts/email/includes/send-email-net.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,8 @@ Run the application from your application directory with the `dotnet run` comman
227227
dotnet run
228228
```
229229

230+
If you see that your application is hanging it could be due to email sending being throttled. You can [handle this through logging or by implementing a custom policy](#throw-an-exception-when-email-sending-tier-limit-is-reached).
231+
230232
### Sample code
231233

232234
You can download the sample app from [GitHub](https://github.com/Azure-Samples/communication-services-dotnet-quickstarts/tree/main/SendEmail)
@@ -393,4 +395,37 @@ For more information on acceptable MIME types for email attachments, see the [al
393395

394396
You can download the sample app demonstrating this action from [GitHub](https://github.com/Azure-Samples/communication-services-dotnet-quickstarts/tree/main/SendEmailAdvanced/SendEmailWithAttachments)
395397

398+
### Throw an exception when email sending tier limit is reached
399+
400+
The Email API has throttling with limitations on the number of email messages that you can send. Email sending has limits applied per minute and per hour as mentioned in [API Throttling and Timeouts](https://learn.microsoft.com/azure/communication-services/concepts/service-limits). When you have reached these limits, additional email sends with `SendAsync` calls will receive an error response of “429: Too Many Requests”. By default, the SDK is configured to retry these requests after waiting a certain period of time. We recommend you [set up logging with the Azure SDK](https://learn.microsoft.com/dotnet/azure/sdk/logging) to capture these response codes.
401+
402+
Alternatively, you can manually define a custom policy as shown below.
403+
404+
```csharp
405+
using Azure.Core.Pipeline;
406+
407+
public class Catch429Policy : HttpPipelineSynchronousPolicy
408+
{
409+
public override void OnReceivedResponse(HttpMessage message)
410+
{
411+
if (message.Response.Status == 429)
412+
{
413+
throw new Exception(message.Response);
414+
}
415+
else
416+
{
417+
base.OnReceivedResponse(message);
418+
}
419+
}
420+
}
421+
```
422+
423+
Add this policy to your email client. This will ensure that 429 response codes throw an exception rather than being retried.
424+
425+
```csharp
426+
EmailClientOptions emailClientOptions = new EmailClientOptions();
427+
emailClientOptions.AddPolicy(new Catch429Policy(), HttpPipelinePosition.PerRetry);
428+
429+
EmailClient emailClient = new EmailClient(connectionString, emailClientOptions);
430+
```
396431

articles/communication-services/quickstarts/email/includes/send-email-python.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,6 @@ Make these replacements in the code:
231231
- Replace `<[email protected]>` with the email address you would like to send a message to.
232232
- Replace `<[email protected]>` with the MailFrom address of your verified domain.
233233

234-
235234
### Get the status of the email delivery
236235

237236
We can poll for the status of the email delivery by setting a loop on the operation status object returned from the EmailClient's `begin_send` method:
@@ -271,6 +270,8 @@ Run the application from your application directory with the `python` command.
271270
python send-email.py
272271
```
273272

273+
If you see that your application is hanging it could be due to email sending being throttled. You can [handle this through logging or by implementing a custom policy](#throw-an-exception-when-email-sending-tier-limit-is-reached).
274+
274275
### Sample code
275276

276277
You can download the sample app from [GitHub](https://github.com/Azure-Samples/communication-services-python-quickstarts/tree/main/send-email)
@@ -347,3 +348,17 @@ message = {
347348
For more information on acceptable MIME types for email attachments, see the [allowed MIME types](../../../concepts/email/email-attachment-allowed-mime-types.md) documentation.
348349

349350
You can download the sample app demonstrating this action from [GitHub](https://github.com/Azure-Samples/communication-services-python-quickstarts/tree/main/send-email-advanced)
351+
352+
### Throw an exception when email sending tier limit is reached
353+
354+
The Email API has throttling with limitations on the number of email messages that you can send. Email sending has limits applied per minute and per hour as mentioned in [API Throttling and Timeouts](https://learn.microsoft.com/azure/communication-services/concepts/service-limits). When you have reached these limits, additional email sends with `SendAsync` calls will receive an error response of “429: Too Many Requests”. By default, the SDK is configured to retry these requests after waiting a certain period of time. We recommend you [set up logging with the Azure SDK](https://learn.microsoft.com/azure/developer/python/sdk/azure-sdk-logging) to capture these response codes.
355+
356+
Alternatively, you can manually define a custom policy as shown below. This will ensure that 429 response codes throw an exception rather than being retried.
357+
358+
```python
359+
def callback(response):
360+
if response.http_response.status_code == 429:
361+
raise Exception(response.http_response)
362+
363+
email_client = EmailClient.from_connection_string(<connection_string>, raw_response_hook=callback)
364+
```

0 commit comments

Comments
 (0)