library/src/network/httpd/BP-HTTPdDataRateLimits.cc
// Adjust rate for websocket based on new add data.
Adjust rate for websocket based on new add data.
Adjust rate for websocket based on new add data .
BP_ERROR_T BP_HTTPdRateLimitAdjustRatesForWebsocket
(
	P_BP_HTTPD_WEBSOCKET websocket,
	size_t               send_rate_bytes_add,
	size_t               recv_rate_bytes_add,
	size_t               send_rate_frames_add,
	size_t               recv_rate_frames_add
)
{
	if(!websocket)
		return ERR_FAILURE;
	if(!websocket->hreg)
		return ERR_FAILURE;
	BP_HASH_TABLE_ENTER_CRITICAL_SECTION(websocket->hreg);
	P_BP_HTTPD_DATA_RATE_LIMITER rate_limits = &websocket->connection_policy.socket_rate_limits;
	if(!rate_limits->initial_timestamp)
	{
		if(!BP_HTTPdRateLimitInitTimers(rate_limits))
		{
			BP_HASH_TABLE_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC(websocket->hreg, ERR_FAILURE);
		}
	}
	if(send_rate_bytes_add)
	{
		rate_limits->total_websocket_send_bytes += send_rate_bytes_add;
	}
	if(recv_rate_bytes_add)
	{
		rate_limits->total_websocket_recv_bytes += recv_rate_bytes_add;
	}
	if(send_rate_frames_add)
	{
		rate_limits->total_websocket_send_frames += send_rate_frames_add;
	}
	if(recv_rate_frames_add)
	{
		rate_limits->total_websocket_recv_frames += recv_rate_frames_add;
	}
	BP_HTTPD_DATA_RATE_LIMITER rate_limits_preserved;
	BP_ZERO_STACK(rate_limits_preserved);
	memcpy(&rate_limits_preserved, rate_limits, sizeof(BP_HTTPD_DATA_RATE_LIMITER));
	if(!BP_HTTPdRateLimitUpdateTimers(rate_limits))
	{
		BP_HASH_TABLE_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC(websocket->hreg, ERR_FAILURE);
	}
	if(rate_limits->difference_in_seconds_between_initial_and_current_timestamp)
	{
		if(!rate_limits->avg_websocket_recv_bytes_per_second_since_initial_timestamp)
		{
			rate_limits->avg_websocket_recv_bytes_per_second_since_initial_timestamp = recv_rate_bytes_add;
		}
		rate_limits->avg_websocket_recv_bytes_per_second_since_initial_timestamp =\
				rate_limits->total_websocket_recv_bytes / rate_limits->difference_in_seconds_between_initial_and_current_timestamp;
	}
	if(rate_limits->difference_in_seconds_between_current_and_last_timestamp)
	{
		if(!rate_limits->curr_websocket_recv_bytes_per_second)
		{
			rate_limits->curr_websocket_recv_bytes_per_second          = recv_rate_bytes_add;
			rate_limits->curr_websocket_recv_bytes_between_measurement = recv_rate_bytes_add;
		}
		rate_limits->curr_websocket_recv_bytes_per_second =\
				rate_limits->curr_websocket_recv_bytes_between_measurement \
				/ rate_limits->difference_in_seconds_between_current_and_last_timestamp;
		rate_limits->curr_websocket_recv_bytes_between_measurement = 0;
	}
	else
	{
		rate_limits->curr_websocket_recv_bytes_between_measurement += recv_rate_bytes_add;
		rate_limits->curr_websocket_recv_bytes_per_second          += recv_rate_bytes_add;
	}
	BP_HTTPdRateLimitCheckRecvExceedsLimit(&websocket->connection_policy.socket_rate_limits);
	printf("\n Adjusting rates for websocket?!:                              %p", websocket->connection_policy.socket_rate_limits);
	printf("\n recv_rate_bytes_add:                                          %u", recv_rate_bytes_add);
	printf("\n curr_websocket_recv_bytes_per_second?:                        %u", rate_limits->curr_websocket_recv_bytes_per_second);
	printf("\n total_websocket_recv_bytes?:                                  %u", rate_limits->total_websocket_recv_bytes);
	printf("\n avg_websocket_recv_bytes_per_second_since_initial_timestamp:  %u", rate_limits->avg_websocket_recv_bytes_per_second_since_initial_timestamp);
	printf("\n curr_websocket_recv_frames_per_second:                        %u", rate_limits->curr_websocket_recv_frames_per_second);
	printf("\n avg_websocket_recv_frames_per_second_since_initial_timestamp: %u", rate_limits->avg_websocket_recv_frames_per_second_since_initial_timestamp);
	printf("\n");
	printf("\n");
	BP_HASH_TABLE_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC(websocket->hreg, ERR_SUCCESS);
}
BP_ERROR_T BP_HTTPdRateLimitAdjustRatesForWebsocket ( P_BP_HTTPD_WEBSOCKET websocket , size_t send_rate_bytes_add , size_t recv_rate_bytes_add , size_t send_rate_frames_add , size_t recv_rate_frames_add ) { if ( ! websocket ) return ERR_FAILURE ; if ( ! websocket -> hreg ) return ERR_FAILURE ; BP_HASH_TABLE_ENTER_CRITICAL_SECTION ( websocket -> hreg ) ; P_BP_HTTPD_DATA_RATE_LIMITER rate_limits = & websocket -> connection_policy . socket_rate_limits ; if ( ! rate_limits -> initial_timestamp ) { if ( ! BP_HTTPdRateLimitInitTimers ( rate_limits ) ) { BP_HASH_TABLE_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC ( websocket -> hreg , ERR_FAILURE ) ; } } if ( send_rate_bytes_add ) { rate_limits -> total_websocket_send_bytes += send_rate_bytes_add ; } if ( recv_rate_bytes_add ) { rate_limits -> total_websocket_recv_bytes += recv_rate_bytes_add ; } if ( send_rate_frames_add ) { rate_limits -> total_websocket_send_frames += send_rate_frames_add ; } if ( recv_rate_frames_add ) { rate_limits -> total_websocket_recv_frames += recv_rate_frames_add ; } BP_HTTPD_DATA_RATE_LIMITER rate_limits_preserved ; BP_ZERO_STACK ( rate_limits_preserved ) ; memcpy ( & rate_limits_preserved , rate_limits , sizeof ( BP_HTTPD_DATA_RATE_LIMITER ) ) ; if ( ! BP_HTTPdRateLimitUpdateTimers ( rate_limits ) ) { BP_HASH_TABLE_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC ( websocket -> hreg , ERR_FAILURE ) ; } if ( rate_limits -> difference_in_seconds_between_initial_and_current_timestamp ) { if ( ! rate_limits -> avg_websocket_recv_bytes_per_second_since_initial_timestamp ) { rate_limits -> avg_websocket_recv_bytes_per_second_since_initial_timestamp = recv_rate_bytes_add ; } rate_limits -> avg_websocket_recv_bytes_per_second_since_initial_timestamp = rate_limits -> total_websocket_recv_bytes / rate_limits -> difference_in_seconds_between_initial_and_current_timestamp ; } if ( rate_limits -> difference_in_seconds_between_current_and_last_timestamp ) { if ( ! rate_limits -> curr_websocket_recv_bytes_per_second ) { rate_limits -> curr_websocket_recv_bytes_per_second = recv_rate_bytes_add ; rate_limits -> curr_websocket_recv_bytes_between_measurement = recv_rate_bytes_add ; } rate_limits -> curr_websocket_recv_bytes_per_second = rate_limits -> curr_websocket_recv_bytes_between_measurement / rate_limits -> difference_in_seconds_between_current_and_last_timestamp ; rate_limits -> curr_websocket_recv_bytes_between_measurement = 0 ; } else { rate_limits -> curr_websocket_recv_bytes_between_measurement += recv_rate_bytes_add ; rate_limits -> curr_websocket_recv_bytes_per_second += recv_rate_bytes_add ; } BP_HTTPdRateLimitCheckRecvExceedsLimit ( & websocket -> connection_policy . socket_rate_limits ) ; printf ( " \n " , websocket -> connection_policy . socket_rate_limits ) ; printf ( " \n " , recv_rate_bytes_add ) ; printf ( " \n " , rate_limits -> curr_websocket_recv_bytes_per_second ) ; printf ( " \n " , rate_limits -> total_websocket_recv_bytes ) ; printf ( " \n " , rate_limits -> avg_websocket_recv_bytes_per_second_since_initial_timestamp ) ; printf ( " \n " , rate_limits -> curr_websocket_recv_frames_per_second ) ; printf ( " \n " , rate_limits -> avg_websocket_recv_frames_per_second_since_initial_timestamp ) ; printf ( " \n " ) ; printf ( " \n " ) ; BP_HASH_TABLE_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC ( websocket -> hreg , ERR_SUCCESS ) ; }
Adjust rate for websocket based on new add data.
Adjust rate for websocket based on new add data .
// ensure we have a websocket and a registry // enter critical section // --- dereference rate limits from websocket ----- // create local reference to rate limits // initialize the timer if we have no initial timestamp // initialize the timers // --- adjust send/recv bytes ----------------------- // add bytes // add recv bytes // --- adjust send/recv frames --------------------- // add to the total sent frames // add to the total recv frames // --- preserve rate limits before calculation ---- // create and preserve rate limits // copy in the rate limiter to preserve values // --- update timers after adjustment ------------ // update timers // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%% Adjust Recv Bytes %%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // check if we have a difference in seconds, if there is no difference, simply increase // the rate linearally.  If we do have a difference, divide by rate (disgard remainders). // if the initial timestamp isn't set, set it here // calculate the average since initial // adjust bytes per second // set curr websocket recv bytes per second // create the rate from previous and bytes // reset the preserved measurement // perform adjustment // increase the bytes added // rate_limits->curr_websocket_recv_bytes_per_second          += recv_rate_bytes_add; // check recv exception // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%% Adjust Send Bytes %%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%% Adjust Recv Frames %%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%% Adjust Send Frames %%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // leave critical section and return