From 87b8816bf933955980ddfdf60a8a1f740e6c21f9 Mon Sep 17 00:00:00 2001 From: HaoweiCh Date: Thu, 7 May 2020 08:13:40 +0800 Subject: [PATCH 1/2] support custom dialer to support proxy --- config.go | 7 +++++++ conn.go | 9 ++++++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/config.go b/config.go index 05a81575..7faed608 100644 --- a/config.go +++ b/config.go @@ -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 @@ -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"` // Deadlines for network reads and writes diff --git a/conn.go b/conn.go index f4180a4d..29c316a6 100644 --- a/conn.go +++ b/conn.go @@ -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) From 7c19a91142da60348b96dfbf78866dfc831c4133 Mon Sep 17 00:00:00 2001 From: 221 <221@404.com> Date: Fri, 8 May 2020 15:34:16 +0800 Subject: [PATCH 2/2] add notes --- config.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/config.go b/config.go index 7faed608..a529392e 100644 --- a/config.go +++ b/config.go @@ -98,7 +98,6 @@ type Config struct { // used to Initialize, Validate configHandlers []configHandler - Dialer Dialer `opt:"dialer"` DialTimeout time.Duration `opt:"dial_timeout" default:"1s"` // Deadlines for network reads and writes @@ -108,6 +107,10 @@ type Config struct { // LocalAddr is the local address to use when dialing an nsqd. // If empty, a local address is automatically chosen. LocalAddr net.Addr `opt:"local_addr"` + // Dialer affect connection when dialing an nsqd. Overwrite this to connect over proxy. + // + // Conflict with options LocalAddr and DialTimeout. + Dialer Dialer `opt:"dialer"` // Duration between polling lookupd for new producers, and fractional jitter to add to // the lookupd pool loop. this helps evenly distribute requests even if multiple consumers