412 Project 3
Loading...
Searching...
No Matches
LoadBalancer Class Reference

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< Requestrequests
 Queue of incoming requests to be processed.
 
std::vector< WebServer * > servers
 List of active servers.
 

Detailed Description

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.

Constructor & Destructor Documentation

◆ LoadBalancer()

LoadBalancer::LoadBalancer ( int numServers,
int numRequests,
int maxServers,
int requestThreshold,
int maxIdle )

Constructs a LoadBalancer with the specified number of servers and requests.

Parameters
numServersInitial number of servers.
numRequestsInitial number of requests to populate the request queue.
maxServersMaximum number of servers allowed to be created.
requestThresholdThe threshold of requests to decide when to add more servers.
maxIdleThe maximum idle time allowed for servers before they are deallocated.

Member Function Documentation

◆ adjust_servers()

void LoadBalancer::adjust_servers ( )

Adjusts the number of servers based on the size of the request queue and server idle times.

  • Adds new servers if the request queue size exceeds the number of servers, up to the maximum server limit.
  • Deallocates (deletes) servers that have been idle for longer than the allowed maximum idle time.

◆ dispatch_requests()

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.

◆ process_requests()

void LoadBalancer::process_requests ( int time)

Processes requests on all servers for a given time unit.

Parameters
timeThe amount of time to process each request.

The documentation for this class was generated from the following files: