Skip to content
This repository was archived by the owner on Aug 4, 2021. It is now read-only.

Commit 40631d0

Browse files
Prevent being blocked if no connection is available in the pool
1 parent 7bccaaf commit 40631d0

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

util/lock.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ import (
77
"github.com/timescale/prometheus-postgresql-adapter/log"
88
"strconv"
99
"sync"
10+
"time"
11+
)
12+
13+
const (
14+
waitForConnectionTimeout = time.Second
1015
)
1116

1217
// PgAdvisoryLock is implementation of leader election based on PostgreSQL advisory locks. All adapters withing a HA group are trying
@@ -35,7 +40,8 @@ func getConn(pool *sql.DB, cur, maxRetries int) (*sql.Conn, error) {
3540
if maxRetries == cur {
3641
return nil, fmt.Errorf("max attempts reached. giving up on getting a db connection")
3742
}
38-
lockConn, err := pool.Conn(context.Background())
43+
ctx, _ := context.WithTimeout(context.Background(), waitForConnectionTimeout)
44+
lockConn, err := pool.Conn(ctx)
3945
if err != nil {
4046
return nil, fmt.Errorf("error getting DB connection: %v", err)
4147
}

0 commit comments

Comments
 (0)