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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
|
#include "client.h"
#include "request.h"
#include <library/cpp/coroutine/dns/cache.h>
#include <library/cpp/coroutine/dns/coro.h>
#include <library/cpp/coroutine/dns/helpers.h>
#include <library/cpp/coroutine/engine/condvar.h>
#include <library/cpp/coroutine/engine/impl.h>
#include <library/cpp/coroutine/engine/network.h>
#include <library/cpp/coroutine/util/pipeque.h>
#include <library/cpp/http/client/ssl/sslsock.h>
#include <library/cpp/http/client/fetch/coctx.h>
#include <library/cpp/http/client/fetch/codes.h>
#include <library/cpp/http/client/fetch/cosocket.h>
#include <library/cpp/http/client/fetch/fetch_single.h>
#include <util/stream/output.h>
#include <util/thread/factory.h>
#include <util/system/event.h>
#include <util/system/spinlock.h>
#include <util/system/thread.h>
namespace NHttp {
namespace {
using namespace NHttpFetcher;
class TFetcher: private IThreadFactory::IThreadAble {
public:
TFetcher(const TClientOptions& options)
: Options_(options)
, FetchCoroutines(Max<size_t>(Options_.FetchCoroutines, 1))
, RequestsQueue_(true, false)
, Done_(false)
{
}
void Start() {
T_ = SystemThreadFactory()->Run(this);
}
void Stop() {
if (T_) {
for (size_t i = 0; i < FetchCoroutines; ++i) {
RequestsQueue_.Push(nullptr);
}
T_->Join();
T_.Reset();
}
}
~TFetcher() override {
Stop();
}
TFetchState FetchAsync(const TFetchRequestRef& req, NHttpFetcher::TCallBack cb) {
req->SetCallback(cb);
RequestsQueue_.Push(req);
return TFetchState(req);
}
static TFetcher* Instance() {
static struct TFetcherHolder {
TFetcherHolder() {
Fetcher.Start();
}
TFetcher Fetcher{{}};
} holder;
return &holder.Fetcher;
}
private:
void DoDrainLoop(TCont* c) {
TInstant nextDrain = TInstant::Now() + Options_.KeepAliveTimeout;
while (true) {
DrainMutex_.LockI(c);
while (true) {
if (Done_) {
DrainMutex_.UnLock();
// All sockets in the connection pool should be cleared
// on some active couroutine.
SocketPool_.Clear();
return;
}
if (DrainCond_.WaitD(c, &DrainMutex_, nextDrain) != 0) {
// In case of timeout the mutex will be in unlocked state.
break;
}
}
SocketPool_.Drain(Options_.KeepAliveTimeout);
nextDrain = TInstant::Now() + Options_.KeepAliveTimeout;
}
}
void DoFetchLoop(TCont* c) {
while (true) {
if (NCoro::PollI(c, RequestsQueue_.PopFd(), CONT_POLL_READ) == 0) {
TFetchRequestRef req;
if (RequestsQueue_.Pop(&req)) {
if (!req) {
DrainMutex_.LockI(c);
const auto wasDone = Done_;
Done_ = true;
DrainMutex_.UnLock();
if (!wasDone) {
DrainCond_.Signal();
}
break;
}
if (req->IsCancelled()) {
auto result = MakeIntrusive<TResult>(req->GetRequestImpl()->Url, FETCH_CANCELLED);
req->OnResponse(result);
continue;
}
try {
while (true) {
auto getConnectionPool = [&] () -> TSocketPool* {
if (!Options_.KeepAlive || req->GetForceReconnect()) {
return nullptr;
}
return &SocketPool_;
};
auto sleep = req->OnResponse(
FetchSingleImpl(req->GetRequestImpl(), getConnectionPool()));
if (!req->IsValid()) {
break;
}
if (sleep != TDuration::Zero()) {
c->SleepT(sleep);
}
}
} catch (...) {
req->SetException(std::current_exception());
}
}
}
}
}
void DoExecute() override {
// Executor must be initialized in the same thread that will use it
// for fibers to work correctly on windows
TContExecutor executor(Options_.ExecutorStackSize);
TThread::SetCurrentThreadName(Options_.Name.c_str());
NAsyncDns::TOptions dnsOpts;
dnsOpts.SetMaxRequests(200);
NAsyncDns::TContResolver resolver(&executor, dnsOpts);
THolder<NAsyncDns::TContDnsCache> dnsCache;
if (Options_.DnsCacheLifetime != TDuration::Zero()) {
NAsyncDns::TCacheOptions cacheOptions;
cacheOptions.SetEntryLifetime(Options_.DnsCacheLifetime);
dnsCache = MakeHolder<NAsyncDns::TContDnsCache>(&executor, cacheOptions);
}
TCoCtxSetter ctxSetter(&executor, &resolver, dnsCache.Get());
for (size_t i = 0; i < FetchCoroutines; ++i) {
executor.Create<TFetcher, &TFetcher::DoFetchLoop>(this, "fetch_loop");
}
if (Options_.KeepAlive) {
executor.Create<TFetcher, &TFetcher::DoDrainLoop>(this, "drain_loop");
}
executor.Execute();
executor.Abort();
}
private:
using IThreadRef = THolder<IThreadFactory::IThread>;
const TClientOptions Options_;
const size_t FetchCoroutines;
TContCondVar DrainCond_;
TContMutex DrainMutex_;
TSocketPool SocketPool_;
/// Queue of incoming requests.
TPipeQueue<TFetchRequestRef> RequestsQueue_;
bool Done_;
IThreadRef T_;
};
}
class TFetchClient::TImpl: public TFetcher {
public:
inline TImpl(const TClientOptions& options)
: TFetcher(options)
{
}
};
TFetchClient::TFetchClient(const TClientOptions& options)
: Impl_(new TImpl(options))
{
Impl_->Start();
}
TFetchClient::~TFetchClient() {
Impl_->Stop();
}
TFetchState TFetchClient::Fetch(const TFetchQuery& query, NHttpFetcher::TCallBack cb) {
return Impl_->FetchAsync(TFetchRequest::FromQuery(query), cb);
}
TResultRef Fetch(const TFetchQuery& query) {
return FetchAsync(query, NHttpFetcher::TCallBack()).Get();
}
TFetchState FetchAsync(const TFetchQuery& query, NHttpFetcher::TCallBack cb) {
return TFetcher::Instance()->FetchAsync(TFetchRequest::FromQuery(query), cb);
}
}
|