add load balancer implementation

This commit is contained in:
Ashish Pratap Singh
2024-06-01 22:55:35 -07:00
parent e274b977fb
commit 4043fe83f0
5 changed files with 122 additions and 0 deletions

View File

@@ -0,0 +1,16 @@
class RoundRobin:
def __init__(self, servers):
self.servers = servers
self.current_index = -1
def get_next_server(self):
self.current_index = (self.current_index + 1) % len(self.servers)
return self.servers[self.current_index]
# Example usage
servers = ["Server1", "Server2", "Server3"]
load_balancer = RoundRobin(servers)
for i in range(6):
server = load_balancer.get_next_server()
print(f"Request {i + 1} -> {server}")