Skip to content

Commit 6633c2a

Browse files
committed
add WithConnectionsPerEndpoint and WithLocationPreference options
1 parent f28fded commit 6633c2a

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

client.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@ import (
1414
qq "github.com/adwski/ydb-go-query/query"
1515
)
1616

17-
const (
18-
defaultConnectionsPerEndpoint = 2
19-
)
20-
2117
var (
2218
ErrNoInitialNodes = errors.New("no initial nodes was provided")
2319
ErrDBEmpty = errors.New("db is empty")
@@ -127,8 +123,8 @@ func newClient(ctx context.Context, cfg *Config, opts ...Option) (*Client, error
127123
InitNodes: cfg.InitialNodes,
128124
DB: cfg.DB,
129125
Balancer: balancer.Config{
130-
LocationPreference: []string{"TODO"},
131-
ConnsPerEndpoint: defaultConnectionsPerEndpoint,
126+
LocationPreference: cfg.locationPreference,
127+
ConnsPerEndpoint: cfg.connectionsPerEndpoint,
132128
IgnoreLocations: false,
133129
},
134130
TransportCredentials: cfg.transportCredentials,

config.go

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package ydbgoquery
33
import (
44
"context"
55
"errors"
6+
"strings"
67
"time"
78

89
"github.com/adwski/ydb-go-query/internal/logger"
@@ -23,9 +24,10 @@ import (
2324
)
2425

2526
const (
26-
defaultSessionCreateTimeout = 3 * time.Second
27-
defaultQueryTimeout = 5 * time.Minute
28-
defaultSessionPoolSize = 10
27+
defaultSessionCreateTimeout = 3 * time.Second
28+
defaultQueryTimeout = 5 * time.Minute
29+
defaultSessionPoolSize = 10
30+
defaultConnectionsPerEndpoint = 2
2931
)
3032

3133
var (
@@ -43,10 +45,14 @@ type (
4345
DB string
4446
InitialNodes []string
4547

48+
locationPreference []string
49+
4650
poolSize uint
4751
poolReadyHi uint
4852
poolReadyLo uint
4953

54+
connectionsPerEndpoint int
55+
5056
sessionCreateTimeout time.Duration
5157
queryTimeout time.Duration
5258
}
@@ -58,6 +64,7 @@ func (cfg *Config) setDefaults() {
5864
cfg.sessionCreateTimeout = defaultSessionCreateTimeout
5965
cfg.queryTimeout = defaultQueryTimeout
6066
cfg.poolSize = defaultSessionPoolSize
67+
cfg.connectionsPerEndpoint = defaultConnectionsPerEndpoint
6168
cfg.transportCredentials = transportCreds.Insecure()
6269
cfg.txSettings = txsettings.SerializableReadWrite()
6370
}
@@ -123,6 +130,27 @@ func WithSessionPoolReadyThresholds(high, low uint) Option {
123130
}
124131
}
125132

133+
func WithLocationPreference(pref string) Option {
134+
return func(ctx context.Context, cfg *Config) error {
135+
cfg.locationPreference = strings.Split(pref, ",")
136+
for idx := range cfg.locationPreference {
137+
cfg.locationPreference[idx] = strings.TrimSpace(cfg.locationPreference[idx])
138+
}
139+
140+
return nil
141+
}
142+
}
143+
144+
func WithConnectionsPerEndpoint(connections int) Option {
145+
return func(ctx context.Context, cfg *Config) error {
146+
if connections > 0 {
147+
cfg.connectionsPerEndpoint = connections
148+
}
149+
150+
return nil
151+
}
152+
}
153+
126154
func withTransportSecurity(credentials credentials.TransportCredentials) Option {
127155
return func(ctx context.Context, cfg *Config) error {
128156
cfg.transportCredentials = credentials

0 commit comments

Comments
 (0)