Add connection pooling
With TLS-based client authentication, clients could only send notifications to the topics mentioned in the certificate used for authentication. With the arrival of token-based authentication, a single client can send notifications to any number of topics.
One consequence is that many users may now want to centralize on a single client instance for simplicity. For performance reasons, we should consider allowing clients to operate a pool of connections to make better use of CPU resources (right now, a client will use exactly one thread regardless of the number of cores available) and possibly upstream resources (Apple claims that it's beneficial to spread connections across multiple servers, but I'm skeptical).
I took a first shot at connection pooling in the connection_pooling_spike branch and have some observations to share around (a) design implications and (b) performance.
In terms of design changes, it seems like one thing we may want to give up on is the idea of explicitly "starting" an ApnsClient, and instead letting it lazily-create channels. In the past, this would have been a really bad idea because there were a lot of ways for connections attempts to fail without much hope of self-resolution:
- Callers might not have Apple's intermediate certificates in their trust store
- This is less of an issue now that Apple is using a more widely-recognized CA, and more to the point, one that's in most JRE trust stores by default
- Callers might have an expired client certificate
- Auth tokens don't expire, but can be revoked; either way, this would be a problem when actually sending notifications, but not when establishing a connection
- Callers might have a screwy/malformed keystore
- Now that callers don't have to deal with CSRs, exporting certificates, and weird Keychain Access bugs, external factors are much less likely to interfere with the connection process
With token-based authentication, the things that can go wrong with opening a connection are mostly network-level things that are much more likely to self-resolve (or be beyond the control of the caller), and so it's less important that we provide a clear view into the status of the initial connection attempt.
On performance, some preliminary benchmarking shows per-channel throughput to be slower than the single-channel approach by about a factor of two. There are three things that I think are driving the slowdown:
- Client channels need to deal with thread contention when working with the client's
responseFuturesmap. - Client channels (and mock server channels) need to deal with thread contention when getting
AuthenticationTokenSuppliers. - Actually acquiring and releasing channels now involves some kind of synchronization and is generally a new cost that we simply didn't have to pay before.
To address the first point, I'm pretty sure we can make the responseFutures map a property of the channel (or handler? or pipeline?), so each channel has uncontested access to its own map. This is probably a better design anyhow.
I'm tempted to take a similar approach for the authentication token suppliers, but am concerned about the memory implications for industrial-scale users. If an AuthenticationTokenSupplier takes up ~1kB, and we want a copy of that supplier in every channel, we're looking at 1kB per channel per public key. For users with, say, a thousand public keys and 32 channels, we'd be looking at ~32MB just to store all the token suppliers. That seems like a lot, but it might be an acceptable price to pay for industrial-scale users in order to maximize throughput. I'm very open to feedback on this point!
As for the third point—actually getting channels from a pool—it's hard to tell how much of the cost is avoidable. The acquire/release pattern imposed by Netty's ChannelPool interface may be too heavy for what we need (I suspect the time spent acquiring/releasing channels is very significant in comparison to the time taken to call that channel's sendNotification method), and we might be able to come up with something a little more tailored to our purpose if this proves to be a major driver of performance woes.
So, long story short: we've learned some things and have some questions to resolve. I'm very interested in feedback from you all as we move forward; please let me know what you all think!
A cross-post from the mailing list:
I've been seeing some alarming posts on Apple's push notification forums:
- https://forums.developer.apple.com/thread/71523
- https://forums.developer.apple.com/thread/72999
Those threads suggest that, even though we send an authentication token with each push notification, Apple is "binding" connections to the set of topics associated with whichever token it receives first, and subsequent attempts to send notifications with valid key associated with a different set of topics (a different team ID?) will cause ALL notifications on that connection to be rejected with HTTP/403 (InvalidProviderToken). This is a Big Deal™ for Pushy, and flies counter to a number of design decisions related to auth tokens.
Regrettably, we at Turo only have our one team (and thus only one signing key). I'm trying to get clarification from Apple, but in the meantime, have any of you succeeded in using a single ApnsClient with keys from multiple teams in the wild?
If it turns out that this is actually what Apple intended, I think we have to bail out on the connection pooling effort.
I'm getting a slow trickle of information from upstream on this topic, but don't have resolution yet.
From #437, it sounds like at least one user is seeing failures when trying to use tokens from multiple teams on the same connection.
For reference, I've opened rdar://31345903 for this issue.
Okay; I've learned that "one team per connection" is still the intended behavior, even with token authentication. Regrettably, I think that means we have to shelve connection pooling (as imagined here, at least) for now.
answering to your comment in #477. I know about connection reuse. And tested pushes with one connection. If I send 10, 50, 100, ±500 pushes per second, I see that full request time (time measured from client.sendNotification(notification) to GenericFutureListener::operationComplete) is in acceptable bounds - in a range from 150 to several hundred milliseconds. But then when I increase throughput - let say to around 1000 messages per second, I see that this time starts to grow: near second, second, several seconds, several tens of seconds and so on. It looks like there is some throughput limit per connection at Apple side. After couple of minutes of such request time increase stopped testing. Currently implemented simple connection pool around ApnsClient with let's say array of 10 ApnsClients and taking one of them by deviceToken.hashCode % 10.
Thanks, Z.
By the way Apple sends MAX_CONCURRENT_STREAMS=1000 and if admit duration of one push 200ms, then it comes out that one connection can handle 5000 rps, but seems that in reality it is much less :( Thanks, Z.
@janzar hi, why couldnt you just set concurrentConnections property to 10?
Hi. We use concurrentConnections. As I remember, this discussion was about pushy version, that didn't implemented connection pooling in itself yet.