Updating README.md adding 'Configuring the Database Connection Pool'. Updating main.go configurating the database connection pool with maxOpenConns, maxIdleConns and maxIdleTime values.
This commit is contained in:
11
README.md
11
README.md
@@ -200,4 +200,13 @@ To read more about PostgreSQL optimization :
|
||||
https://www.enterprisedb.com/postgres-tutorials/how-tune-postgresql-memory
|
||||
|
||||
To generate suggested values based on your available system hardware, you can use :
|
||||
https://pgtune.leopard.in.ua
|
||||
https://pgtune.leopard.in.ua
|
||||
|
||||
#### Configuring the Database Connection Pool
|
||||
|
||||
1. You should explicitly set a **MaxOpenConns** value. This should be comfortably below any hard limits on the number of connections imposed by your database and infrastructure, and you may also want to consider keeping it fairly low to act as a rudimentary throttle.
|
||||
For this project we'll set a **MaxOpenConns** limit of 25 connections. This is a reasonable starting point for small-to-medium web applications and APIs, but ideally you should tweak this value for your hardware depending on the results of benchmarking and load-testing.
|
||||
2. In general, higher **MaxOpenConns** and **MaxIdleConns** values will lead to better performance. But the returns are diminishing, and you should be aware that having a too-large idle connection pool (with connections that are not frequently re-used) can actually lead to reduced performance and unnecessary resource consumption.
|
||||
Because **MaxIdleConns** should always than or equal to **MaxOpensConns**, we'll also limit **MaxIdleConns** to 25 connections for this project.
|
||||
3. To mitigate the risk from point 2 above, you should generally set a ConnMaxIdleTime value to remove idle connections that haven't been used for a long time. In this project we'll set a ConnMaxIdleTime duration of 15 minutes.
|
||||
4. It's probably OK to leave **ConnMaxLifetime** as unlimited, unless your database imposes a hard limit on connection lifetime, or you need it specifically to facilitate something like gracefully swapping databases. Neither of those things apply in this project, so we'll leave this as the default unlimited setting.
|
||||
@@ -28,11 +28,15 @@ and the name of the current operating environment for the application (developme
|
||||
We'll read in the configuration settings from command-line flags when the application starts.
|
||||
*/
|
||||
// Add a db struct field to hold the configuration settings for our database connection pool. For now, this only holds the DSN, which we will read in from a command-line flag.
|
||||
// Add maxOpenConns, maxIdleConns and maxIdleTime fields to hold the configuration settings for the connection pool
|
||||
type config struct {
|
||||
port int
|
||||
env string
|
||||
db struct {
|
||||
dsn string
|
||||
dsn string
|
||||
maxOpenConns int
|
||||
maxIdleConns int
|
||||
maxIdleTime time.Duration
|
||||
}
|
||||
}
|
||||
|
||||
@@ -60,6 +64,11 @@ func main() {
|
||||
// Use the value of the GREENLIGHT_DB_DSN environment variable as the default value for our db-dsn command-line flag.
|
||||
flag.StringVar(&cfg.db.dsn, "db-dsn", os.Getenv("GREENLIGHT_DB_DSN"), "PostgreSQL DSN")
|
||||
|
||||
// Read the connection pool settings from command-line flags into the config struct.
|
||||
flag.IntVar(&cfg.db.maxOpenConns, "db-max-open-conns", 25, "PostgreSQL max open connections")
|
||||
flag.IntVar(&cfg.db.maxIdleConns, "db-max-idle-conns", 25, "PostgreSQL max idle connections")
|
||||
flag.DurationVar(&cfg.db.maxIdleTime, "db-max-idle-time", 15*time.Minute, "PostgreSQL max connection idle time")
|
||||
|
||||
flag.Parse()
|
||||
|
||||
// Initialize a new structured logger which writes log entries to standard out stream.
|
||||
@@ -117,6 +126,15 @@ func openDB(cfg config) (*sql.DB, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Set the maximum number of open (in-use + idle) connections in the pool. Note that passing a value less than or equal to 0 will mean there is no limit.
|
||||
db.SetMaxOpenConns(cfg.db.maxOpenConns)
|
||||
|
||||
// Set the maximum number of idle connections in the pool. Again, passing a value less than or equal to 0 will mean there is no limit.
|
||||
db.SetMaxIdleConns(cfg.db.maxIdleConns)
|
||||
|
||||
// Set the maximum idle timeout for connections in the pool. Passing a duration less than or equal to 0 will mean that connections are not closed due to their idle time.
|
||||
db.SetConnMaxIdleTime(cfg.db.maxIdleTime)
|
||||
|
||||
// Create a context with a 5-second timeout deadline.
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
Reference in New Issue
Block a user