An example of a round robin algorithm in Go that can be used for load balancing.
Last active
March 4, 2024 21:12
-
-
Save amanbolat/ec95400dfcd3aaad2bdb2f9e4a25d469 to your computer and use it in GitHub Desktop.
Load balancing in golang
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
package main | |
import ( | |
"fmt" | |
"sync/atomic" | |
) | |
func main() { | |
type node struct {} | |
var nodes []node | |
var robinCounter uint64 | |
var chosenNode *node | |
n := uint64(len(nodes)) | |
for i := uint64(0); i < n; i++ { | |
r := atomic.AddUint64(&robinCounter, 1) | |
chosenNode = &nodes[r%n] | |
} | |
fmt.Println(chosenNode) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment