-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathload_balancer.h
More file actions
55 lines (44 loc) · 1.73 KB
/
load_balancer.h
File metadata and controls
55 lines (44 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#pragma once
#include "config.h"
#include "timer.h" // rpp::TimePoint
namespace rpp
{
////////////////////////////////////////////////////////////////////////////////
/**
* Helper utility for UDP sockets to perform load balancing on send operations
*/
class RPPAPI load_balancer
{
uint32 maxBytesPerSec;
uint32 nanosBetweenBytes;
rpp::TimePoint lastSendTime;
int64 nextSendTimeout = 0;
public:
explicit load_balancer(uint32 maxBytesPerSec) noexcept
{
set_max_bytes_per_sec(maxBytesPerSec);
}
/** @returns Current bytes per sec limit */
uint32 get_max_bytes_per_sec() const noexcept { return maxBytesPerSec; }
/** @brief Sets the new max bytes per second limit */
void set_max_bytes_per_sec(uint32 maxBytesPerSec) noexcept;
/** @returns Average nanoseconds between bytes */
uint32 avg_nanos_between_bytes() const noexcept { return nanosBetweenBytes; }
/**
* @returns TRUE if the specified number of bytes can be sent
* @warning The caller MUST MANUALLY call notify_sent() after sending the bytes
*/
bool can_send() const noexcept;
bool can_send(const rpp::TimePoint& now) const noexcept;
/**
* @brief Blocks until the specified number of bytes can be sent
* and notifies the load balancer that we sent the bytes
*/
void wait_to_send(uint32 bytesToSend) noexcept;
/**
* @brief Notify that we sent this # of bytes
*/
void notify_sent(const rpp::TimePoint& now, uint32 bytesToSend) noexcept;
};
////////////////////////////////////////////////////////////////////////////////
}