| 187 | } |
| 188 | |
| 189 | void CChooseMaster::CJob::Run() |
| 190 | { |
| 191 | // Check masters in a random order. |
| 192 | int aRandomized[MAX_URLS] = {0}; |
| 193 | for(int i = 0; i < m_pData->m_NumUrls; i++) |
| 194 | { |
| 195 | aRandomized[i] = i; |
| 196 | } |
| 197 | // https://en.wikipedia.org/w/index.php?title=Fisher%E2%80%93Yates_shuffle&oldid=1002922479#The_modern_algorithm |
| 198 | // The equivalent version. |
| 199 | for(int i = 0; i <= m_pData->m_NumUrls - 2; i++) |
| 200 | { |
| 201 | int j = i + secure_rand_below(m_pData->m_NumUrls - i); |
| 202 | std::swap(aRandomized[i], aRandomized[j]); |
| 203 | } |
| 204 | // Do a HEAD request to ensure that a connection is established and |
| 205 | // then do a GET request to check how fast we can get the server list. |
| 206 | // |
| 207 | // 10 seconds connection timeout, lower than 8KB/s for 10 seconds to |
| 208 | // fail. |
| 209 | CTimeout Timeout{10000, 0, 8000, 10}; |
| 210 | int aTimeMs[MAX_URLS]; |
| 211 | int aAgeS[MAX_URLS]; |
| 212 | for(int i = 0; i < m_pData->m_NumUrls; i++) |
| 213 | { |
| 214 | aTimeMs[i] = -1; |
| 215 | aAgeS[i] = SanitizeAge({}); |
| 216 | const char *pUrl = m_pData->m_aaUrls[aRandomized[i]]; |
| 217 | std::shared_ptr<CHttpRequest> pHead = HttpHead(pUrl); |
| 218 | pHead->Timeout(Timeout); |
| 219 | pHead->LogProgress(HTTPLOG::FAILURE); |
| 220 | { |
| 221 | const CLockScope LockScope(m_Lock); |
| 222 | m_pHead = pHead; |
| 223 | } |
| 224 | |
| 225 | m_pParent->m_pHttp->Run(pHead); |
| 226 | pHead->Wait(); |
| 227 | if(pHead->State() == EHttpState::ABORTED || State() == IJob::STATE_ABORTED) |
| 228 | { |
| 229 | log_debug("serverbrowser_http", "master chooser aborted"); |
| 230 | return; |
| 231 | } |
| 232 | if(pHead->State() != EHttpState::DONE) |
| 233 | { |
| 234 | continue; |
| 235 | } |
| 236 | |
| 237 | auto StartTime = time_get_nanoseconds(); |
| 238 | std::shared_ptr<CHttpRequest> pGet = HttpGet(pUrl); |
| 239 | pGet->Timeout(Timeout); |
| 240 | pGet->LogProgress(HTTPLOG::FAILURE); |
| 241 | { |
| 242 | const CLockScope LockScope(m_Lock); |
| 243 | m_pGet = pGet; |
| 244 | } |
| 245 | |
| 246 | m_pParent->m_pHttp->Run(pGet); |
no test coverage detected