Skip to content

Commit d4f197d

Browse files
committed
Timeout implementation
1 parent f433106 commit d4f197d

File tree

12 files changed

+137
-3
lines changed

12 files changed

+137
-3
lines changed

core/common/BaseClient.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -476,12 +476,13 @@ namespace Nakama {
476476
) override;
477477

478478
protected:
479-
int _port;
480-
std::string _host;
479+
int _port = -1;
480+
int _timeout = -1;
481481
bool _ssl = false;
482+
std::string _host;
482483
std::string _basicAuthMetadata;
483-
ErrorCallback _defaultErrorCallback;
484484
void* _userData = nullptr;
485+
ErrorCallback _defaultErrorCallback;
485486
NPlatformParameters _platformParams;
486487
};
487488
}

core/core-rest/RestClient.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ RestClient::RestClient(const NClientParameters& parameters, NHttpTransportPtr ht
7272
_ssl = parameters.ssl;
7373
_platformParams = parameters.platformParams;
7474
_port = parameters.port;
75+
_timeout = parameters.timeout;
7576
std::string baseUrl;
7677

7778

@@ -86,6 +87,11 @@ RestClient::RestClient(const NClientParameters& parameters, NHttpTransportPtr ht
8687

8788
_httpClient->setBaseUri(baseUrl);
8889

90+
if (_timeout >= 0)
91+
{
92+
_httpClient->setTimeout(_timeout);
93+
}
94+
8995
_basicAuthMetadata = "Basic " + base64Encode(parameters.serverKey + ":");
9096
}
9197

impl/httpCppRest/NHttpClientCppRest.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,17 @@ void NHttpClientCppRest::setBaseUri(const std::string& uri)
9292
_client = std::make_unique<http_client>(http_client(FROM_STD_STR(uri)));
9393
}
9494

95+
96+
97+
void NHttpClientCppRest::setTimeout(int seconds)
98+
{
99+
if(_client.get())
100+
{
101+
// _client->set_timeout(_timeout);
102+
_client->setHttpResponseTimeout(seconds);
103+
}
104+
}
105+
95106
void NHttpClientCppRest::tick()
96107
{
97108
ReqContextPtr ctx;

impl/httpCppRest/NHttpClientCppRest.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ namespace Nakama {
3939

4040
void setBaseUri(const std::string& uri) override;
4141

42+
void setTimeout(int seconds) override;
43+
4244
void tick() override;
4345

4446
void request(const NHttpRequest& req, const NHttpResponseCallback& callback = nullptr) override;

impl/httpCurl/NHttpClientLibCurl.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,16 @@ void NHttpClientLibCurl::request(const NHttpRequest& req, const NHttpResponseCal
120120
return;
121121
}
122122

123+
if (_timeout >= 0)
124+
{
125+
curl_code = curl_easy_setopt(curl_easy.get(), CURLOPT_TIMEOUT, _timeout);
126+
if (curl_code != CURLE_OK)
127+
{
128+
handle_curl_easy_set_opt_error("setting timeout", curl_code, callback);
129+
return;
130+
}
131+
}
132+
123133
curl_code = curl_easy_setopt(curl_easy.get(), CURLOPT_HTTPHEADER, headers_list);
124134
if (curl_code != CURLE_OK)
125135
{
@@ -206,6 +216,11 @@ void NHttpClientLibCurl::setBaseUri(const std::string& uri)
206216
_base_uri = uri;
207217
}
208218

219+
void NHttpClientLibCurl::setTimeout(int seconds)
220+
{
221+
_timeout = seconds;
222+
}
223+
209224
void NHttpClientLibCurl::tick()
210225
{
211226
std::unique_lock lock(_mutex, std::try_to_lock);

impl/httpCurl/NHttpClientLibCurl.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,14 @@ namespace Nakama {
3535
NHttpClientLibCurl(const NPlatformParameters& platformParameters);
3636

3737
void setBaseUri(const std::string& uri) override;
38+
void setTimeout(int seconds) override;
3839
void tick() override;
3940
void request(const NHttpRequest& req, const NHttpResponseCallback& callback = nullptr) noexcept override;
4041
void cancelAllRequests() override;
4142
private:
4243
std::unique_ptr<CURLM, decltype(&curl_multi_cleanup)> _curl_multi;
4344
std::string _base_uri;
45+
int _timeout = -1;
4446
// TODO implement curl_easy reuse.
4547
// TODO would be more performant but less safe as a map with CURL* as key
4648
std::list<std::pair<std::unique_ptr<CURL, decltype(&curl_easy_cleanup)>, std::unique_ptr<NHttpClientLibCurlContext>>> _contexts;

impl/httpLibHttpClient/NHttpClientLibHC.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,11 @@ void NHttpClientLibHC::setBaseUri(const std::string& uri)
107107
m_baseUri = uri;
108108
}
109109

110+
void NHttpClientLibHC::setTimeout(int seconds)
111+
{
112+
HCHttpCallRequestSetTimeout(nullptr, seconds);
113+
}
114+
110115
#define CHECK_CB(exp, msg, cb) { \
111116
HRESULT hr = exp; if (FAILED(hr)) { \
112117
HC_TRACE_ERROR_HR(httpTransportLibHC, hr, msg); \

impl/httpLibHttpClient/NHttpClientLibHC.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ namespace Nakama {
3232

3333
void setBaseUri(const std::string& uri) override;
3434

35+
void setTimeout(int seconds) override;
36+
3537
void tick() override;
3638

3739
void request(const NHttpRequest& req, const NHttpResponseCallback& callback = nullptr) override;

interface/include/nakama-cpp/ClientFactory.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ NAKAMA_NAMESPACE_BEGIN
4646
/// See the server documentation for more information.
4747
bool ssl = false;
4848

49+
/// Set request timeout in seconds for individual calls
50+
/// -1 or other negative number to leave the default
51+
int timeout = -1;
52+
4953
/// Platform specific parameters
5054
#ifdef DEFAULT_PLATFORM_PARAMS
5155
NPlatformParameters platformParams = {};

interface/include/nakama-cpp/NHttpTransportInterface.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ NAKAMA_NAMESPACE_BEGIN
7474

7575
virtual void setBaseUri(const std::string& uri) = 0;
7676

77+
virtual void setTimeout(int seconds) = 0;
78+
7779
virtual void tick() = 0;
7880

7981
/**

0 commit comments

Comments
 (0)