Skip to content

Commit a8defac

Browse files
committed
Fix timeout issues (#251)
1 parent 443b769 commit a8defac

6 files changed

Lines changed: 19 additions & 15 deletions

File tree

include/tgbot/Api.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1982,12 +1982,13 @@ friend class Bot;
19821982
* @return Returns True if bot is blocked by user
19831983
*/
19841984
bool blockedByUser(std::int64_t chatId) const;
1985+
1986+
const HttpClient& _httpClient;
19851987

19861988
private:
19871989
boost::property_tree::ptree sendRequest(const std::string& method, const std::vector<HttpReqArg>& args = std::vector<HttpReqArg>()) const;
19881990

19891991
const std::string _token;
1990-
const HttpClient& _httpClient;
19911992
const TgTypeParser _tgTypeParser;
19921993
const std::string _url;
19931994
};

include/tgbot/net/HttpClient.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ class TGBOT_API HttpClient {
2626
* If at least 1 arg is marked as file, the content type of a request will be multipart/form-data, otherwise it will be application/x-www-form-urlencoded.
2727
*/
2828
virtual std::string makeRequest(const Url& url, const std::vector<HttpReqArg>& args) const = 0;
29+
30+
std::int32_t _timeout = 25;
2931
};
3032

3133
}

include/tgbot/net/TgLongPoll.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#ifndef TGBOT_TGLONGPOLL_H
22
#define TGBOT_TGLONGPOLL_H
33

4+
#include "tgbot/Api.h"
45
#include "tgbot/export.h"
56

67
#include <cstdint>
@@ -10,7 +11,6 @@
1011

1112
namespace TgBot {
1213

13-
class Api;
1414
class Bot;
1515
class EventHandler;
1616

@@ -22,8 +22,8 @@ class EventHandler;
2222
class TGBOT_API TgLongPoll {
2323

2424
public:
25-
TgLongPoll(const Api* api, const EventHandler* eventHandler, std::int32_t, std::int32_t, std::shared_ptr<std::vector<std::string>>);
26-
TgLongPoll(const Bot& bot, std::int32_t = 100, std::int32_t = 10, const std::shared_ptr<std::vector<std::string>>& = nullptr);
25+
TgLongPoll(const Api* api, const EventHandler* eventHandler, std::int32_t limit, std::int32_t timeout, std::shared_ptr<std::vector<std::string>> allowUpdates);
26+
TgLongPoll(const Bot& bot, std::int32_t limit = 100, std::int32_t timeout = 10, const std::shared_ptr<std::vector<std::string>>& allowUpdates = nullptr);
2727

2828
/**
2929
* @brief Starts long poll. After new update will come, this method will parse it and send to EventHandler which invokes your listeners. Designed to be executed in a loop.
@@ -37,6 +37,8 @@ class TGBOT_API TgLongPoll {
3737
std::int32_t _limit;
3838
std::int32_t _timeout;
3939
std::shared_ptr<std::vector<std::string>> _allowUpdates;
40+
41+
std::vector<Update::Ptr> _updates;
4042
};
4143

4244
}

src/net/BoostHttpOnlySslClient.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ string BoostHttpOnlySslClient::makeRequest(const Url& url, const vector<HttpReqA
5252
struct timeval timeStruct;
5353

5454
// set the timeout to 20 seconds
55-
timeStruct.tv_sec = 20;
55+
timeStruct.tv_sec = _timeout;
5656
timeStruct.tv_usec = 0;
5757
FD_ZERO(&fileDescriptorSet);
5858

src/net/CurlHttpClient.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ CurlHttpClient::CurlHttpClient() : _httpParser() {
1111
curlSettings = curl_easy_init();
1212

1313
curl_easy_setopt(curlSettings, CURLOPT_CONNECTTIMEOUT, 20);
14-
curl_easy_setopt(curlSettings, CURLOPT_TIMEOUT, 25);
14+
curl_easy_setopt(curlSettings, CURLOPT_TIMEOUT, _timeout);
1515
}
1616

1717
CurlHttpClient::~CurlHttpClient() {

src/net/TgLongPoll.cpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,27 @@
1212
namespace TgBot {
1313

1414
TgLongPoll::TgLongPoll(const Api* api, const EventHandler* eventHandler, std::int32_t limit, std::int32_t timeout, std::shared_ptr<std::vector<std::string>> allowUpdates)
15-
: _api(api), _eventHandler(eventHandler), _limit(limit), _timeout(timeout),
16-
_allowUpdates(std::move(allowUpdates)) {
15+
: _api(api), _eventHandler(eventHandler), _limit(limit), _timeout(timeout)
16+
, _allowUpdates(std::move(allowUpdates)) {
17+
18+
const_cast<TgBot::HttpClient&>(_api->_httpClient)._timeout = _timeout + 5;
1719
}
1820

19-
TgLongPoll::TgLongPoll(const Bot& bot, std::int32_t limit, std::int32_t timeout, const std::shared_ptr<std::vector<std::string>>& allowUpdates) :
20-
TgLongPoll(&bot.getApi(), &bot.getEventHandler(), limit, timeout, allowUpdates) {
21+
TgLongPoll::TgLongPoll(const Bot& bot, std::int32_t limit, std::int32_t timeout, const std::shared_ptr<std::vector<std::string>>& allowUpdates)
22+
: TgLongPoll(&bot.getApi(), &bot.getEventHandler(), limit, timeout, allowUpdates) {
2123
}
2224

2325
void TgLongPoll::start() {
24-
// get all unconfirmed updates
25-
std::vector<Update::Ptr> updates = _api->getUpdates(0, _limit, _timeout, _allowUpdates);
26-
2726
// handle updates
28-
for (Update::Ptr& item : updates) {
27+
for (Update::Ptr& item : _updates) {
2928
if (item->updateId >= _lastUpdateId) {
3029
_lastUpdateId = item->updateId + 1;
3130
}
3231
_eventHandler->handleUpdate(item);
3332
}
3433

3534
// confirm handled updates
36-
_api->getUpdates(_lastUpdateId, _limit, _timeout, _allowUpdates);
35+
_updates = _api->getUpdates(_lastUpdateId, _limit, _timeout, _allowUpdates);
3736
}
3837

3938
}

0 commit comments

Comments
 (0)