|
412 Project 3
|
The LoadBalancer class manages a set of WebServers and dispatches incoming requests to them. More...
#include <LoadBalancer.h>
Public Member Functions | |
| LoadBalancer (int numServers, int numRequests, int maxServers, int requestThreshold, int maxIdle) | |
| Constructs a LoadBalancer with the specified number of servers and requests. | |
| ~LoadBalancer () | |
| Destructor for the LoadBalancer class, deletes all dynamically allocated servers. | |
| void | create_requests () |
| Creates a new request with a 10% chance on each call and adds it to the request queue. | |
| void | dispatch_requests () |
| Dispatches requests from the queue to available servers. | |
| void | process_requests (int time) |
| Processes requests on all servers for a given time unit. | |
| void | adjust_servers () |
| Adjusts the number of servers based on the size of the request queue and server idle times. | |
Public Attributes | |
| int | max_servers |
| Maximum number of servers allowed. | |
| int | request_threshold |
| Threshold of request queue size to trigger server addition. | |
| int | max_idle |
| Maximum allowed idle time before a server is removed. | |
| int | curr_idle |
| Current count of idle servers. | |
| std::queue< Request > | requests |
| Queue of incoming requests to be processed. | |
| std::vector< WebServer * > | servers |
| List of active servers. | |
The LoadBalancer class manages a set of WebServers and dispatches incoming requests to them.
This class handles the creation of new requests, dispatches them to available servers, processes the requests, and dynamically adjusts the number of servers based on load and idle time.
| LoadBalancer::LoadBalancer | ( | int | numServers, |
| int | numRequests, | ||
| int | maxServers, | ||
| int | requestThreshold, | ||
| int | maxIdle ) |
Constructs a LoadBalancer with the specified number of servers and requests.
| numServers | Initial number of servers. |
| numRequests | Initial number of requests to populate the request queue. |
| maxServers | Maximum number of servers allowed to be created. |
| requestThreshold | The threshold of requests to decide when to add more servers. |
| maxIdle | The maximum idle time allowed for servers before they are deallocated. |
| void LoadBalancer::adjust_servers | ( | ) |
Adjusts the number of servers based on the size of the request queue and server idle times.
| void LoadBalancer::dispatch_requests | ( | ) |
Dispatches requests from the queue to available servers.
If a server is not busy and there are requests in the queue, the server is assigned the top request.
| void LoadBalancer::process_requests | ( | int | time | ) |
Processes requests on all servers for a given time unit.
| time | The amount of time to process each request. |