Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mumble-voip/mumble.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobert Adam <dev@robert-adam.de>2020-03-21 16:10:28 +0300
committerRobert Adam <dev@robert-adam.de>2020-03-21 16:17:41 +0300
commit029b9802f97bcd8e6bc5e65274645f53c6b29bf3 (patch)
treec0cb7ff661c62225a0ebb88ee3b9ec98eb66b765 /src/murmur/ServerUser.h
parentf8ee53688353c8f5e1650504a961ee582ac16668 (diff)
src/murmur/ServerUser: Improve implementation of the leaky bucket algorithm
The most important change is to use a timer that is based on a monotonic clock in order to avoid time jumps and this inconsistent measurements of time intervals (see https://github.com/mumble-voip/mumble/issues/3985#issuecomment-601754212). Furthermore the code-style has been adapted (m_ prefix instead of usage of this->) and documentation has been added / extended.
Diffstat (limited to 'src/murmur/ServerUser.h')
-rw-r--r--src/murmur/ServerUser.h26
1 files changed, 20 insertions, 6 deletions
diff --git a/src/murmur/ServerUser.h b/src/murmur/ServerUser.h
index 37b7776fb..21283324f 100644
--- a/src/murmur/ServerUser.h
+++ b/src/murmur/ServerUser.h
@@ -18,7 +18,7 @@
#include "HostAddress.h"
#include <QtCore/QStringList>
-#include <QtCore/QDateTime>
+#include <QtCore/QElapsedTimer>
#ifdef Q_OS_WIN
# include <winsock2.h>
@@ -62,15 +62,29 @@ struct WhisperTarget {
class Server;
-// Simple algorithm for rate limiting
+/// A simple implementation for rate-limiting.
+/// See https://en.wikipedia.org/wiki/Leaky_bucket
class LeakyBucket {
private:
- unsigned int tokensPerSec, maxTokens;
- long currentTokens;
- QDateTime lastUpdate;
+ /// The amount of tokens that are drained per second.
+ /// (The sze of the whole in the bucket)
+ unsigned int m_tokensPerSec;
+ /// The maximum amount of tokens that may be encountered.
+ /// (The capacity of the bucket)
+ unsigned int m_maxTokens;
+ /// The amount of tokens currently stored
+ /// (The amount of whater currently in the bucket)
+ long m_currentTokens;
+ /// A timer that is used to measure time intervals. It is essential
+ /// that this timer uses a monotonic clock (which is why QElapsedTimer is
+ /// used instead of QTime or QDateTime).
+ QElapsedTimer m_timer;
public:
- // Returns true if packets should be dropped
+ /// @param tokens The amount of tokens that should be added.
+ /// @returns Whether adding this amount of tokens triggers rate
+ /// limiting (true means the corresponding packet has to be
+ /// discared and false means the packet may be processed)
bool ratelimit(int tokens);
LeakyBucket(unsigned int tokensPerSec, unsigned int maxTokens);