Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

config: add Dialer option to connect through a proxy #296

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ type BackoffStrategy interface {
Calculate(attempt int) time.Duration
}

// A Dialer is a means to establish a connection.
type Dialer interface {
// Dial connects to the given address via the proxy.
Dial(network, addr string) (c net.Conn, err error)
}

// ExponentialStrategy implements an exponential backoff strategy (default)
type ExponentialStrategy struct {
cfg *Config
Expand Down Expand Up @@ -92,6 +98,7 @@ type Config struct {
// used to Initialize, Validate
configHandlers []configHandler

Dialer Dialer `opt:"dialer"`
DialTimeout time.Duration `opt:"dial_timeout" default:"1s"`
HaoweiCh marked this conversation as resolved.
Show resolved Hide resolved

// Deadlines for network reads and writes
Expand Down
9 changes: 6 additions & 3 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,12 @@ func (c *Conn) getLogger() (logger, LogLevel, string) {
// Connect dials and bootstraps the nsqd connection
// (including IDENTIFY) and returns the IdentifyResponse
func (c *Conn) Connect() (*IdentifyResponse, error) {
dialer := &net.Dialer{
LocalAddr: c.config.LocalAddr,
Timeout: c.config.DialTimeout,
dialer := c.config.Dialer
if dialer == nil {
dialer = &net.Dialer{
LocalAddr: c.config.LocalAddr,
Timeout: c.config.DialTimeout,
}
}

conn, err := dialer.Dial("tcp", c.addr)
Expand Down