1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
#include "storage/pinger.hpp"
#include "platform/http_client.hpp"
#include "platform/preferred_languages.hpp"
#include "base/assert.hpp"
#include "base/logging.hpp"
#include "base/stl_helpers.hpp"
#include "base/worker_thread.hpp"
#include "3party/Alohalytics/src/alohalytics.h"
#include <sstream>
#include <utility>
using namespace std;
namespace
{
auto constexpr kTimeoutInSeconds = 5.0;
void DoPing(string const & url, size_t index, vector<string> & readyUrls)
{
if (url.empty())
{
ASSERT(false, ("Metaserver returned an empty url."));
return;
}
platform::HttpClient request(url);
request.SetHttpMethod("HEAD");
request.SetTimeout(kTimeoutInSeconds);
if (request.RunHttpRequest() && !request.WasRedirected() && request.ErrorCode() == 200)
{
readyUrls[index] = url;
}
else
{
ostringstream ost;
ost << "Request to server " << url << " failed. Code = " << request.ErrorCode()
<< ", redirection = " << request.WasRedirected();
LOG(LINFO, (ost.str()));
}
}
void SendStatistics(size_t serversLeft)
{
alohalytics::Stats::Instance().LogEvent(
"Downloader_ServerList_check",
{{"lang", languages::GetCurrentNorm()}, {"servers", to_string(serversLeft)}});
}
} // namespace
namespace storage
{
// static
void Pinger::Ping(vector<string> const & urls, Pinger::Pong const & pong)
{
auto const size = urls.size();
CHECK_GREATER(size, 0, ());
vector<string> readyUrls(size);
{
base::WorkerThread t(size);
for (size_t i = 0; i < size; ++i)
t.Push([url = urls[i], &readyUrls, i] { DoPing(url, i, readyUrls); });
t.Shutdown(base::WorkerThread::Exit::ExecPending);
}
base::EraseIf(readyUrls, [](auto const & url) { return url.empty(); });
SendStatistics(readyUrls.size());
pong(move(readyUrls));
}
} // namespace storage
|