Code Monkey home page Code Monkey logo

Comments (10)

tanner0101 avatar tanner0101 commented on May 12, 2024 1

@MrMage see here: https://github.com/vapor/fluent-postgresql/blob/master/Sources/FluentPostgresDriver/Postgres%2BFluent.swift#L35

ConnectionPool conforms to Fluent.Database using withConnection(_:).

from auth.

tanner0101 avatar tanner0101 commented on May 12, 2024

Yeah, this is unfortunately a big problem with Vapor 3's design. See here for additional discussion: vapor/vapor#1711

Vapor 4's design is exactly what you described: anything that needs connections, whether it be a middleware or a controller will get a pool. Each query to the DB will pop a connection from that pool for the lifetime of the query. This will allow a max connection pool size of 1 to work without causing deadlocks.

As for porting this fix back to Vapor 3, I'm not sure it's worth the potential issues it could cause. I see this as more of a fundamental design flaw than a bug.

from auth.

tanner0101 avatar tanner0101 commented on May 12, 2024

See also: vapor/fluent#564 (comment)

from auth.

MrMage avatar MrMage commented on May 12, 2024

Vapor 4's design is exactly what you described: anything that needs connections, whether it be a middleware or a controller will get a pool. Each query to the DB will pop a connection from that pool for the lifetime of the query. This will allow a max connection pool size of 1 to work without causing deadlocks.

That sounds great! Would you mind providing a pointer to how this is going to be implemented (or is implemented) in Fluent 4? In particular, how does the pool know when the query is finished? For something like db.withPooledConnection { connection in query(on: connection) }, this is obvious, but how would

let db = try container.connectionPool(for: .sqlite)
router.get("work") { req -> String in
    _ = Todo.query(on: db).all().map { todos in
        print(todos)
    }
    return "done"
}

work with regards to detecting when the query is done?

As for porting this fix back to Vapor 3, I'm not sure it's worth the potential issues it could cause. I see this as more of a fundamental design flaw than a bug.

Agreed. I guess we can close this issue, but I'd still like to have a look at the Fluent 4 pool implementation :-)

from auth.

MrMage avatar MrMage commented on May 12, 2024

ConnectionPool conforms to Fluent.Database using withConnection(_:).

Looks great! I guess in theory the connection could even be released before onOutput is called; is that the case?

from auth.

tanner0101 avatar tanner0101 commented on May 12, 2024

Theoretically yes, but in reality NIOMySQL and NIOPostgres don't complete their return futures until they have passed back all rows to the user.

from auth.

mxcl avatar mxcl commented on May 12, 2024

So… for those of us are experiencing this and who are building something where we anticipate heavy usage in the near future, we should opt in to beta test Vapor 4?

from auth.

jdmcd avatar jdmcd commented on May 12, 2024

@mxcl Depends on your database setup mostly. If you can have a pool size per loop greater than the potential deadlock minimum, then you're ok. Otherwise you should either generate a database connection manually (instead of passing in Request as the DatabaseConnectable object, since that uses request caching) or try out Vapor 4 (Forewarning, I have no idea what the state of the alphas/betas are, I don't think they're even out yet)

from auth.

mxcl avatar mxcl commented on May 12, 2024

I appreciate the advice, thank you.

from auth.

jdmcd avatar jdmcd commented on May 12, 2024

No problem, let me know if I can help in any way!

from auth.

Related Issues (20)

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.