Skip to content

Commit dda98da

Browse files
authored
feat: Add Postgres ConnPool Config (#5226)
Add Postgres ConnPool Config for user to tune the ConnPool. Signed-off-by: bruce <[email protected]>
1 parent 7373110 commit dda98da

File tree

6 files changed

+69
-21
lines changed

6 files changed

+69
-21
lines changed

cmd/core-common-config-bootstrapper/res/configuration.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ all-services:
4646
Port: 5432
4747
Timeout: "5s"
4848
Type: "postgres"
49+
MaxConns: 4
50+
MaxConnIdleTime: "30m"
51+
MaxConnLifetime: "1h"
4952

5053
MessageBus:
5154
Protocol: "mqtt"

cmd/core-data/res/configuration.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ Service:
1515
Host: "localhost"
1616
StartupMsg: "This is the Core Data Microservice"
1717

18+
Database:
19+
MaxConns: 4
20+
MaxConnIdleTime: "30m"
21+
MaxConnLifetime: "1h"
22+
1823
Clients:
1924
core-metadata:
2025
Protocol: http

cmd/core-keeper/res/configuration.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ Database:
3939
Port: 5432
4040
Timeout: "5s"
4141
Type: "postgres"
42+
MaxConns: 4
43+
MaxConnIdleTime: "30m"
44+
MaxConnLifetime: "1h"
4245

4346
MessageBus:
4447
Protocol: "mqtt"

internal/pkg/bootstrap/handlers/database.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,13 @@ func (d Database) newDBClient(
6767
databaseInfo := d.database.GetDatabaseInfo()
6868

6969
databaseConfig := db.Configuration{
70-
Host: databaseInfo.Host,
71-
Port: databaseInfo.Port,
72-
Password: credentials.Password,
73-
Timeout: databaseInfo.Timeout,
70+
Host: databaseInfo.Host,
71+
Port: databaseInfo.Port,
72+
Password: credentials.Password,
73+
Timeout: databaseInfo.Timeout,
74+
MaxConns: databaseInfo.MaxConns,
75+
MaxConnIdleTime: databaseInfo.MaxConnIdleTime,
76+
MaxConnLifetime: databaseInfo.MaxConnLifetime,
7477
}
7578

7679
switch databaseInfo.Type {

internal/pkg/db/db.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,15 @@ var (
2929
)
3030

3131
type Configuration struct {
32-
DbType string
33-
Host string
34-
Port int
35-
Timeout string
36-
DatabaseName string
37-
Username string
38-
Password string
39-
BatchSize int
32+
DbType string
33+
Host string
34+
Port int
35+
Timeout string
36+
DatabaseName string
37+
Username string
38+
Password string
39+
BatchSize int
40+
MaxConns int
41+
MaxConnIdleTime string
42+
MaxConnLifetime string
4043
}

internal/pkg/db/postgres/client.go

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"net/url"
1313
"os"
1414
"sync"
15+
"time"
1516

1617
"github.com/jackc/pgx/v5/pgxpool"
1718

@@ -22,6 +23,9 @@ import (
2223
)
2324

2425
const defaultDBName = "edgex_db"
26+
const defaultMaxConns = int32(4)
27+
const defaultMaxConnIdleTime = time.Minute * 30
28+
const defaultMaxConnLifetime = time.Hour
2529

2630
var once sync.Once
2731
var dc *Client
@@ -35,17 +39,15 @@ type Client struct {
3539

3640
// NewClient returns a pointer to the Postgres client
3741
func NewClient(ctx context.Context, config db.Configuration, lc logger.LoggingClient, schemaName, serviceKey, serviceVersion string, sqlFiles embed.FS) (*Client, errors.EdgeX) {
38-
// Get the database name from the environment variable
39-
databaseName := os.Getenv("EDGEX_DBNAME")
40-
if databaseName == "" {
41-
databaseName = defaultDBName
42-
}
43-
4442
var edgeXerr errors.EdgeX
4543
once.Do(func() {
46-
// use url encode to prevent special characters in the connection string
47-
connectionStr := "postgres://" + fmt.Sprintf("%s:%s@%s:%d/%s", url.PathEscape(config.Username), url.PathEscape(config.Password), url.PathEscape(config.Host), config.Port, url.PathEscape(databaseName))
48-
dbPool, err := pgxpool.New(ctx, connectionStr)
44+
connPoolConfig, err := connPoolConConfig(config, lc)
45+
if err != nil {
46+
edgeXerr = WrapDBError("fail to parse pg conn pool config", err)
47+
return
48+
}
49+
50+
dbPool, err := pgxpool.NewWithConfig(ctx, connPoolConfig)
4951
if err != nil {
5052
edgeXerr = WrapDBError("fail to create pg connection pool", err)
5153
}
@@ -78,6 +80,35 @@ func NewClient(ctx context.Context, config db.Configuration, lc logger.LoggingCl
7880
return dc, nil
7981
}
8082

83+
func connPoolConConfig(config db.Configuration, lc logger.LoggingClient) (*pgxpool.Config, error) {
84+
// Get the database name from the environment variable
85+
databaseName := os.Getenv("EDGEX_DBNAME")
86+
if databaseName == "" {
87+
databaseName = defaultDBName
88+
}
89+
90+
// use url encode to prevent special characters in the connection string
91+
connectionStr := "postgres://" + fmt.Sprintf("%s:%s@%s:%d/%s", url.PathEscape(config.Username), url.PathEscape(config.Password), url.PathEscape(config.Host), config.Port, url.PathEscape(databaseName))
92+
connPoolConfig, err := pgxpool.ParseConfig(connectionStr)
93+
if err != nil {
94+
return nil, WrapDBError("fail to parse pg connection string", err)
95+
}
96+
connPoolConfig.MaxConns = int32(config.MaxConns)
97+
if connPoolConfig.MaxConns <= 0 {
98+
lc.Errorf("The MaxConns too small, use default '%d'", defaultMaxConns)
99+
connPoolConfig.MaxConns = defaultMaxConns
100+
}
101+
if connPoolConfig.MaxConnIdleTime, err = time.ParseDuration(config.MaxConnIdleTime); err != nil {
102+
connPoolConfig.MaxConnIdleTime = defaultMaxConnIdleTime
103+
lc.Errorf("Fail to parse pg conn pool MaxConnIdleTime config, use default '%v', err: %v", defaultMaxConnIdleTime, err)
104+
}
105+
if connPoolConfig.MaxConnLifetime, err = time.ParseDuration(config.MaxConnLifetime); err != nil {
106+
connPoolConfig.MaxConnLifetime = defaultMaxConnLifetime
107+
lc.Errorf("Fail to parse pg conn pool MaxConnLifetime config, use default '%v', err: %v", defaultMaxConnLifetime, err)
108+
}
109+
return connPoolConfig, nil
110+
}
111+
81112
// CloseSession closes the connections to postgres
82113
func (c *Client) CloseSession() {
83114
c.ConnPool.Close()

0 commit comments

Comments
 (0)