+
+/******** Custom Rate Limiting ********/
+
+void CoreNetwork::updateRateLimiting(const bool forceUnlimited)
+{
+ // Always reset the delay and token bucket (safe-guard against accidentally starting the timer)
+
+ if (useCustomMessageRate() || forceUnlimited) {
+ // Custom message rates enabled, or chosen by means of forcing unlimited. Let's go for it!
+
+ _messageDelay = messageRateDelay();
+
+ _burstSize = messageRateBurstSize();
+ if (_burstSize < 1) {
+ qWarning() << "Invalid messageRateBurstSize data, cannot have zero message burst size!"
+ << _burstSize;
+ // Can't go slower than one message at a time
+ _burstSize = 1;
+ }
+
+ if (_tokenBucket > _burstSize) {
+ // Don't let the token bucket exceed the maximum
+ _tokenBucket = _burstSize;
+ // To fill up the token bucket, use resetRateLimiting(). Don't do that here, otherwise
+ // changing the rate-limit settings while connected to a server will incorrectly reset
+ // the token bucket.
+ }
+
+ // Toggle the timer according to whether or not rate limiting is enabled
+ // If we're here, useCustomMessageRate is true. Thus, the logic becomes
+ // _skipMessageRates = (useCustomMessageRate && (unlimitedMessageRate || forceUnlimited))
+ // Override user preferences if called with force unlimited
+ _skipMessageRates = (unlimitedMessageRate() || forceUnlimited);
+ if (_skipMessageRates) {
+ // If the message queue already contains messages, they need sent before disabling the
+ // timer. Set the timer to a rapid pace and let it disable itself.
+ if (_msgQueue.size() > 0) {
+ qDebug() << "Outgoing message queue contains messages while disabling rate "
+ "limiting. Sending remaining queued messages...";
+ // Promptly run the timer again to clear the messages. Rate limiting is disabled,
+ // so nothing should cause this to block.. in theory. However, don't directly call
+ // fillBucketAndProcessQueue() in order to keep it on a separate thread.
+ //
+ // TODO If testing shows this isn't needed, it can be simplified to a direct call.
+ // Hesitant to change it without a wide variety of situations to verify behavior.
+ _tokenBucketTimer.start(100);
+ } else {
+ // No rate limiting, disable the timer
+ _tokenBucketTimer.stop();
+ }
+ } else {
+ // Rate limiting enabled, enable the timer
+ _tokenBucketTimer.start(_messageDelay);
+ }
+ } else {
+ // Custom message rates disabled. Go for the default.
+
+ _skipMessageRates = false; // Enable rate-limiting by default
+ // TokenBucket to avoid sending too much at once
+ _messageDelay = 2200; // This seems to be a safe value (2.2 seconds delay)
+ _burstSize = 5; // 5 messages at once
+ if (_tokenBucket > _burstSize) {
+ // Don't let the token bucket exceed the maximum
+ _tokenBucket = _burstSize;
+ // To fill up the token bucket, use resetRateLimiting(). Don't do that here, otherwise
+ // changing the rate-limit settings while connected to a server will incorrectly reset
+ // the token bucket.
+ }
+ // Rate limiting enabled, enable the timer
+ _tokenBucketTimer.start(_messageDelay);
+ }
+}
+
+void CoreNetwork::resetTokenBucket()
+{
+ // Fill up the token bucket to the maximum
+ _tokenBucket = _burstSize;
+}
+
+