Created
November 9, 2019 07:40
-
-
Save kasvith/7e3efccd3e26615654106265b1ce43f4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// GetNextPeer returns next active peer to take a connection | |
func (s *ServerPool) GetNextPeer() *Backend { | |
// loop entire backends to find out an Alive backend | |
next := s.NextIndex() | |
l := len(s.backends) + next // start from next and move a full cycle | |
for i := next; i < l; i++ { | |
idx := i % len(s.backends) // take an index by modding | |
if s.backends[idx].IsAlive() { // if we have an alive backend, use it and store if its not the original one | |
if i != next { | |
atomic.StoreUint64(&s.current, uint64(idx)) | |
} | |
return s.backends[idx] | |
} | |
} | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment