Comments (15)
Ah, found the bug: https://github.com/vapor/apns/blob/master/Sources/APNS/APNSConnectionSource.swift#L24
That needs a !
from apns.
It should definitely be reusing the connection, where possible!
from apns.
cc @tanner0101 So it's not pooling I presume?
from apns.
@tanner0101 Remember common usage would be like so
tokens.forEach { token in
app.apns.send(token, ...
}
from apns.
FWIW Apple recommends keeping your connection open. if you open a new connection for each request, that's going to be a LOT of opens and closes.
Its probably safer to have an overload for send to accept a connection to send on, and you decide which one you want to use.
from apns.
@handya add this to your project and run:
print(System.coreCount)
You'll need to make System.coreCount + 1 requests before Vapor will start re-using connections. Connections are stored local to each event loop for better performance.
If you're still noticing connections not being reused after System.coreCount + 1 requests, then we may have a bug.
from apns.
@tanner0101 Still having the issue, unless I've done something wrong, still appears to create a new connection each time.
[ INFO ] GET /send-push
Request count: 1, core count: 16
[ DEBUG ] Connection - starting ["origin": APNSwift] (APNSwift/APNSwiftConnection.swift:90)
[ DEBUG ] Connection - token factory setup ["origin": APNSwift] (APNSwift/APNSwiftConnection.swift:106)
[ DEBUG ] Creating a new APNS token ["origin": APNSwift] (APNSwift/APNSwiftBearerTokenFactory.swift:28)
[ DEBUG ] Connection - token factory created ["origin": APNSwift] (APNSwift/APNSwiftConnection.swift:110)
[ DEBUG ] Connection - bringing up ["origin": APNSwift] (APNSwift/APNSwiftConnection.swift:119)
[ INFO ] Connection - up ["origin": APNSwift] (APNSwift/APNSwiftConnection.swift:143)
[ DEBUG ] Send - starting up ["origin": APNSwift] (APNSwift/APNSwiftConnection.swift:185)
[ INFO ] Send - sending ["origin": APNSwift] (APNSwift/APNSwiftConnection.swift:204)
[ DEBUG ] Request - building ["origin": APNSwift] (APNSwift/APNSwiftRequestEncoder.swift:55)
[ DEBUG ] Response - received ["origin": APNSwift] (APNSwift/APNSwiftStreamHandler.swift:35)
[ INFO ] Response - successful ["origin": APNSwift] (APNSwift/APNSwiftStreamHandler.swift:52)
[ INFO ] GET /send-push
Request count: 17, core count: 16
[ DEBUG ] Connection - starting ["origin": APNSwift] (APNSwift/APNSwiftConnection.swift:90)
[ DEBUG ] Connection - token factory setup ["origin": APNSwift] (APNSwift/APNSwiftConnection.swift:106)
[ DEBUG ] Creating a new APNS token ["origin": APNSwift] (APNSwift/APNSwiftBearerTokenFactory.swift:28)
[ DEBUG ] Connection - token factory created ["origin": APNSwift] (APNSwift/APNSwiftConnection.swift:110)
[ DEBUG ] Connection - bringing up ["origin": APNSwift] (APNSwift/APNSwiftConnection.swift:119)
[ INFO ] Connection - up ["origin": APNSwift] (APNSwift/APNSwiftConnection.swift:143)
[ DEBUG ] Send - starting up ["origin": APNSwift] (APNSwift/APNSwiftConnection.swift:185)
[ INFO ] Send - sending ["origin": APNSwift] (APNSwift/APNSwiftConnection.swift:204)
[ DEBUG ] Request - building ["origin": APNSwift] (APNSwift/APNSwiftRequestEncoder.swift:55)
[ DEBUG ] Response - received ["origin": APNSwift] (APNSwift/APNSwiftStreamHandler.swift:35)
[ INFO ] Response - successful ["origin": APNSwift] (APNSwift/APNSwiftStreamHandler.swift:52)
[ INFO ] GET /send-push
Request count: 18, core count: 16
[ DEBUG ] Connection - starting ["origin": APNSwift] (APNSwift/APNSwiftConnection.swift:90)
[ DEBUG ] Connection - token factory setup ["origin": APNSwift] (APNSwift/APNSwiftConnection.swift:106)
[ DEBUG ] Creating a new APNS token ["origin": APNSwift] (APNSwift/APNSwiftBearerTokenFactory.swift:28)
[ DEBUG ] Connection - token factory created ["origin": APNSwift] (APNSwift/APNSwiftConnection.swift:110)
[ DEBUG ] Connection - bringing up ["origin": APNSwift] (APNSwift/APNSwiftConnection.swift:119)
[ INFO ] Connection - up ["origin": APNSwift] (APNSwift/APNSwiftConnection.swift:143)
[ DEBUG ] Send - starting up ["origin": APNSwift] (APNSwift/APNSwiftConnection.swift:185)
[ INFO ] Send - sending ["origin": APNSwift] (APNSwift/APNSwiftConnection.swift:204)
[ DEBUG ] Request - building ["origin": APNSwift] (APNSwift/APNSwiftRequestEncoder.swift:55)
[ DEBUG ] Response - received ["origin": APNSwift] (APNSwift/APNSwiftStreamHandler.swift:35)
[ INFO ] Response - successful ["origin": APNSwift] (APNSwift/APNSwiftStreamHandler.swift:52)
from apns.
@handya thanks for testing that out. Could you try enabling --log trace
? That will include logs from the connection pool which should help debug this.
from apns.
@tanner0101 ok, I think I did that correctly, all entries from one request:
[ codes.vapor.server ] [ TRACE ] Decoded HTTP part: head(HTTPRequestHead { method: GET, uri: "/send-push", version: HTTP/1.1, headers: [("User-Agent", "PostmanRuntime/7.20.1"), ("Accept", "*/*"), ("Cache-Control", "no-cache"), ("Postman-Token", "7f529ded-59a6-4845-94e3-ad4724c55955"), ("Host", "localhost:8080"), ("Accept-Encoding", "gzip, deflate"), ("Connection", "keep-alive")] }) (Vapor/Server/HTTPServerRequestDecoder.swift:42)
[ codes.vapor.server ] [ TRACE ] Decoded HTTP part: end(nil) (Vapor/Server/HTTPServerRequestDecoder.swift:42)
[ codes.vapor.application ] [ INFO ] GET /send-push ["request-id": 5EA1107C-358B-426C-AB76-FD27DFD1429E] (Vapor/HTTP/ApplicationResponder.swift:44)
Request count: 3, core count: 16
[ codes.vapor.application ] [ DEBUG ] Pruning available connection that has closed ["request-id": 5EA1107C-358B-426C-AB76-FD27DFD1429E] (AsyncKit/ConnectionPool/EventLoopConnectionPool.swift:186)
[ codes.vapor.application ] [ DEBUG ] No available connections on this event loop, creating a new one ["request-id": 5EA1107C-358B-426C-AB76-FD27DFD1429E] (AsyncKit/ConnectionPool/EventLoopConnectionPool.swift:193)
[ DEBUG ] Connection - starting ["origin": APNSwift] (APNSwift/APNSwiftConnection.swift:90)
[ DEBUG ] Connection - token factory setup ["origin": APNSwift] (APNSwift/APNSwiftConnection.swift:106)
[ DEBUG ] Creating a new APNS token ["origin": APNSwift] (APNSwift/APNSwiftBearerTokenFactory.swift:28)
[ DEBUG ] Connection - token factory created ["origin": APNSwift] (APNSwift/APNSwiftConnection.swift:110)
[ DEBUG ] Connection - bringing up ["origin": APNSwift] (APNSwift/APNSwiftConnection.swift:119)
[ INFO ] Connection - up ["origin": APNSwift] (APNSwift/APNSwiftConnection.swift:143)
[ DEBUG ] Send - starting up ["origin": APNSwift] (APNSwift/APNSwiftConnection.swift:185)
[ INFO ] Send - sending ["origin": APNSwift] (APNSwift/APNSwiftConnection.swift:204)
[ DEBUG ] Request - building ["origin": APNSwift] (APNSwift/APNSwiftRequestEncoder.swift:55)
[ DEBUG ] Response - received ["origin": APNSwift] (APNSwift/APNSwiftStreamHandler.swift:35)
[ INFO ] Response - successful ["origin": APNSwift] (APNSwift/APNSwiftStreamHandler.swift:52)
[ codes.vapor.application ] [ TRACE ] Releasing connection ["request-id": 5EA1107C-358B-426C-AB76-FD27DFD1429E] (AsyncKit/ConnectionPool/EventLoopConnectionPool.swift:244)
from apns.
@handya yup that's perfect, this is the log I was looking for:
[ codes.vapor.application ] [ DEBUG ] No available connections on this event loop, creating a new one
Now repeat the test again until you get to request count > System.coreCount and that's where the interesting logs will be.
from apns.
Hmm actually, I'm already seeing something potentially interesting:
[ codes.vapor.application ] [ DEBUG ] Pruning available connection that has closed ["request-id": 5EA1107C-358B-426C-AB76-FD27DFD1429E] (AsyncKit/ConnectionPool/EventLoopConnectionPool.swift:186)
It appears that APNSwiftConnection
is reporting that it has closed?
from apns.
@tanner0101 looks like that's it :D
Thanks for your help 🥇
[ codes.vapor.application ] [ INFO ] GET /send-push ["request-id": A3D4A3C1-787B-4BDF-9EB1-693ACD7FDC97] (Vapor/HTTP/ApplicationResponder.swift:44)
Request count: 2, core count: 16
[ codes.vapor.application ] [ TRACE ] Re-using available connection ["request-id": A3D4A3C1-787B-4BDF-9EB1-693ACD7FDC97] (AsyncKit/ConnectionPool/EventLoopConnectionPool.swift:182)
[ DEBUG ] Send - starting up ["origin": APNSwift] (APNSwift/APNSwiftConnection.swift:185)
[ INFO ] Send - sending ["origin": APNSwift] (APNSwift/APNSwiftConnection.swift:204)
[ DEBUG ] Request - building ["origin": APNSwift] (APNSwift/APNSwiftRequestEncoder.swift:55)
[ DEBUG ] Response - received ["origin": APNSwift] (APNSwift/APNSwiftStreamHandler.swift:35)
[ INFO ] Response - successful ["origin": APNSwift] (APNSwift/APNSwiftStreamHandler.swift:52)
[ codes.vapor.application ] [ TRACE ] Releasing connection ["request-id": A3D4A3C1-787B-4BDF-9EB1-693ACD7FDC97] (AsyncKit/ConnectionPool/EventLoopConnectionPool.swift:244)
from apns.
@handya could you test out my fix in your project?
.package("https://github.com/vapor/apns.git", .branch("tn-is-closed-fix"))
We haven't really found a good way to test APNS yet unfortunately since Apple doesn't provide any easy way to do it.
from apns.
@tanner0101 tested on that branch 🥇
[ INFO ] GET /send-push
Request count: 1, core count: 16
[ DEBUG ] Connection - starting ["origin": APNSwift] (APNSwift/APNSwiftConnection.swift:90)
[ DEBUG ] Connection - token factory setup ["origin": APNSwift] (APNSwift/APNSwiftConnection.swift:106)
[ DEBUG ] Creating a new APNS token ["origin": APNSwift] (APNSwift/APNSwiftBearerTokenFactory.swift:28)
[ DEBUG ] Connection - token factory created ["origin": APNSwift] (APNSwift/APNSwiftConnection.swift:110)
[ DEBUG ] Connection - bringing up ["origin": APNSwift] (APNSwift/APNSwiftConnection.swift:119)
[ INFO ] Connection - up ["origin": APNSwift] (APNSwift/APNSwiftConnection.swift:143)
[ DEBUG ] Send - starting up ["origin": APNSwift] (APNSwift/APNSwiftConnection.swift:185)
[ INFO ] Send - sending ["origin": APNSwift] (APNSwift/APNSwiftConnection.swift:204)
[ DEBUG ] Request - building ["origin": APNSwift] (APNSwift/APNSwiftRequestEncoder.swift:55)
[ DEBUG ] Response - received ["origin": APNSwift] (APNSwift/APNSwiftStreamHandler.swift:35)
[ INFO ] Response - successful ["origin": APNSwift] (APNSwift/APNSwiftStreamHandler.swift:52)
[ INFO ] GET /send-push
Request count: 2, core count: 16
[ DEBUG ] Send - starting up ["origin": APNSwift] (APNSwift/APNSwiftConnection.swift:185)
[ INFO ] Send - sending ["origin": APNSwift] (APNSwift/APNSwiftConnection.swift:204)
[ DEBUG ] Request - building ["origin": APNSwift] (APNSwift/APNSwiftRequestEncoder.swift:55)
[ DEBUG ] Response - received ["origin": APNSwift] (APNSwift/APNSwiftStreamHandler.swift:35)
[ INFO ] Response - successful ["origin": APNSwift] (APNSwift/APNSwiftStreamHandler.swift:52)
[ INFO ] GET /send-push
Request count: 3, core count: 16
[ DEBUG ] Send - starting up ["origin": APNSwift] (APNSwift/APNSwiftConnection.swift:185)
[ INFO ] Send - sending ["origin": APNSwift] (APNSwift/APNSwiftConnection.swift:204)
[ DEBUG ] Request - building ["origin": APNSwift] (APNSwift/APNSwiftRequestEncoder.swift:55)
[ DEBUG ] Response - received ["origin": APNSwift] (APNSwift/APNSwiftStreamHandler.swift:35)
[ INFO ] Response - successful ["origin": APNSwift] (APNSwift/APNSwiftStreamHandler.swift:52)
from apns.
Nice, thanks for your help debugging this. I'll merge and tag. :)
from apns.
Related Issues (16)
- Allow swapping of configurations HOT 6
- Mock client for testing HOT 4
- Tag a new relaease? HOT 1
- Connection deadlock after random time HOT 6
- Connection timed out deadlock issue HOT 6
- Jan 9 HOT 2
- Bundle GeoTrust Global CA certificate HOT 1
- Support for sending PushNotifications via ProxyServer HOT 7
- Pass `Request.logger` as default logger parameter HOT 3
- Can't send "contentAvailable" on it's own HOT 14
- Getting invalidProviderToken HOT 1
- Feature Request: How to register the APNSClient with Application? HOT 1
- Update to 4.0.0-beta.2 not working HOT 3
- Use Data, not ByteBuffer HOT 1
- Doesn't work with certificates HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from apns.