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

Stop old dialers when we get a new proxy config #1445

Merged
merged 4 commits into from
Nov 22, 2024

Conversation

myleshorton
Copy link
Contributor

@garmr-ulfr identified a bug in our dialer whereby we keep dialing old dialers when we get a new proxy config. This stops 'em.

Comment on lines 116 to 132
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
}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be simplified to:

for {
	// Loop until we're connected
	if len(fcd.connected.dialers) < 2 {
		fcd.parallelDial(dialers)
	} else {
		break
	}
	select {
	case <-fcd.stopCh:
		log.Debug("Stopping parallel dialing")
		return
	case time.After(time.Duration(rand.Intn(4000)) * time.Millisecond):
	}
}

But your way works too!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like it! Will change.

// New creates a new dialer that first tries to connect as quickly as possilbe while also
// optimizing for the fastest dialer.
func New(opts *Options) Dialer {
return NewTwoPhaseDialer(opts, func(opts *Options, existing Dialer) Dialer {
if currentDialer.Load() != nil {
Copy link
Contributor

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)

Copy link
Contributor Author

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

// 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{}{}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would make stopCh buffered or just close it so this doesn't block until outerLoop tries to read from it again.

Copy link
Contributor

@garmr-ulfr garmr-ulfr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Comment on lines 121 to 131
time.Sleep(time.Duration(rand.Intn(4000)) * time.Millisecond)
} else {
break
}
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
}

}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not big deal since the goroutine running connectAll would only hang around for a few seconds, at most, but, if you add another case and use time.After instead of time.Sleep here, the goroutine will exit as soon as it's canceled and be cleaned up. Just FYI.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah good point -- fixed!

@myleshorton myleshorton merged commit 00339e0 into main Nov 22, 2024
1 check passed
@myleshorton myleshorton deleted the myles/stop-old-dialer branch November 22, 2024 16:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants