Skip to content

Commit

Permalink
config: make dialer an interface (apache#1398)
Browse files Browse the repository at this point in the history
  • Loading branch information
Zariel authored Jan 31, 2020
1 parent 95d072f commit 68212b7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
7 changes: 6 additions & 1 deletion cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package gocql

import (
"context"
"errors"
"net"
"time"
Expand Down Expand Up @@ -146,12 +147,16 @@ type ClusterConfig struct {

// Dialer will be used to establish all connections created for this Cluster.
// If not provided, a default dialer configured with ConnectTimeout will be used.
Dialer *net.Dialer
Dialer Dialer

// internal config for testing
disableControlConn bool
}

type Dialer interface {
DialContext(ctx context.Context, network, addr string) (net.Conn, error)
}

// NewCluster generates a new config for the default cluster implementation.
//
// The supplied hosts are used to initially connect to the cluster then the rest of
Expand Down
11 changes: 6 additions & 5 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ type ConnConfig struct {
CQLVersion string
Timeout time.Duration
ConnectTimeout time.Duration
Dialer *net.Dialer
Dialer Dialer
Compressor Compressor
Authenticator Authenticator
AuthProvider func(h *HostInfo) (Authenticator, error)
Expand Down Expand Up @@ -203,14 +203,15 @@ func (s *Session) dialWithoutObserver(ctx context.Context, host *HostInfo, cfg *

dialer := cfg.Dialer
if dialer == nil {
dialer = &net.Dialer{
d := &net.Dialer{
Timeout: cfg.ConnectTimeout,
}
if cfg.Keepalive > 0 {
d.KeepAlive = cfg.Keepalive
}
dialer = d
}

if cfg.Keepalive > 0 {
dialer.KeepAlive = cfg.Keepalive
}

conn, err := dialer.DialContext(ctx, "tcp", host.HostnameAndPort())
if err != nil {
Expand Down

0 comments on commit 68212b7

Please sign in to comment.