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

LIVE-6468 Reuse http client across invocations #1218

Merged
merged 5 commits into from
Apr 22, 2024

Conversation

waisingyiu
Copy link
Contributor

@waisingyiu waisingyiu commented Apr 18, 2024

What does this change?

The PR #1217 creates a HttpClient instance for the processing of each SQS client so that we don't send too many requests over the same connection.

However, the AWS lambda service reuses execution environment when it needs to invoke a lambda shortly after one has finished. So it is possible that within the same Java runtime, the AndroidSender instance is used to process SQS messages many times and, as a result, creates too many HttpClient instances and the connections that are left idle. The connections consume the limit on the file descriptors, which is 1024 for Java lambda runtime. It is likely to happen when we send a huge notification (more than 100,000).

What we really want is that we have dedicated HttpClient instances for each SQS message, but we want to reuse these instances in the subsequent invocation if the same runtime environment is reused.

This PR creates 20 HttpClient instances upfront (we process at most 20 SQS messages at a time) and uses them when processing the stream of SQS messages.

This PR also creates a new configuration to control the max connection pool size to avoid "too-many-opened-files" exception.

How to test

We invoked the Android sender locally with two batches of SQS messages (with 3 messages in each batch). We printed the instance in the log message here and we saw from the logs that the three FcmClient instances were reused across invocation:

First batch of SQS messages:
Individual send request completed - com.gu.notifications.worker.delivery.fcm.FcmClient@19d807f9

Individual send request completed - com.gu.notifications.worker.delivery.fcm.FcmClient@591354dd

Individual send request completed - com.gu.notifications.worker.delivery.fcm.FcmClient@13fa8ac

Second batch of SQS messages:

Individual send request completed - com.gu.notifications.worker.delivery.fcm.FcmClient@19d807f9

Individual send request completed - com.gu.notifications.worker.delivery.fcm.FcmClient@591354dd

Individual send request completed - com.gu.notifications.worker.delivery.fcm.FcmClient@13fa8ac

The service was also tested on CODE and notification was sent successfully to android app on emulator.

@waisingyiu waisingyiu force-pushed the LIVE-6468-reuse-http-client-across-invocations branch from effa81f to 60a9b06 Compare April 18, 2024 12:12
@waisingyiu waisingyiu force-pushed the LIVE-6468-reuse-http-client-across-invocations branch from e9342ba to 234cebd Compare April 18, 2024 17:14
@waisingyiu waisingyiu marked this pull request as ready for review April 18, 2024 17:15
@waisingyiu waisingyiu requested a review from a team as a code owner April 18, 2024 17:15
Copy link
Contributor

@lindseydew lindseydew left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍 Great work 🔥

@waisingyiu waisingyiu merged commit 7a01851 into main Apr 22, 2024
2 checks passed
@waisingyiu waisingyiu deleted the LIVE-6468-reuse-http-client-across-invocations branch April 22, 2024 10:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants