Skip to content

Commit 769499e

Browse files
authored
Connect timeout as CLI param (#117)
1 parent 4207850 commit 769499e

File tree

7 files changed

+23
-11
lines changed

7 files changed

+23
-11
lines changed

proxy/proxy.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ type Config struct {
7171
NumConns int
7272
Logger *zap.Logger
7373
HeartBeatInterval time.Duration
74+
ConnectTimeout time.Duration
7475
IdleTimeout time.Duration
7576
RPCAddr string
7677
DC string
@@ -170,6 +171,7 @@ func (p *Proxy) Connect() error {
170171
Resolver: p.config.Resolver,
171172
ReconnectPolicy: p.config.ReconnectPolicy,
172173
HeartBeatInterval: p.config.HeartBeatInterval,
174+
ConnectTimeout: p.config.ConnectTimeout,
173175
IdleTimeout: p.config.IdleTimeout,
174176
Logger: p.logger,
175177
})
@@ -202,6 +204,7 @@ func (p *Proxy) Connect() error {
202204
Version: p.cluster.NegotiatedVersion,
203205
Auth: p.config.Auth,
204206
HeartBeatInterval: p.config.HeartBeatInterval,
207+
ConnectTimeout: p.config.ConnectTimeout,
205208
IdleTimeout: p.config.IdleTimeout,
206209
PreparedCache: p.preparedCache,
207210
Logger: p.logger,
@@ -325,6 +328,7 @@ func (p *Proxy) maybeCreateSession(version primitive.ProtocolVersion, keyspace s
325328
PreparedCache: p.preparedCache,
326329
Keyspace: keyspace,
327330
HeartBeatInterval: p.config.HeartBeatInterval,
331+
ConnectTimeout: p.config.ConnectTimeout,
328332
IdleTimeout: p.config.IdleTimeout,
329333
Logger: p.logger,
330334
})

proxy/proxy_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,7 @@ func setupProxyTestWithConfig(ctx context.Context, numNodes int, cfg *proxyTestC
345345
ReconnectPolicy: proxycore.NewReconnectPolicyWithDelays(200*time.Millisecond, time.Second),
346346
NumConns: 2,
347347
HeartBeatInterval: 30 * time.Second,
348+
ConnectTimeout: 10 * time.Second,
348349
IdleTimeout: 60 * time.Second,
349350
RPCAddr: cfg.rpcAddr,
350351
Peers: cfg.peers,

proxy/run.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ type runConfig struct {
5757
HealthCheck bool `yaml:"health-check" help:"Enable liveness and readiness checks" default:"false" env:"HEALTH_CHECK"`
5858
HttpBind string `yaml:"http-bind" help:"Address to use to bind HTTP server used for health checks" default:":8000" env:"HTTP_BIND"`
5959
HeartbeatInterval time.Duration `yaml:"heartbeat-interval" help:"Interval between performing heartbeats to the cluster" default:"30s" env:"HEARTBEAT_INTERVAL"`
60+
ConnectTimeout time.Duration `yaml:"connect-timeout" help:"Duration before an attempt to connect to a cluster is considered timed out" default:"10s" env:"CONNECT_TIMEOUT"`
6061
IdleTimeout time.Duration `yaml:"idle-timeout" help:"Duration between successful heartbeats before a connection to the cluster is considered unresponsive and closed" default:"60s" env:"IDLE_TIMEOUT"`
6162
ReadinessTimeout time.Duration `yaml:"readiness-timeout" help:"Duration the proxy is unable to connect to the backend cluster before it is considered not ready" default:"30s" env:"READINESS_TIMEOUT"`
6263
IdempotentGraph bool `yaml:"idempotent-graph" help:"If true it will treat all graph queries as idempotent by default and retry them automatically. It may be dangerous to retry some graph queries -- use with caution." default:"false" env:"IDEMPOTENT_GRAPH"`
@@ -178,6 +179,7 @@ func Run(ctx context.Context, args []string) int {
178179
Auth: auth,
179180
Logger: logger,
180181
HeartBeatInterval: cfg.HeartbeatInterval,
182+
ConnectTimeout: cfg.ConnectTimeout,
181183
IdleTimeout: cfg.IdleTimeout,
182184
RPCAddr: cfg.RpcAddress,
183185
DC: cfg.DataCenter,

proxycore/cluster.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import (
3030

3131
const (
3232
DefaultRefreshWindow = 10 * time.Second
33-
DefaultConnectTimeout = 10 * time.Second
3433
DefaultRefreshTimeout = 5 * time.Second
3534
)
3635

@@ -102,11 +101,11 @@ type ClusterConfig struct {
102101
Resolver EndpointResolver
103102
ReconnectPolicy ReconnectPolicy
104103
RefreshWindow time.Duration
104+
HeartBeatInterval time.Duration
105105
ConnectTimeout time.Duration
106106
RefreshTimeout time.Duration
107-
Logger *zap.Logger
108-
HeartBeatInterval time.Duration
109107
IdleTimeout time.Duration
108+
Logger *zap.Logger
110109
}
111110

112111
type ClusterInfo struct {
@@ -190,8 +189,8 @@ func (c *Cluster) OnEvent(frame *frame.Frame) {
190189
}
191190

192191
func (c *Cluster) connect(ctx context.Context, endpoint Endpoint, initial bool) (err error) {
193-
timeout := getOrUseDefault(c.config.ConnectTimeout, DefaultConnectTimeout)
194-
ctx, cancel := context.WithTimeout(context.Background(), timeout)
192+
c.logger.Debug("connecting to cluster", zap.Stringer("connect timeout", c.config.ConnectTimeout))
193+
ctx, cancel := context.WithTimeout(context.Background(), c.config.ConnectTimeout)
195194
defer cancel()
196195

197196
conn, err := ConnectClient(ctx, endpoint, ClientConnConfig{Handler: c, Logger: c.logger})
@@ -233,7 +232,7 @@ func (c *Cluster) connect(ctx context.Context, endpoint Endpoint, initial bool)
233232
c.Info = info
234233
}
235234

236-
go conn.Heartbeats(timeout, version, c.config.HeartBeatInterval, c.config.IdleTimeout, c.logger)
235+
go conn.Heartbeats(c.config.ConnectTimeout, version, c.config.HeartBeatInterval, c.config.IdleTimeout, c.logger)
237236

238237
return c.mergeHosts(hosts)
239238
}

proxycore/connpool.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,10 @@ func (p *connPool) leastBusyConn() *ClientConn {
129129
}
130130

131131
func (p *connPool) connect() (conn *ClientConn, err error) {
132-
timeout := getOrUseDefault(p.config.ConnectTimeout, DefaultConnectTimeout)
133-
ctx, cancel := context.WithTimeout(p.ctx, timeout)
132+
p.logger.Debug("creating connection pool",
133+
zap.Stringer("endpoint", p.config.Endpoint),
134+
zap.Stringer("connect timeout", p.config.ConnectTimeout))
135+
ctx, cancel := context.WithTimeout(p.ctx, p.config.ConnectTimeout)
134136
defer cancel()
135137
conn, err = ConnectClient(ctx, p.config.Endpoint, ClientConnConfig{
136138
PreparedCache: p.preparedCache,
@@ -149,7 +151,7 @@ func (p *connPool) connect() (conn *ClientConn, err error) {
149151
version, err = conn.Handshake(ctx, p.config.Version, p.config.Auth)
150152
if err != nil {
151153
if errors.Is(err, context.DeadlineExceeded) {
152-
return nil, fmt.Errorf("handshake took longer than %s to complete", timeout)
154+
return nil, fmt.Errorf("handshake took longer than %s to complete", p.config.ConnectTimeout)
153155
}
154156
return nil, err
155157
}
@@ -165,7 +167,7 @@ func (p *connPool) connect() (conn *ClientConn, err error) {
165167
}
166168
}
167169

168-
go conn.Heartbeats(timeout, p.config.Version, p.config.HeartBeatInterval, p.config.IdleTimeout, p.logger)
170+
go conn.Heartbeats(p.config.ConnectTimeout, p.config.Version, p.config.HeartBeatInterval, p.config.IdleTimeout, p.logger)
169171
return conn, nil
170172
}
171173

proxycore/connpool_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ func TestConnectPool_InvalidAuth(t *testing.T) {
131131
ReconnectPolicy: NewReconnectPolicy(),
132132
NumConns: 2,
133133
Version: supported,
134+
ConnectTimeout: 20 * time.Second,
134135
},
135136
})
136137
if assert.Error(t, err) {
@@ -162,6 +163,7 @@ func TestConnectPool_AuthExpected(t *testing.T) {
162163
ReconnectPolicy: NewReconnectPolicy(),
163164
NumConns: 2,
164165
Version: supported,
166+
ConnectTimeout: 20 * time.Second,
165167
},
166168
})
167169
if assert.Error(t, err) {
@@ -190,6 +192,7 @@ func TestConnectPool_InvalidProtocolVersion(t *testing.T) {
190192
ReconnectPolicy: NewReconnectPolicy(),
191193
NumConns: 2,
192194
Version: wanted,
195+
ConnectTimeout: 20 * time.Second,
193196
},
194197
})
195198
if assert.Error(t, err) {
@@ -241,6 +244,7 @@ func TestConnectPool_InvalidKeyspace(t *testing.T) {
241244
ReconnectPolicy: NewReconnectPolicy(),
242245
NumConns: 2,
243246
Version: supported,
247+
ConnectTimeout: 20 * time.Second,
244248
},
245249
})
246250
if assert.Error(t, err) {

proxycore/session.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,12 @@ type SessionConfig struct {
4848
Keyspace string
4949
Version primitive.ProtocolVersion
5050
Auth Authenticator
51-
Logger *zap.Logger
5251
// PreparedCache a global cache share across sessions for storing previously prepared queries
5352
PreparedCache PreparedCache
5453
ConnectTimeout time.Duration
5554
HeartBeatInterval time.Duration
5655
IdleTimeout time.Duration
56+
Logger *zap.Logger
5757
}
5858

5959
type Session struct {

0 commit comments

Comments
 (0)