2744: Allow different timeouts for different downloads r=heinrich5991 a=def-



Co-authored-by: def <dennis@felsin9.de>
This commit is contained in:
bors[bot] 2020-09-06 23:05:57 +00:00 committed by GitHub
commit 742ee9094c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 34 additions and 35 deletions

View file

@ -1736,9 +1736,9 @@ void CClient::ProcessServerPacket(CNetChunk *pPacket)
char aUrl[256]; char aUrl[256];
char aEscaped[256]; char aEscaped[256];
EscapeUrl(aEscaped, sizeof(aEscaped), aFilename); EscapeUrl(aEscaped, sizeof(aEscaped), aFilename);
str_format(aUrl, sizeof(aUrl), "%s/%s", g_Config.m_ClDDNetMapDownloadUrl, aEscaped); str_format(aUrl, sizeof(aUrl), "%s/%s", g_Config.m_ClMapDownloadUrl, aEscaped);
m_pMapdownloadTask = std::make_shared<CGetFile>(Storage(), aUrl, m_aMapdownloadFilename, IStorage::TYPE_SAVE, true); m_pMapdownloadTask = std::make_shared<CGetFile>(Storage(), aUrl, m_aMapdownloadFilename, IStorage::TYPE_SAVE, CTimeout{g_Config.m_ClMapDownloadConnectTimeoutMs, g_Config.m_ClMapDownloadLowSpeedLimit, g_Config.m_ClMapDownloadLowSpeedTime});
Engine()->AddJob(m_pMapdownloadTask); Engine()->AddJob(m_pMapdownloadTask);
} }
else else
@ -4323,7 +4323,7 @@ void CClient::RequestDDNetInfo()
str_append(aUrl, aEscaped, sizeof(aUrl)); str_append(aUrl, aEscaped, sizeof(aUrl));
} }
m_pDDNetInfoTask = std::make_shared<CGetFile>(Storage(), aUrl, m_aDDNetInfoTmp, IStorage::TYPE_SAVE, true); m_pDDNetInfoTask = std::make_shared<CGetFile>(Storage(), aUrl, m_aDDNetInfoTmp, IStorage::TYPE_SAVE, CTimeout{10000, 500, 10});
Engine()->AddJob(m_pDDNetInfoTask); Engine()->AddJob(m_pDDNetInfoTask);
} }

View file

