#ifndef MS_BACKOFF_TIMER_HANDLE_HPP
#define MS_BACKOFF_TIMER_HANDLE_HPP
#include "common.hpp"
#include "handles/BackoffTimerHandleInterface.hpp"
#include "handles/TimerHandle.hpp"
#include "handles/TimerHandleInterface.hpp"
class Shared;
class BackoffTimerHandle : public BackoffTimerHandleInterface, public TimerHandleInterface::Listener
{
friend class Shared;
private:
explicit BackoffTimerHandle(BackoffTimerHandleOptions options);
public:
BackoffTimerHandle& operator=(const BackoffTimerHandle&) = delete;
BackoffTimerHandle(const BackoffTimerHandle&) = delete;
~BackoffTimerHandle() override;
public:
void Start() override;
void Stop() override;
void SetBaseTimeoutMs(uint64_t baseTimeoutMs) override;
bool IsRunning() const override
{
return this->running;
}
const std::string GetLabel() const override
{
return this->label;
}
std::optional<size_t> GetMaxRestarts() const override
{
return this->maxRestarts;
}
size_t GetExpirationCount() const override
{
return this->expirationCount;
}
private:
uint64_t ComputeNextTimeoutMs() const;
public:
void OnTimer(TimerHandleInterface* timer) override;
private:
BackoffTimerHandleInterface::Listener* listener{ nullptr };
const std::string label;
uint64_t baseTimeoutMs;
BackoffAlgorithm backoffAlgorithm;
std::optional<uint64_t> maxBackoffTimeoutMs;
std::optional<size_t> maxRestarts;
TimerHandle* timer{ nullptr };
bool running{ false };
size_t expirationCount{ 0 };
};
#endif