database.swift 1.1 KB

123456789101112131415161718192021222324252627282930313233
  1. import Hummingbird
  2. import PostgresKit
  3. // tfb-server (aka, citrine) uses 28 hyper-threaded cores
  4. // postgresql.conf specifies max_connections = 2000
  5. //
  6. // 2000 / (28 * 2) = 35.7 (theoretical max)
  7. //
  8. // https://github.com/TechEmpower/FrameworkBenchmarks/wiki/Project-Information-Environment#citrine-self-hosted
  9. // https://github.com/TechEmpower/FrameworkBenchmarks/blob/master/toolset/databases/postgres/postgresql.conf#L64
  10. let maxConnectionsPerEventLoop = 32
  11. var connectionPool: EventLoopGroupConnectionPool<PostgresConnectionSource>!
  12. extension HBApplication {
  13. func initConnectionPool() {
  14. connectionPool = EventLoopGroupConnectionPool(
  15. source: PostgresConnectionSource(configuration: .init(
  16. hostname: "tfb-database",
  17. username: "benchmarkdbuser",
  18. password: "benchmarkdbpass",
  19. database: "hello_world"
  20. )),
  21. maxConnectionsPerEventLoop: maxConnectionsPerEventLoop,
  22. on: self.eventLoopGroup
  23. )
  24. }
  25. }
  26. extension HBRequest {
  27. var db: PostgresDatabase {
  28. connectionPool.pool(for: self.eventLoop).database(logger: self.logger)
  29. }
  30. }