X-Git-Url: https://git.quassel-irc.org/?a=blobdiff_plain;f=src%2Fcore%2Fnetworkconnection.cpp;h=15b081cc0a7a633a8f11e1307f47c7e064b00f2c;hb=374ea2a5188930b880ad67584a9b0055022feecf;hp=be2787aa3009e5bd04c8e5ca54a243f962b3c1aa;hpb=2bcb925e42921b124efd19d1978ff19f44a09113;p=quassel.git diff --git a/src/core/networkconnection.cpp b/src/core/networkconnection.cpp index be2787aa..15b081cc 100644 --- a/src/core/networkconnection.cpp +++ b/src/core/networkconnection.cpp @@ -61,6 +61,14 @@ NetworkConnection::NetworkConnection(Network *network, CoreSession *session) : Q _autoWhoCycleTimer.setInterval(_autoWhoInterval * 1000); _autoWhoCycleTimer.setSingleShot(false); + // TokenBucket to avaid sending too much at once + _messagesPerSecond = 1; + _burstSize = 5; + _tokenBucket = 5; // init with a full bucket + + _tokenBucketTimer.start(_messagesPerSecond * 1000); + _tokenBucketTimer.setSingleShot(false); + QHash channels = coreSession()->persistentChannels(networkId()); foreach(QString chan, channels.keys()) { _channelKeys[chan.toLower()] = channels[chan]; @@ -69,6 +77,7 @@ NetworkConnection::NetworkConnection(Network *network, CoreSession *session) : Q connect(&_autoReconnectTimer, SIGNAL(timeout()), this, SLOT(doAutoReconnect())); connect(&_autoWhoTimer, SIGNAL(timeout()), this, SLOT(sendAutoWho())); connect(&_autoWhoCycleTimer, SIGNAL(timeout()), this, SLOT(startAutoWhoCycle())); + connect(&_tokenBucketTimer, SIGNAL(timeout()), this, SLOT(fillBucketAndProcessQueue())); connect(network, SIGNAL(currentServerSet(const QString &)), this, SLOT(networkInitialized(const QString &))); connect(network, SIGNAL(useAutoReconnectSet(bool)), this, SLOT(autoReconnectSettingsChanged())); @@ -380,8 +389,27 @@ void NetworkConnection::userInput(BufferInfo buf, QString msg) { } void NetworkConnection::putRawLine(QByteArray s) { + if(_tokenBucket > 0) { + writeToSocket(s); + } else { + _msgQueue.append(s); + } +} + +void NetworkConnection::writeToSocket(QByteArray s) { s += "\r\n"; socket.write(s); + _tokenBucket--; +} + +void NetworkConnection::fillBucketAndProcessQueue() { + if(_tokenBucket < _burstSize) { + _tokenBucket++; + } + + while(_msgQueue.size() > 0 && _tokenBucket > 0) { + writeToSocket(_msgQueue.takeFirst()); + } } void NetworkConnection::putCmd(const QString &cmd, const QVariantList ¶ms, const QByteArray &prefix) {