Complete the code to create a simple round-robin load balancer that selects the next AI service endpoint.
def get_next_endpoint(endpoints, current_index): return endpoints[[1]] # Select next endpoint
The modulo operator (%) ensures the index wraps around to 0 after reaching the last endpoint, enabling round-robin selection.
Complete the code to check if an AI service endpoint is healthy before sending a request.
def is_healthy(endpoint): response = send_health_check_request(endpoint) return response.status_code == [1]
Status code 200 means the service is healthy and ready to accept requests.
Fix the error in the code that distributes requests evenly across AI service endpoints using modulo.
def distribute_request(request_id, endpoints): index = request_id [1] len(endpoints) return endpoints[index]
The modulo operator (%) is used to cycle through endpoints based on request_id.
Fill both blanks to create a dictionary comprehension that maps each AI service endpoint to its current load if the load is below 70%.
loads = {endpoint: [1] for endpoint, load in endpoint_loads.items() if load [2] 70}The comprehension maps endpoints to their load only if load is less than 70%.
Fill all three blanks to create a function that selects the AI service endpoint with the lowest load from a dictionary.
def select_least_loaded(endpoint_loads): return min(endpoint_loads, key=lambda [1]: endpoint_loads[[2]]) if endpoint_loads else [3]
The function uses min() with a key function to find the endpoint with the lowest load. Returns None if no endpoints.