Code Monkey home page Code Monkey logo

Comments (15)

tanner0101 avatar tanner0101 commented on May 13, 2024 1

Ah, found the bug: https://github.com/vapor/apns/blob/master/Sources/APNS/APNSConnectionSource.swift#L24

That needs a !

from apns.

grosch avatar grosch commented on May 13, 2024

It should definitely be reusing the connection, where possible!

from apns.

kylebrowning avatar kylebrowning commented on May 13, 2024

cc @tanner0101 So it's not pooling I presume?

from apns.

grosch avatar grosch commented on May 13, 2024

@tanner0101 Remember common usage would be like so

tokens.forEach { token in 
    app.apns.send(token, ...
}

from apns.

kylebrowning avatar kylebrowning commented on May 13, 2024

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.

tanner0101 avatar tanner0101 commented on May 13, 2024

@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.

handya avatar handya commented on May 13, 2024

@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.

tanner0101 avatar tanner0101 commented on May 13, 2024

@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.

handya avatar handya commented on May 13, 2024

@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.

tanner0101 avatar tanner0101 commented on May 13, 2024

@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.

tanner0101 avatar tanner0101 commented on May 13, 2024

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.

handya avatar handya commented on May 13, 2024

@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.

tanner0101 avatar tanner0101 commented on May 13, 2024

@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.

handya avatar handya commented on May 13, 2024

@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.

tanner0101 avatar tanner0101 commented on May 13, 2024

Nice, thanks for your help debugging this. I'll merge and tag. :)

from apns.

Related Issues (16)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.