-
Notifications
You must be signed in to change notification settings - Fork 1
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
Stop old dialers when we get a new proxy config #1445
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,8 +29,14 @@ type fastConnectDialer struct { | |
|
||
next func(*Options, Dialer) Dialer | ||
opts *Options | ||
|
||
// Create a channel for stopping connections to dialers | ||
stopCh chan struct{} | ||
} | ||
|
||
// Make sure fastConnectDialer implements Dialer | ||
var _ Dialer = (*fastConnectDialer)(nil) | ||
|
||
func newFastConnectDialer(opts *Options, next func(opts *Options, existing Dialer) Dialer) *fastConnectDialer { | ||
if opts.OnError == nil { | ||
opts.OnError = func(error, bool) {} | ||
|
@@ -45,6 +51,7 @@ func newFastConnectDialer(opts *Options, next func(opts *Options, existing Diale | |
opts: opts, | ||
next: next, | ||
topDialer: protectedDialer{}, | ||
stopCh: make(chan struct{}), | ||
} | ||
} | ||
|
||
|
@@ -76,6 +83,13 @@ func (fcd *fastConnectDialer) DialContext(ctx context.Context, network, addr str | |
return conn, err | ||
} | ||
|
||
func (fcd *fastConnectDialer) Close() { | ||
// We don't call Stop on the Dialers themselves here because they are likely | ||
// in use by other Dialers, such as the BanditDialer. | ||
// Stop all dialing | ||
fcd.stopCh <- struct{}{} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would make |
||
} | ||
|
||
func (fcd *fastConnectDialer) onConnected(pd ProxyDialer, connectTime time.Duration) { | ||
log.Debugf("Connected to %v", pd.Name()) | ||
|
||
|
@@ -99,13 +113,23 @@ func (fcd *fastConnectDialer) connectAll(dialers []ProxyDialer) { | |
return | ||
} | ||
log.Debugf("Dialing all dialers in parallel %#v", dialers) | ||
// Loop until we're connected | ||
for len(fcd.connected.dialers) < 2 { | ||
fcd.parallelDial(dialers) | ||
// Add jitter to avoid thundering herd | ||
time.Sleep(time.Duration(rand.Intn(4000)) * time.Millisecond) | ||
outerLoop: | ||
for { | ||
select { | ||
case <-fcd.stopCh: | ||
log.Debug("Stopping parallel dialing") | ||
return | ||
default: | ||
// Loop until we're connected | ||
if len(fcd.connected.dialers) < 2 { | ||
fcd.parallelDial(dialers) | ||
// Add jitter to avoid thundering herd | ||
time.Sleep(time.Duration(rand.Intn(4000)) * time.Millisecond) | ||
} else { | ||
break outerLoop | ||
} | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could be simplified to:
But your way works too! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like it! Will change. |
||
|
||
// At this point, we've tried all of the dialers, and they've all either | ||
// succeeded or failed. | ||
|
||
|
@@ -114,6 +138,7 @@ func (fcd *fastConnectDialer) connectAll(dialers []ProxyDialer) { | |
nextOpts := fcd.opts.Clone() | ||
nextOpts.Dialers = fcd.connected.proxyDialers() | ||
fcd.next(nextOpts, fcd) | ||
|
||
} | ||
|
||
func (fcd *fastConnectDialer) parallelDial(dialers []ProxyDialer) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are we assuming that there will only ever be one place
New
is called? (ignoring tests)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's only called when we get new configs