update load balancer directory name
This commit is contained in:
19
implementations/python/load_balancing_algorithms/ip_hash.py
Normal file
19
implementations/python/load_balancing_algorithms/ip_hash.py
Normal file
@@ -0,0 +1,19 @@
|
||||
import hashlib
|
||||
|
||||
class IPHash():
|
||||
def __init__(self, servers):
|
||||
self.servers = servers
|
||||
|
||||
def get_next_server(self, client_ip):
|
||||
hash_value = hashlib.md5(client_ip.encode()).hexdigest()
|
||||
index = int(hash_value, 16) % len(self.servers)
|
||||
return self.servers[index]
|
||||
|
||||
# Example usage
|
||||
servers = ["Server1", "Server2", "Server3"]
|
||||
load_balancer = IPHash(servers)
|
||||
|
||||
client_ips = ["192.168.0.1", "192.168.0.2", "192.168.0.3", "192.168.0.4"]
|
||||
for ip in client_ips:
|
||||
server = load_balancer.get_next_server(ip)
|
||||
print(f"Client {ip} -> {server}")
|
||||
Reference in New Issue
Block a user