1 #include "throttledhttp.h"
3 ThrottledHttp::ThrottledHttp(Http &http) : http(http), milliseconds(1000) {
7 HttpReply *ThrottledHttp::request(const HttpRequest &req) {
8 return new ThrottledHttpReply(http, req, milliseconds, elapsedTimer);
11 ThrottledHttpReply::ThrottledHttpReply(Http &http,
12 const HttpRequest &req,
14 QElapsedTimer &elapsedTimer)
15 : http(http), req(req), milliseconds(milliseconds), elapsedTimer(elapsedTimer), timer(nullptr) {
19 void ThrottledHttpReply::checkElapsed() {
22 QMutexLocker locker(&mutex);
25 const qint64 elapsedSinceLastRequest = elapsedTimer.elapsed();
26 if (elapsedSinceLastRequest < milliseconds) {
28 timer = new QTimer(this);
29 timer->setSingleShot(true);
30 timer->setTimerType(Qt::PreciseTimer);
31 connect(timer, SIGNAL(timeout()), SLOT(checkElapsed()));
33 qDebug() << "Throttling" << req.url
34 << QStringLiteral("%1ms").arg(milliseconds - elapsedSinceLastRequest);
35 timer->setInterval(milliseconds - elapsedSinceLastRequest);
43 void ThrottledHttpReply::doRequest() {
44 QObject *reply = http.request(req);
45 connect(reply, SIGNAL(data(QByteArray)), SIGNAL(data(QByteArray)));
46 connect(reply, SIGNAL(error(QString)), SIGNAL(error(QString)));
47 connect(reply, SIGNAL(finished(HttpReply)), SIGNAL(finished(HttpReply)));
49 // this will cause the deletion of this object once the request is finished