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

Explicitly configure the aioapns event loop so proxying works when configured #360

Merged
merged 2 commits into from
Feb 15, 2024

Conversation

devonh
Copy link
Contributor

@devonh devonh commented Feb 8, 2024

When the aioapns dependency was updated, they removed the ability to inject an event loop.
This change sets the event loop manually after creating the apns client.

We use a custom event loop in this case in order to override create_connection when a proxy is configured to force all traffic over the proxy.

Fixes https://github.com/element-hq/backend-internal/issues/22

@devonh devonh requested a review from a team as a code owner February 8, 2024 01:53
@devonh devonh changed the title Manually configurepecify the aioapns event loop so proxying works when configured Explicitly configure the aioapns event loop so proxying works when configured Feb 8, 2024
@@ -213,6 +213,9 @@ async def make_apns() -> aioapns.APNs:
# without this, aioapns will retry every second forever.
self.apns_client.pool.max_connection_attempts = 3

# without this, aioapns will not use the proxy if one is configured.
self.apns_client.pool.loop = loop
Copy link
Member

Choose a reason for hiding this comment

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

I'm slightly hesitant to gut wrench into the depths of classes TBH, though appreciate this might be the best option.

Given this proxy seems to be a global value, I wonder if we could create the new loop at startup and do asyncio.set_event_loop(loop)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tried doing that, in many many different ways but couldn't get aioapns to actually behave and use that loop.
So I gave up and resorted to this method (which I also don't like)

Maybe there is a way to do it with setting the event loop, but I wasn't able to make it work.

Copy link
Member

Choose a reason for hiding this comment

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

Huh, wtf. Having looked at the code it seems that it just does asyncio.get_event_loop() in the constructor, but maybe something is somehow constructing a class at import time or something? Weird.

@devonh devonh merged commit 9c40c7a into main Feb 15, 2024
5 checks passed
@devonh devonh deleted the devon/proxy-fix branch February 15, 2024 16:03
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