<commit_before>#pragma once

#include "HitQueue.h"
#include "Tick.h"

#include <chrono>

struct HttpResult
{
	enum class Code {
		Ok = 200,
		TooManyRequests = 429
	};

	HttpResult(Code code) : code(code)
	{}
	
	const Code code;
};

class Limiter
{
public:
	Limiter(int maxRequestCount, int timeSlotsInSecond, std::chrono::microseconds tickTime)
		: hitQueue_(timeSlotsInSecond)
		, tick_(tickTime, [this] { OnTimeSlotBoundary(); })	// note that callbacks may start coming right after this
		, maxRequestCount_(maxRequestCount)
	{
	}

	~Limiter()
	{
		tick_.Deactivate();	// lifecycle issues are still possible, see Tick::Loop()
	}

	HttpResult::Code ValidateRequest()
	{
		if (hitQueue_.ActiveSum() >= maxRequestCount_)
		{
			return HttpResult::Code::TooManyRequests;
		}
		hitQueue_.AddHit();
		return HttpResult::Code::Ok;
	}

private:
	void OnTimeSlotBoundary()
	{
		hitQueue_.NextTimeSlot();
	}

	HitQueue hitQueue_;
	Tick tick_;
	const int maxRequestCount_;
};
<commit_msg>Limiter class - add maxRPS getter<commit_after>#pragma once

#include "HitQueue.h"
#include "Tick.h"

#include <chrono>

struct HttpResult
{
	enum class Code {
		Ok = 200,
		TooManyRequests = 429
	};

	HttpResult(Code code) : code(code)
	{}
	
	const Code code;
};

class Limiter
{
public:
	Limiter(int maxRequestCount, int timeSlotsInSecond, std::chrono::microseconds tickTime)
		: hitQueue_(timeSlotsInSecond)
		, tick_(tickTime, [this] { OnTimeSlotBoundary(); })	// note that callbacks may start coming right after this
		, maxRequestCount_(maxRequestCount)
	{
	}

	~Limiter()
	{
		tick_.Deactivate();	// lifecycle issues are still possible, see Tick::Loop()
	}

	HttpResult::Code ValidateRequest()
	{
		if (hitQueue_.ActiveSum() >= maxRequestCount_)
		{
			return HttpResult::Code::TooManyRequests;
		}
		hitQueue_.AddHit();
		return HttpResult::Code::Ok;
	}

	int maxRPS() const { return maxRequestCount_; }

private:
	void OnTimeSlotBoundary()
	{
		hitQueue_.NextTimeSlot();
	}

	HitQueue hitQueue_;
	Tick tick_;
	const int maxRequestCount_;
};

{'fraction_non_alphanumeric': 0.12315270935960591, 'fraction_numerical': 0.005911330049261084, 'mean_word_length': 4.641666666666667, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '29853', 'n_tokens_mistral': 756, 'n_tokens_neox': 706, 'n_words': 161}