add load balancer implementation
This commit is contained in:
19
implementations/python/load balancer algorithms/ip_hash.py
Normal file
19
implementations/python/load balancer 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}")
|
||||
@@ -0,0 +1,28 @@
|
||||
import random
|
||||
|
||||
class LeastConnections:
|
||||
def __init__(self, servers):
|
||||
self.servers = {server: 0 for server in servers}
|
||||
|
||||
def get_next_server(self):
|
||||
# Find the minimum number of connections
|
||||
min_connections = min(self.servers.values())
|
||||
# Get all servers with the minimum number of connections
|
||||
least_loaded_servers = [server for server, connections in self.servers.items() if connections == min_connections]
|
||||
# Select a random server from the least loaded servers
|
||||
selected_server = random.choice(least_loaded_servers)
|
||||
self.servers[selected_server] += 1
|
||||
return selected_server
|
||||
|
||||
def release_connection(self, server):
|
||||
if self.servers[server] > 0:
|
||||
self.servers[server] -= 1
|
||||
|
||||
# Example usage
|
||||
servers = ["Server1", "Server2", "Server3"]
|
||||
load_balancer = LeastConnections(servers)
|
||||
|
||||
for i in range(6):
|
||||
server = load_balancer.get_next_server()
|
||||
print(f"Request {i + 1} -> {server}")
|
||||
load_balancer.release_connection(server)
|
||||
@@ -0,0 +1,34 @@
|
||||
import time
|
||||
import random
|
||||
|
||||
class LeastResponseTime:
|
||||
def __init__(self, servers):
|
||||
self.servers = servers
|
||||
self.response_times = [0] * len(servers)
|
||||
|
||||
def get_next_server(self):
|
||||
min_response_time = min(self.response_times)
|
||||
min_index = self.response_times.index(min_response_time)
|
||||
return self.servers[min_index]
|
||||
|
||||
def update_response_time(self, server, response_time):
|
||||
index = self.servers.index(server)
|
||||
self.response_times[index] = response_time
|
||||
|
||||
# Simulated server response time function
|
||||
def simulate_response_time():
|
||||
# Simulating response time with random delay
|
||||
delay = random.uniform(0.1, 1.0)
|
||||
time.sleep(delay)
|
||||
return delay
|
||||
|
||||
# Example usage
|
||||
servers = ["Server1", "Server2", "Server3"]
|
||||
load_balancer = LeastResponseTime(servers)
|
||||
|
||||
for i in range(6):
|
||||
server = load_balancer.get_next_server()
|
||||
print(f"Request {i + 1} -> {server}")
|
||||
response_time = simulate_response_time()
|
||||
load_balancer.update_response_time(server, response_time)
|
||||
print(f"Response Time: {response_time:.2f}s")
|
||||
@@ -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}")
|
||||
@@ -0,0 +1,25 @@
|
||||
class WeightedRoundRobin:
|
||||
def __init__(self, servers, weights):
|
||||
self.servers = servers
|
||||
self.weights = weights
|
||||
self.current_index = -1
|
||||
self.current_weight = 0
|
||||
|
||||
def get_next_server(self):
|
||||
while True:
|
||||
self.current_index = (self.current_index + 1) % len(self.servers)
|
||||
if self.current_index == 0:
|
||||
self.current_weight -= 1
|
||||
if self.current_weight <= 0:
|
||||
self.current_weight = max(self.weights)
|
||||
if self.weights[self.current_index] >= self.current_weight:
|
||||
return self.servers[self.current_index]
|
||||
|
||||
# Example usage
|
||||
servers = ["Server1", "Server2", "Server3"]
|
||||
weights = [5, 1, 1]
|
||||
load_balancer = WeightedRoundRobin(servers, weights)
|
||||
|
||||
for i in range(7):
|
||||
server = load_balancer.get_next_server()
|
||||
print(f"Request {i + 1} -> {server}")
|
||||
Reference in New Issue
Block a user