Skip to content

Commit 8214ecf

Browse files
client/pool: add Option to supply custom Dialer (#1086)
1 parent a1483a5 commit 8214ecf

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

client/pool.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"log/slog"
66
"math"
77
"math/rand"
8+
"net"
89
"sync"
910
"time"
1011

@@ -118,7 +119,7 @@ func NewPoolWithOptions(
118119
idlePingTimeout: Timestamp(math.Ceil(MaxIdleTimeoutWithoutPing.Seconds())),
119120

120121
connect: func() (*Conn, error) {
121-
return Connect(addr, user, password, dbName, po.connOptions...)
122+
return ConnectWithDialer(context.Background(), "", addr, user, password, dbName, po.dialer, po.connOptions...)
122123
},
123124

124125
readyConnection: make(chan Connection),
@@ -606,10 +607,12 @@ func (pool *Pool) checkConnection(ctx context.Context) error {
606607

607608
// getDefaultPoolOptions returns pool config for low load services
608609
func getDefaultPoolOptions() poolOptions {
610+
dialer := &net.Dialer{Timeout: 10 * time.Second}
609611
return poolOptions{
610612
logger: slog.Default(),
611613
minAlive: 1,
612614
maxAlive: 10,
613615
maxIdle: 2,
616+
dialer: dialer.DialContext,
614617
}
615618
}

client/pool_options.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ type (
1818
password string
1919
dbName string
2020

21+
dialer Dialer
22+
2123
connOptions []Option
2224

2325
newPoolPingTimeout time.Duration
@@ -59,3 +61,9 @@ func WithNewPoolPingTimeout(timeout time.Duration) PoolOption {
5961
o.newPoolPingTimeout = timeout
6062
}
6163
}
64+
65+
func WithDialer(dialer Dialer) PoolOption {
66+
return func(o *poolOptions) {
67+
o.dialer = dialer
68+
}
69+
}

0 commit comments

Comments
 (0)