@ -75,8 +75,8 @@ void EscapeUrl(char *pBuf, int Size, const char *pStr)
curl_free(pEsc); curl_free(pEsc);
} }
CRequest::CRequest(const char *pUrl, bool CanTimeout) : CRequest::CRequest(const char *pUrl, CTimeout Timeout) :
m_CanTimeout(CanTimeout), m_Timeout(Timeout),
m_Size(0), m_Size(0),
m_Progress(0), m_Progress(0),
m_State(HTTP_QUEUED), m_State(HTTP_QUEUED),
@ -115,18 +115,10 @@ int CRequest::RunImpl(CURL *pHandle)
char aErr[CURL_ERROR_SIZE]; char aErr[CURL_ERROR_SIZE];
curl_easy_setopt(pHandle, CURLOPT_ERRORBUFFER, aErr); curl_easy_setopt(pHandle, CURLOPT_ERRORBUFFER, aErr);
if(m_CanTimeout) curl_easy_setopt(pHandle, CURLOPT_CONNECTTIMEOUT_MS, m_Timeout.ConnectTimeoutMs);
{ curl_easy_setopt(pHandle, CURLOPT_LOW_SPEED_LIMIT, m_Timeout.LowSpeedLimit);
curl_easy_setopt(pHandle, CURLOPT_CONNECTTIMEOUT_MS, (long)g_Config.m_ClHTTPConnectTimeoutMs); curl_easy_setopt(pHandle, CURLOPT_LOW_SPEED_TIME, m_Timeout.LowSpeedTime);
curl_easy_setopt(pHandle, CURLOPT_LOW_SPEED_LIMIT, (long)g_Config.m_ClHTTPLowSpeedLimit);
curl_easy_setopt(pHandle, CURLOPT_LOW_SPEED_TIME, (long)g_Config.m_ClHTTPLowSpeedTime);
}
else
{
curl_easy_setopt(pHandle, CURLOPT_CONNECTTIMEOUT_MS, 0L);
curl_easy_setopt(pHandle, CURLOPT_LOW_SPEED_LIMIT, 0L);
curl_easy_setopt(pHandle, CURLOPT_LOW_SPEED_TIME, 0L);
}
curl_easy_setopt(pHandle, CURLOPT_SHARE, gs_Share); curl_easy_setopt(pHandle, CURLOPT_SHARE, gs_Share);
curl_easy_setopt(pHandle, CURLOPT_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS); curl_easy_setopt(pHandle, CURLOPT_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS);
curl_easy_setopt(pHandle, CURLOPT_FOLLOWLOCATION, 1L); curl_easy_setopt(pHandle, CURLOPT_FOLLOWLOCATION, 1L);
@ -181,8 +173,8 @@ int CRequest::ProgressCallback(void *pUser, double DlTotal, double DlCurr, doubl
return pTask->m_Abort ? -1 : 0; return pTask->m_Abort ? -1 : 0;
} }
CGet::CGet(const char *pUrl, bool CanTimeout) : CGet::CGet(const char *pUrl, CTimeout Timeout) :
CRequest(pUrl, CanTimeout), CRequest(pUrl, Timeout),
m_BufferSize(0), m_BufferSize(0),
m_BufferLength(0), m_BufferLength(0),
m_pBuffer(NULL) m_pBuffer(NULL)
@ -254,8 +246,8 @@ size_t CGet::OnData(char *pData, size_t DataSize)
return DataSize; return DataSize;
} }
CGetFile::CGetFile(IStorage *pStorage, const char *pUrl, const char *pDest, int StorageType, bool CanTimeout) : CGetFile::CGetFile(IStorage *pStorage, const char *pUrl, const char *pDest, int StorageType, CTimeout Timeout) :
CRequest(pUrl, CanTimeout), CRequest(pUrl, Timeout),
m_pStorage(pStorage), m_pStorage(pStorage),
m_StorageType(StorageType) m_StorageType(StorageType)
{ {
@ -295,8 +287,8 @@ bool CGetFile::BeforeCompletion()
return io_close(m_File) == 0; return io_close(m_File) == 0;
} }
CPostJson::CPostJson(const char *pUrl, bool CanTimeout, const char *pJson) CPostJson::CPostJson(const char *pUrl, CTimeout Timeout, const char *pJson)
: CRequest(pUrl, CanTimeout) : CRequest(pUrl, Timeout)
{ {
str_copy(m_aJson, pJson, sizeof(m_aJson)); str_copy(m_aJson, pJson, sizeof(m_aJson));
} }

View file

