HttpRequest: move http cache to RequestRepeater (#21228)

* move cache from HttpRequest to RequestRepeater

* apply review
pull/21341/head
Dean Lee 4 years ago committed by GitHub
parent 26343951c9
commit 9ebfc2ba8d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 15
      selfdrive/ui/qt/api.cc
  2. 7
      selfdrive/ui/qt/api.h
  3. 22
      selfdrive/ui/qt/request_repeater.cc
  4. 3
      selfdrive/ui/qt/request_repeater.h
  5. 2
      selfdrive/ui/qt/widgets/ssh_keys.cc
  6. 2
      selfdrive/ui/replay/replay.cc

@ -67,7 +67,7 @@ QString create_jwt(const QJsonObject &payloads, int expiry) {
} // namespace CommaApi } // namespace CommaApi
HttpRequest::HttpRequest(QObject *parent, const QString &requestURL, const QString &cache_key, bool create_jwt_) : cache_key(cache_key), create_jwt(create_jwt_), QObject(parent) { HttpRequest::HttpRequest(QObject *parent, const QString &requestURL, bool create_jwt_) : create_jwt(create_jwt_), QObject(parent) {
networkAccessManager = new QNetworkAccessManager(this); networkAccessManager = new QNetworkAccessManager(this);
reply = NULL; reply = NULL;
@ -77,12 +77,6 @@ HttpRequest::HttpRequest(QObject *parent, const QString &requestURL, const QStri
connect(networkTimer, &QTimer::timeout, this, &HttpRequest::requestTimeout); connect(networkTimer, &QTimer::timeout, this, &HttpRequest::requestTimeout);
sendRequest(requestURL); sendRequest(requestURL);
if (!cache_key.isEmpty()) {
if (std::string cached_resp = Params().get(cache_key.toStdString()); !cached_resp.empty()) {
QTimer::singleShot(0, [=]() { emit receivedResponse(QString::fromStdString(cached_resp)); });
}
}
} }
void HttpRequest::sendRequest(const QString &requestURL) { void HttpRequest::sendRequest(const QString &requestURL) {
@ -116,15 +110,8 @@ void HttpRequest::requestFinished() {
QString response = reply->readAll(); QString response = reply->readAll();
if (reply->error() == QNetworkReply::NoError) { if (reply->error() == QNetworkReply::NoError) {
// save to cache
if (!cache_key.isEmpty()) {
Params().put(cache_key.toStdString(), response.toStdString());
}
emit receivedResponse(response); emit receivedResponse(response);
} else { } else {
if (!cache_key.isEmpty()) {
Params().remove(cache_key.toStdString());
}
qDebug() << reply->errorString(); qDebug() << reply->errorString();
emit failedResponse(reply->errorString()); emit failedResponse(reply->errorString());
} }

@ -20,14 +20,15 @@ class HttpRequest : public QObject {
Q_OBJECT Q_OBJECT
public: public:
explicit HttpRequest(QObject* parent, const QString &requestURL, const QString &cache_key = "", bool create_jwt_ = true); explicit HttpRequest(QObject* parent, const QString &requestURL, bool create_jwt_ = true);
QNetworkReply *reply;
void sendRequest(const QString &requestURL); void sendRequest(const QString &requestURL);
protected:
QNetworkReply *reply;
private: private:
QNetworkAccessManager *networkAccessManager; QNetworkAccessManager *networkAccessManager;
QTimer *networkTimer; QTimer *networkTimer;
QString cache_key;
bool create_jwt; bool create_jwt;
private slots: private slots:

@ -1,7 +1,7 @@
#include "selfdrive/ui/qt/request_repeater.h" #include "selfdrive/ui/qt/request_repeater.h"
RequestRepeater::RequestRepeater(QObject *parent, const QString &requestURL, const QString &cacheKey, RequestRepeater::RequestRepeater(QObject *parent, const QString &requestURL, const QString &cacheKey,
int period) : HttpRequest(parent, requestURL, cacheKey) { int period) : HttpRequest(parent, requestURL) {
timer = new QTimer(this); timer = new QTimer(this);
timer->setTimerType(Qt::VeryCoarseTimer); timer->setTimerType(Qt::VeryCoarseTimer);
QObject::connect(timer, &QTimer::timeout, [=]() { QObject::connect(timer, &QTimer::timeout, [=]() {
@ -9,5 +9,25 @@ RequestRepeater::RequestRepeater(QObject *parent, const QString &requestURL, con
sendRequest(requestURL); sendRequest(requestURL);
} }
}); });
timer->start(period * 1000); timer->start(period * 1000);
if (!cacheKey.isEmpty()) {
prevResp = QString::fromStdString(params.get(cacheKey.toStdString()));
if (!prevResp.isEmpty()) {
QTimer::singleShot(0, [=]() { emit receivedResponse(prevResp); });
}
QObject::connect(this, &HttpRequest::receivedResponse, [=](const QString &resp) {
if (resp != prevResp) {
params.put(cacheKey.toStdString(), resp.toStdString());
prevResp = resp;
}
});
QObject::connect(this, &HttpRequest::failedResponse, [=](const QString &err) {
if (!prevResp.isEmpty()) {
params.remove(cacheKey.toStdString());
prevResp = "";
}
});
}
} }

@ -1,5 +1,6 @@
#pragma once #pragma once
#include "selfdrive/common/util.h"
#include "selfdrive/ui/qt/api.h" #include "selfdrive/ui/qt/api.h"
#include "selfdrive/ui/ui.h" #include "selfdrive/ui/ui.h"
@ -8,5 +9,7 @@ public:
RequestRepeater(QObject *parent, const QString &requestURL, const QString &cacheKey = "", int period = 0); RequestRepeater(QObject *parent, const QString &requestURL, const QString &cacheKey = "", int period = 0);
private: private:
Params params;
QTimer *timer; QTimer *timer;
QString prevResp;
}; };

@ -40,7 +40,7 @@ void SshControl::refresh() {
} }
void SshControl::getUserKeys(const QString &username) { void SshControl::getUserKeys(const QString &username) {
HttpRequest *request = new HttpRequest(this, "https://github.com/" + username + ".keys", "", false); HttpRequest *request = new HttpRequest(this, "https://github.com/" + username + ".keys", false);
QObject::connect(request, &HttpRequest::receivedResponse, [=](const QString &resp) { QObject::connect(request, &HttpRequest::receivedResponse, [=](const QString &resp) {
if (!resp.isEmpty()) { if (!resp.isEmpty()) {
params.put("GithubUsername", username.toStdString()); params.put("GithubUsername", username.toStdString());

@ -46,7 +46,7 @@ Replay::Replay(QString route, SubMaster *sm_, QObject *parent) : sm(sm_), QObjec
} }
const QString url = "https://api.commadotai.com/v1/route/" + route + "/files"; const QString url = "https://api.commadotai.com/v1/route/" + route + "/files";
http = new HttpRequest(this, url, "", !Hardware::PC()); http = new HttpRequest(this, url, !Hardware::PC());
QObject::connect(http, &HttpRequest::receivedResponse, this, &Replay::parseResponse); QObject::connect(http, &HttpRequest::receivedResponse, this, &Replay::parseResponse);
} }

Loading…
Cancel
Save