DEV Community

Discussion on: A Web App in Rust - 13 Connection Pooling

lwshang profile image
Linwei Shang • Edited on

Create the connections pool inside the anonymous function (the argument of HttpServer::new()) actually create a pool for each HttpServer worker thread. On my server, this behavior resulted in 16 pools x 10 connections per pool = 160 connections.

The 16 is from actix-web:

HttpServer automatically starts a number of HTTP workers, by default this number is equal to the number of logical CPUs in the system. This number can be overridden with the HttpServer::workers() method.

My CPU has 16 hyper-threads.

10 is the default size of each pool which can be changed via max_size() during construction.

That attempt to get 160 connections to PostgreSQL exceeded the default constraint max_connections = 100. So I got panic messages shouting "Sorry, too many clients already".

I would suggest to follow the implementation from the actix example. Basically, it creates the pool outside the HttpServer construction.

krowemoh profile image
Nivethan Author

Updated, thanks!