@ -17,6 +17,13 @@ enum
HTTP_ABORTED, HTTP_ABORTED,
}; };
struct CTimeout
{
long ConnectTimeoutMs;
long LowSpeedLimit;
long LowSpeedTime;
};
class CRequest : public IJob class CRequest : public IJob
{ {
// Abort the request with an error if `BeforeInit()` or `AfterInit()` // Abort the request with an error if `BeforeInit()` or `AfterInit()`
@ -31,7 +38,8 @@ class CRequest : public IJob
virtual void OnCompletion() { } virtual void OnCompletion() { }
char m_aUrl[256]; char m_aUrl[256];
bool m_CanTimeout;
CTimeout m_Timeout;
double m_Size; double m_Size;
double m_Current; double m_Current;
@ -47,7 +55,7 @@ class CRequest : public IJob
int RunImpl(CURL *pHandle); int RunImpl(CURL *pHandle);
public: public:
CRequest(const char *pUrl, bool CanTimeout); CRequest(const char *pUrl, CTimeout Timeout);
double Current() const { return m_Current; } double Current() const { return m_Current; }
double Size() const { return m_Size; } double Size() const { return m_Size; }
@ -65,7 +73,7 @@ class CGet : public CRequest
unsigned char *m_pBuffer; unsigned char *m_pBuffer;
public: public:
CGet(const char *pUrl, bool CanTimeout); CGet(const char *pUrl, CTimeout Timeout);
~CGet(); ~CGet();
size_t ResultSize() const { if(!Result()) { return 0; } else { return m_BufferSize; } } size_t ResultSize() const { if(!Result()) { return 0; } else { return m_BufferSize; } }
@ -87,7 +95,7 @@ class CGetFile : public CRequest
IOHANDLE m_File; IOHANDLE m_File;
public: public:
CGetFile(IStorage *pStorage, const char *pUrl, const char *pDest, int StorageType = -2, bool CanTimeout = true); CGetFile(IStorage *pStorage, const char *pUrl, const char *pDest, int StorageType = -2, CTimeout Timeout = CTimeout{4000, 500, 5});
const char *Dest() const { return m_aDest; } const char *Dest() const { return m_aDest; }
}; };
@ -101,7 +109,7 @@ class CPostJson : public CRequest
char m_aJson[1024]; char m_aJson[1024];
public: public:
CPostJson(const char *pUrl, bool CanTimeout, const char *pJson); CPostJson(const char *pUrl, CTimeout Timeout, const char *pJson);
}; };
bool HttpInit(IStorage *pStorage); bool HttpInit(IStorage *pStorage);

View file

@ -42,7 +42,7 @@ static const char *GetUpdaterDestPath(char *pBuf, int BufSize, const char *pFile
} }
CUpdaterFetchTask::CUpdaterFetchTask(CUpdater *pUpdater, const char *pFile, const char *pDestPath) : CUpdaterFetchTask::CUpdaterFetchTask(CUpdater *pUpdater, const char *pFile, const char *pDestPath) :
CGetFile(pUpdater->m_pStorage, GetUpdaterUrl(m_aBuf, sizeof(m_aBuf), pFile), GetUpdaterDestPath(m_aBuf2, sizeof(m_aBuf), pFile, pDestPath), -2, false), CGetFile(pUpdater->m_pStorage, GetUpdaterUrl(m_aBuf, sizeof(m_aBuf), pFile), GetUpdaterDestPath(m_aBuf2, sizeof(m_aBuf), pFile, pDestPath), -2, CTimeout{0, 0, 0}),
m_pUpdater(pUpdater) m_pUpdater(pUpdater)
{ {
} }

View file

