Complete the code to select the next server using round-robin load balancing.
next_server = servers[[1] % len(servers)]
The current_index variable holds the last used server index. Increment it by 1 and use modulo the number of servers to select the next server in round-robin.
Complete the code to route requests based on URL path prefix.
if request.path.startswith([1]): route_to = 'serviceA' else: route_to = 'serviceB'
The routing decision is based on the URL path prefix. '/api/v1/' is a common API prefix to route to serviceA.
Fix the error in the load balancer health check function.
def is_healthy(server): response = ping(server) return response.status_code == [1]
A healthy server responds with status code 200 (OK). Other codes indicate errors or redirects.
Fill both blanks to implement weighted load balancing selection.
total_weight = sum(server['weight'] for server in servers) random_pick = random.uniform(0, [1]) current = 0 for server in servers: current += server['weight'] if current >= [2]: selected = server break
The random_pick is a random number between 0 and total_weight. The loop selects the server where cumulative weight exceeds random_pick.
Fill all three blanks to complete the request routing with fallback.
try: response = [1](request) except [2]: response = [3](request)
The code tries to handle the request with the primary service. If a TimeoutError occurs, it falls back to the backup service.