-
Notifications
You must be signed in to change notification settings - Fork 0
/
weightedRoundRobin.go
67 lines (48 loc) · 1.43 KB
/
weightedRoundRobin.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package loadbalancingalgo
import (
"fmt"
"github.com/rampa2510/system-design/commons"
)
type WeightedRoundRobinServerNode struct {
weight int
connections int
}
func getSystemState(serverNodes []WeightedRoundRobinServerNode) {
noOfServers := len(serverNodes)
for i := 0; i < noOfServers; i++ {
server := serverNodes[i]
fmt.Printf("Server at index %d has weight %d and live connections %d\n", i, server.weight, server.connections)
}
}
func serveRequests(serverNodes *[]WeightedRoundRobinServerNode) {
noOfServers := len(*serverNodes)
for i := 0; i < noOfServers; i++ {
if (*serverNodes)[i].weight != (*serverNodes)[i].connections {
(*serverNodes)[i].connections++
return
}
}
}
func WeightedRoundRobin() {
var noOfServers int
commons.AcceptInput("Enter Number of servers (0 to exit): ", &noOfServers)
if noOfServers == 0 {
fmt.Println("Exiting Weighted Round Robin. Goodbye!")
return
}
serverNodes := make([]WeightedRoundRobinServerNode, noOfServers)
var weight int
for i := 0; i < noOfServers; i++ {
inputMsg := fmt.Sprintf("Enter weight for server at idx %d: ", i+1)
commons.AcceptInput(inputMsg, &weight)
serverNodes[i].weight = weight
}
var noOfRequests int
commons.AcceptInput("Enter no of requests (o to exit): ", &noOfRequests)
getSystemState(serverNodes)
fmt.Println("Running algo..........")
for i := 0; i < noOfRequests; i++ {
serveRequests(&serverNodes)
}
getSystemState(serverNodes)
}