@ -77,7 +77,11 @@ MACRO_CONFIG_INT(EdShowkeys, ed_showkeys, 0, 0, 1, CFGFLAG_CLIENT|CFGFLAG_SAVE,
MACRO_CONFIG_INT(ClShowWelcome, cl_show_welcome, 1, 0, 1, CFGFLAG_CLIENT|CFGFLAG_SAVE, "") MACRO_CONFIG_INT(ClShowWelcome, cl_show_welcome, 1, 0, 1, CFGFLAG_CLIENT|CFGFLAG_SAVE, "")
MACRO_CONFIG_INT(ClMotdTime, cl_motd_time, 10, 0, 100, CFGFLAG_CLIENT|CFGFLAG_SAVE, "How long to show the server message of the day") MACRO_CONFIG_INT(ClMotdTime, cl_motd_time, 10, 0, 100, CFGFLAG_CLIENT|CFGFLAG_SAVE, "How long to show the server message of the day")
MACRO_CONFIG_STR(ClDDNetMapDownloadUrl, cl_ddnet_map_download_url, 100, "https://maps2.ddnet.tw", CFGFLAG_CLIENT|CFGFLAG_SAVE, "URL to use to download maps (can start with http:// or https://)") // http map download
MACRO_CONFIG_STR(ClMapDownloadUrl, cl_map_download_url, 100, "https://maps2.ddnet.tw", CFGFLAG_CLIENT|CFGFLAG_SAVE, "URL to use to download maps (can start with http:// or https://)")
MACRO_CONFIG_INT(ClMapDownloadConnectTimeoutMs, cl_map_download_connect_timeout_ms, 2000, 0, 100000, CFGFLAG_CLIENT|CFGFLAG_SAVE, "HTTP map downloads: timeout for the connect phase in milliseconds (0 to disable)")
MACRO_CONFIG_INT(ClMapDownloadLowSpeedLimit, cl_map_download_low_speed_limit, 500, 0, 100000, CFGFLAG_CLIENT|CFGFLAG_SAVE, "HTTP map downloads: Set low speed limit in bytes per second (0 to disable)")
MACRO_CONFIG_INT(ClMapDownloadLowSpeedTime, cl_map_download_low_speed_time, 5, 0, 100000, CFGFLAG_CLIENT|CFGFLAG_SAVE, "HTTP map downloads: Set low speed limit time period (0 to disable)")
MACRO_CONFIG_STR(ClLanguagefile, cl_languagefile, 255, "", CFGFLAG_CLIENT|CFGFLAG_SAVE, "What language file to use") MACRO_CONFIG_STR(ClLanguagefile, cl_languagefile, 255, "", CFGFLAG_CLIENT|CFGFLAG_SAVE, "What language file to use")
MACRO_CONFIG_INT(ClVanillaSkinsOnly, cl_vanilla_skins_only, 0, 0, 1, CFGFLAG_CLIENT|CFGFLAG_SAVE, "Only show skins available in Vanilla Teeworlds") MACRO_CONFIG_INT(ClVanillaSkinsOnly, cl_vanilla_skins_only, 0, 0, 1, CFGFLAG_CLIENT|CFGFLAG_SAVE, "Only show skins available in Vanilla Teeworlds")
@ -131,11 +135,6 @@ MACRO_CONFIG_INT(ClDummyJump, cl_dummy_jump, 0, 0, 1, CFGFLAG_CLIENT, "Whether d
MACRO_CONFIG_INT(ClDummyFire, cl_dummy_fire, 0, 0, 1, CFGFLAG_CLIENT, "Whether dummy is firing") MACRO_CONFIG_INT(ClDummyFire, cl_dummy_fire, 0, 0, 1, CFGFLAG_CLIENT, "Whether dummy is firing")
MACRO_CONFIG_INT(ClDummyHook, cl_dummy_hook, 0, 0, 1, CFGFLAG_CLIENT, "Whether dummy is hooking") MACRO_CONFIG_INT(ClDummyHook, cl_dummy_hook, 0, 0, 1, CFGFLAG_CLIENT, "Whether dummy is hooking")
// curl http download
MACRO_CONFIG_INT(ClHTTPConnectTimeoutMs, cl_http_connect_timeout_ms, 2000, 0, 100000, CFGFLAG_CLIENT|CFGFLAG_SAVE, "HTTP downloads: timeout for the connect phase in milliseconds (0 to disable)")
MACRO_CONFIG_INT(ClHTTPLowSpeedLimit, cl_http_low_speed_limit, 500, 0, 100000, CFGFLAG_CLIENT|CFGFLAG_SAVE, "HTTP downloads: Set low speed limit in bytes per second (0 to disable)")
MACRO_CONFIG_INT(ClHTTPLowSpeedTime, cl_http_low_speed_time, 5, 0, 100000, CFGFLAG_CLIENT|CFGFLAG_SAVE, "HTTP downloads: Set low speed limit time period (0 to disable)")
// server // server
MACRO_CONFIG_INT(SvWarmup, sv_warmup, 0, 0, 0, CFGFLAG_SERVER, "Number of seconds to do warmup before round starts") MACRO_CONFIG_INT(SvWarmup, sv_warmup, 0, 0, 0, CFGFLAG_SERVER, "Number of seconds to do warmup before round starts")
MACRO_CONFIG_STR(SvMotd, sv_motd, 900, "", CFGFLAG_SERVER, "Message of the day to display for the clients") MACRO_CONFIG_STR(SvMotd, sv_motd, 900, "", CFGFLAG_SERVER, "Message of the day to display for the clients")