added priority
Some checks failed
buildbot/DocsGenerator Build finished.
buildbot/LinuxCMakeBuilderQt6 Build finished.
buildbot/AndroidBuilder_v8Qt6 Build finished.

This commit is contained in:
Andrei Yankovich 2024-12-15 00:52:45 +01:00
parent f2cf4b0442
commit f8c04492fa
11 changed files with 66 additions and 17 deletions

View File

@ -65,7 +65,7 @@ int main(int argc, char *argv[]) {
if (tmsg->text() == "spam") {
for (int i = 0 ; i < 1000; i++) {
bot.sendMessage(tmsg->chatId(), QString(" message N %0").arg(i));
bot.sendMessage(tmsg->chatId(), QString(" message N %0").arg(i), qTbot::iRequest::LowPriority);
}
} else {
bot.sendSpecificMessageWithKeyboard(qTbot::TelegramArgs{tmsg->chatId(), "I see it", tmsg->messageId()},

View File

@ -13,6 +13,7 @@ namespace qTbot {
TelegramGetUpdate::TelegramGetUpdate(unsigned long long offset): TelegramSingleRquest("getUpdates"){
addArg("offset", offset);
addArg("timeout", 30);
setPriority(UngeredPriority);
}
}

View File

@ -18,6 +18,7 @@ TelegramSendMsg::TelegramSendMsg(const TelegramArgs& generalArgs,
{
QMap<QString, QVariant>&& args = generalArgs.toMap();
setPriority(generalArgs.requestPriority);
for (auto it = extraObjects.begin(); it != extraObjects.end(); it = std::next(it)) {
args[it.key()] = QJsonDocument(*it.value()).toJson(QJsonDocument::Compact);

View File

@ -121,7 +121,8 @@ QFuture<QByteArray>
IBot::sendRequest(const QSharedPointer<iRequest> &rquest) {
auto&& responce = QSharedPointer<QPromise<QByteArray>>::create();
responce->start();
_requestQueue.push_back(RequestData{rquest, "", responce});
_requestQueue.insert(rquest->priority(),
RequestData{rquest, "", responce});
_requestExecutor->start();
@ -133,7 +134,8 @@ IBot::sendRequest(const QSharedPointer<iRequest> &rquest,
const QString &pathToResult) {
auto&& responce = QSharedPointer<QPromise<QByteArray>>::create();
responce->start();
_requestQueue.push_back(RequestData{rquest, pathToResult, responce});
_requestQueue.insert(rquest->priority(),
RequestData{rquest, pathToResult, responce});
_requestExecutor->start();
@ -171,7 +173,7 @@ void IBot::handleEcxecuteRequest() {
return;
}
auto&& requestData = _requestQueue.takeFirst();
auto&& requestData = _requestQueue.take(_requestQueue.lastKey());
if (requestData.responceFilePath.size()) {
sendRequestPrivate(requestData.request, requestData.responceFilePath, requestData.responce);
@ -189,7 +191,7 @@ void IBot::sendRequestPrivate(const QSharedPointer<iRequest> &rquest,
return;
}
setParallelActiveNetworkThreads(_currentParallelActiveNetworkThreads + 1);
setCurrentParallelActiveNetworkThreads(_currentParallelActiveNetworkThreads + 1);
connect(networkReplay, &QNetworkReply::finished, [this, networkReplay, promise](){
if (networkReplay->error() == QNetworkReply::NoError) {
@ -200,7 +202,7 @@ void IBot::sendRequestPrivate(const QSharedPointer<iRequest> &rquest,
promise->setException(HttpException(networkReplay->error(), networkReplay->errorString().toLatin1() + networkReplay->readAll()));
}
setParallelActiveNetworkThreads(_currentParallelActiveNetworkThreads - 1);
setCurrentParallelActiveNetworkThreads(_currentParallelActiveNetworkThreads - 1);
});
@ -231,7 +233,7 @@ void IBot::sendRequestPrivate(const QSharedPointer<iRequest> &rquest,
return;
}
setParallelActiveNetworkThreads(_currentParallelActiveNetworkThreads + 1);
setCurrentParallelActiveNetworkThreads(_currentParallelActiveNetworkThreads + 1);
connect(networkReplay, &QNetworkReply::finished, [this, promise, networkReplay, pathToResult](){
if (networkReplay->error() == QNetworkReply::NoError) {
@ -240,7 +242,7 @@ void IBot::sendRequestPrivate(const QSharedPointer<iRequest> &rquest,
promise->addResult(pathToResult.toUtf8()); // wil not work with UTF 8 path names
promise->finish();
}
setParallelActiveNetworkThreads(_currentParallelActiveNetworkThreads - 1);
setCurrentParallelActiveNetworkThreads(_currentParallelActiveNetworkThreads - 1);
});
connect(networkReplay, &QNetworkReply::readyRead, [networkReplay, promise, pathToResult, file](){

View File

@ -92,7 +92,7 @@ public:
*
* @note the specific implementations of this interface can have a different method for sending.
*/
virtual bool sendMessage(const QVariant& chatId, const QString& text) = 0;
virtual bool sendMessage(const QVariant& chatId, const QString& text, iRequest::RequestPriority priority = iRequest::NormalPriority) = 0;
/**
* @brief deleteMessage This is main method to delete messages.
@ -310,9 +310,9 @@ private:
QSet<unsigned long long> _processed;
QNetworkAccessManager *_manager = nullptr;
QTimer* _requestExecutor = nullptr;
QList<RequestData> _requestQueue;
QMultiMap<int, RequestData> _requestQueue;
int _currentParallelActiveNetworkThreads = 0;
int _parallelActiveNetworkThreads = 10;
int _parallelActiveNetworkThreads = 5;

View File

@ -91,6 +91,14 @@ QSharedPointer<QHttpMultiPart> iRequest::argsToMultipartFormData() const {
return multiPart;
}
iRequest::RequestPriority iRequest::priority() const {
return _priority;
}
void iRequest::setPriority(RequestPriority newPriority) {
_priority = newPriority;
}
const QString& iRequest::request() const {
return _request;
}

View File

@ -46,6 +46,19 @@ public:
Upload
};
/**
* @brief The RequestPriority enum
*/
enum RequestPriority {
NoPriority = 0,
LowPriority = 1,
NormalPriority = 2,
HighPriority = 3,
UngeredPriority = 4,
MaxPriorityValue = 0xff
};
/**
* @brief makeUpload This method prepare data to upload;
* @return data array prepared to sending.
@ -109,9 +122,19 @@ public:
* @return QHttpMultiPart - A QHttpMultiPart object containing multipart/form-data request data.
*/
QSharedPointer<QHttpMultiPart> argsToMultipartFormData() const;
/**
* @brief priority This is priority of executabel this request on client.
* @return
*/
RequestPriority priority() const;
void setPriority(RequestPriority newPriority);
private:
QString _request;
QMap<QString, QVariant> _args;
RequestPriority _priority = RequestPriority::NormalPriority;
};

View File

@ -64,11 +64,17 @@ bool ITelegramBot::login(const QByteArray &token) {
return loginFuture.isValid();
}
bool ITelegramBot::sendMessage(const QVariant &chatId, const QString &text) {
return sendSpecificMessage(TelegramArgs{chatId, text});
bool ITelegramBot::sendMessage(const QVariant &chatId,
const QString &text,
iRequest::RequestPriority priority) {
TelegramArgs arg{chatId, text};
arg.requestPriority = priority;
return sendSpecificMessage(arg);
}
bool ITelegramBot::sendLocationRequest(const QVariant &chatId, const QString &text, const QString &buttonText,
bool ITelegramBot::sendLocationRequest(const QVariant &chatId,
const QString &text,
const QString &buttonText,
bool onetimeKeyboard) {
auto replyMarkup = QSharedPointer<QJsonObject>::create();

View File

@ -40,7 +40,9 @@ public:
bool login(const QByteArray &token) override;
bool sendMessage(const QVariant &chatId, const QString& text) override;
bool sendMessage(const QVariant &chatId,
const QString& text,
iRequest::RequestPriority priority = iRequest::NormalPriority) override;
/**
* @brief sendLocationRequest This method setn into chat button that will automaticaly sent geo location to bot.

View File

@ -13,7 +13,8 @@ TelegramArgs::TelegramArgs(const QVariant &id,
unsigned long long replyToMessageId,
const QString &parseMode,
bool disableWebPagePreview,
const QString &callBackQueryId, const std::function<void (int)> &msgIdCB)
const QString &callBackQueryId, const std::function<void (int)> &msgIdCB,
iRequest::RequestPriority priority)
{
this->chatId = id;
@ -23,6 +24,7 @@ TelegramArgs::TelegramArgs(const QVariant &id,
this->replyToMessageId = replyToMessageId;
this->parseMode = parseMode;
this->msgIdCB = msgIdCB;
this->requestPriority = priority;
}
QMap<QString, QVariant> TelegramArgs::toMap(bool textAsCaption) const {

View File

@ -10,6 +10,7 @@
#include <QVariant>
#include "global.h"
#include "irequest.h"
namespace qTbot {
@ -24,7 +25,8 @@ struct QTBOT_EXPORT TelegramArgs
const QString& parseMode = "html",
bool disableWebPagePreview = false,
const QString& callBackQueryId = "",
const std::function<void(int msgId)>& msgIdCB = {}
const std::function<void(int msgId)>& msgIdCB = {},
iRequest::RequestPriority priority = iRequest::RequestPriority::NormalPriority
);
/**
@ -74,6 +76,8 @@ struct QTBOT_EXPORT TelegramArgs
* @brief msgIdCB This is id message call bak function. Will be inwoked when request finished successful.
*/
std::function<void(int msgId)> msgIdCB = {};
iRequest::RequestPriority requestPriority = iRequest::RequestPriority::NormalPriority;
};
}