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

Attempt to connect to leader via 10 servers simultaneously #292

Merged
merged 2 commits into from
Mar 21, 2024

Conversation

cnnrznn
Copy link
Contributor

@cnnrznn cnnrznn commented Mar 17, 2024

This PR implements the solution discussed in my first PR

  1. Spawn connection attempts in goroutines
  2. Goroutines
    a. Grab semaphore, defer release
    b. Check if a connection has been found, exit
    c. Execute original logic, write protocol output to channel

Resolves #134


ctx, cancel := context.WithTimeout(ctx, c.config.AttemptTimeout)
defer cancel()
sem.Acquire(ctx, 1)
Copy link
Member

Choose a reason for hiding this comment

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

This can return an error. We should handle that case, and if you release too many times, it will cause a panic. We should be careful in that scenario.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated, following the example

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There is still the case where the semaphore can be acquired after cancellation. To sacrifice performance but improve readability we could also eliminate (new) lines 156-158

Copy link
Member

Choose a reason for hiding this comment

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

We do exactly the same in juju 😉

Copy link
Contributor

@cole-miller cole-miller left a comment

Choose a reason for hiding this comment

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

Thanks again for working on this and sorry for the delay! Only minor comments.

}(server, protocolChan)
}

// Read from protocol chan, cancel context\
Copy link
Contributor

Choose a reason for hiding this comment

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

typo

Comment on lines 214 to 224
var protocol *Protocol
for pc := range protocolChan {
if protocol != nil {
pc.Close()
continue
}
log(logging.Debug, "connected")
protocol = pc
cancel()
}

if protocol != nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

I feel this would be a bit clearer as follows:

protocol, ok := <-protocolChan
if !ok {
    return nil, ErrNoAvailableLeader
}
cancel()
for extra := range protocolChan {
    extra.Close()
}
return protocol, nil

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

@cnnrznn
Copy link
Contributor Author

cnnrznn commented Mar 21, 2024

@cole-miller would you like me to squash these commits to keep the history clean?

@cole-miller
Copy link
Contributor

I don't think it's necessary!

@cole-miller cole-miller self-requested a review March 21, 2024 15:35
@cole-miller cole-miller merged commit 1940345 into canonical:master Mar 21, 2024
37 checks passed
@cnnrznn
Copy link
Contributor Author

cnnrznn commented Mar 21, 2024

🚀 woohoo! 🚀

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.

Parallellize connectAttemptAll
3 participants