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

Stylistic improvements #6

Merged
merged 6 commits into from
Sep 14, 2022
Merged

Conversation

jacobvosmaer
Copy link
Contributor

  • use defer to unlock mutex
  • use redis.Strings and redis.String helpers more
  • small other simplifications

This function was called only once and it is a one-liner.
Writing `sentinels[i] = v` is shorter than
`sentinels = append(sentinels, v)`.

Also, why we are here, change `value, err` returns to idiomatic
`nil, err`.
return pool
}
s.mu.Unlock()
newPool := s.newPool(addr)
s.mu.Lock()
p, ok := s.pools[addr]
if ok {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This p is now called pool like the identical check earlier in the function.

@@ -200,10 +191,8 @@ func (s *Sentinel) newPool(addr string) *redis.Pool {
// close connection pool to Sentinel.
// Lock must be hold by caller.
func (s *Sentinel) close() {
if s.pools != 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.

We don't need this nil check because len(nil) == 0; the for loop will do nothing in that case.

if ok {
s.mu.Unlock()
defer s.mu.Unlock()
if pool, ok := s.pools[addr]; ok {
return pool
}
s.mu.Unlock()
Copy link
Owner

Choose a reason for hiding this comment

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

Since there is a defer we don't need this Unlock anymore?

I am not sure, but I think the initial idea of lock usage in this func could be avoid locking while calling s.newPool(addr) (which can contain user supplied function to get Pool). This pr changes behaviour a bit.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The original behaviour is still there, isn't it?

s.mu.Unlock()
newPool := s.newPool(addr)
s.mu.Lock()

Copy link
Owner

Choose a reason for hiding this comment

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

Oh, I see, was confused by defer and thought that the entire function meant to be under lock now, never seen this pattern before - interesting approach

@FZambia
Copy link
Owner

FZambia commented Sep 13, 2022

Thanks, looks awesome! Left one note where I think it's better to keep previous style if there is no other reason for a change.

@FZambia FZambia merged commit 0c03d24 into FZambia:master Sep 14, 2022
@jacobvosmaer
Copy link
Contributor Author

Thanks!

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