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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
|
#include "rpc.h"
#include "rq.h"
#include "multi.h"
#include "location.h"
#include <library/cpp/threading/thread_local/thread_local.h>
#include <util/generic/hash.h>
#include <util/thread/factory.h>
#include <util/system/spinlock.h>
using namespace NNeh;
namespace {
typedef std::pair<TString, IServiceRef> TServiceDescr;
typedef TVector<TServiceDescr> TServicesBase;
class TServices: public TServicesBase, public TThrRefBase, public IOnRequest {
typedef THashMap<TStringBuf, IServiceRef> TSrvs;
struct TVersionedServiceMap {
TSrvs Srvs;
i64 Version = 0;
};
struct TFunc: public IThreadFactory::IThreadAble {
inline TFunc(TServices* parent)
: Parent(parent)
{
}
void DoExecute() override {
TThread::SetCurrentThreadName("NehTFunc");
TVersionedServiceMap mp;
while (true) {
IRequestRef req = Parent->RQ_->Next();
if (!req) {
break;
}
Parent->ServeRequest(mp, req);
}
Parent->RQ_->Schedule(nullptr);
}
TServices* Parent;
};
public:
inline TServices()
: RQ_(CreateRequestQueue())
{
}
inline TServices(TCheck check)
: RQ_(CreateRequestQueue())
, C_(check)
{
}
inline ~TServices() override {
LF_.Destroy();
}
inline void Add(const TString& service, IServiceRef srv) {
TGuard<TSpinLock> guard(L_);
push_back(std::make_pair(service, srv));
AtomicIncrement(SelfVersion_);
}
inline void Listen() {
Y_ENSURE(!HasLoop_ || !*HasLoop_);
HasLoop_ = false;
RR_ = MultiRequester(ListenAddrs(), this);
}
inline void Loop(size_t threads) {
Y_ENSURE(!HasLoop_ || *HasLoop_);
HasLoop_ = true;
TIntrusivePtr<TServices> self(this);
IRequesterRef rr = MultiRequester(ListenAddrs(), this);
TFunc func(this);
typedef TAutoPtr<IThreadFactory::IThread> IThreadRef;
TVector<IThreadRef> thrs;
for (size_t i = 1; i < threads; ++i) {
thrs.push_back(SystemThreadFactory()->Run(&func));
}
func.Execute();
for (size_t i = 0; i < thrs.size(); ++i) {
thrs[i]->Join();
}
RQ_->Clear();
}
inline void ForkLoop(size_t threads) {
Y_ENSURE(!HasLoop_ || *HasLoop_);
HasLoop_ = true;
//here we can have trouble with binding port(s), so expect exceptions
IRequesterRef rr = MultiRequester(ListenAddrs(), this);
LF_.Reset(new TLoopFunc(this, threads, rr));
}
inline void Stop() {
RQ_->Schedule(nullptr);
}
inline void SyncStopFork() {
Stop();
if (LF_) {
LF_->SyncStop();
}
RQ_->Clear();
LF_.Destroy();
}
void OnRequest(IRequestRef req) override {
if (C_) {
if (auto error = C_(req)) {
req->SendError(*error);
return;
}
}
if (!*HasLoop_) {
ServeRequest(LocalMap_.GetRef(), req);
} else {
RQ_->Schedule(req);
}
}
private:
class TLoopFunc: public TFunc {
public:
TLoopFunc(TServices* parent, size_t threads, IRequesterRef& rr)
: TFunc(parent)
, RR_(rr)
{
T_.reserve(threads);
try {
for (size_t i = 0; i < threads; ++i) {
T_.push_back(SystemThreadFactory()->Run(this));
}
} catch (...) {
//paranoid mode on
SyncStop();
throw;
}
}
~TLoopFunc() override {
try {
SyncStop();
} catch (...) {
Cdbg << TStringBuf("neh rpc ~loop_func: ") << CurrentExceptionMessage() << Endl;
}
}
void SyncStop() {
if (!T_) {
return;
}
Parent->Stop();
for (size_t i = 0; i < T_.size(); ++i) {
T_[i]->Join();
}
T_.clear();
}
private:
typedef TAutoPtr<IThreadFactory::IThread> IThreadRef;
TVector<IThreadRef> T_;
IRequesterRef RR_;
};
inline void ServeRequest(TVersionedServiceMap& mp, IRequestRef req) {
if (!req) {
return;
}
const TStringBuf name = req->Service();
TSrvs::const_iterator it = mp.Srvs.find(name);
if (Y_UNLIKELY(it == mp.Srvs.end())) {
if (UpdateServices(mp.Srvs, mp.Version)) {
it = mp.Srvs.find(name);
}
}
if (Y_UNLIKELY(it == mp.Srvs.end())) {
it = mp.Srvs.find(TStringBuf("*"));
}
if (Y_UNLIKELY(it == mp.Srvs.end())) {
req->SendError(IRequest::NotExistService);
} else {
try {
it->second->ServeRequest(req);
} catch (...) {
Cdbg << CurrentExceptionMessage() << Endl;
}
}
}
inline bool UpdateServices(TSrvs& srvs, i64& version) const {
if (AtomicGet(SelfVersion_) == version) {
return false;
}
srvs.clear();
TGuard<TSpinLock> guard(L_);
for (const auto& it : *this) {
srvs[TParsedLocation(it.first).Service] = it.second;
}
version = AtomicGet(SelfVersion_);
return true;
}
inline TListenAddrs ListenAddrs() const {
TListenAddrs addrs;
{
TGuard<TSpinLock> guard(L_);
for (const auto& it : *this) {
addrs.push_back(it.first);
}
}
return addrs;
}
TSpinLock L_;
IRequestQueueRef RQ_;
THolder<TLoopFunc> LF_;
TAtomic SelfVersion_ = 1;
TCheck C_;
NThreading::TThreadLocalValue<TVersionedServiceMap> LocalMap_;
IRequesterRef RR_;
TMaybe<bool> HasLoop_;
};
class TServicesFace: public IServices {
public:
inline TServicesFace()
: S_(new TServices())
{
}
inline TServicesFace(TCheck check)
: S_(new TServices(check))
{
}
void DoAdd(const TString& service, IServiceRef srv) override {
S_->Add(service, srv);
}
void Loop(size_t threads) override {
S_->Loop(threads);
}
void ForkLoop(size_t threads) override {
S_->ForkLoop(threads);
}
void SyncStopFork() override {
S_->SyncStopFork();
}
void Stop() override {
S_->Stop();
}
void Listen() override {
S_->Listen();
}
private:
TIntrusivePtr<TServices> S_;
};
}
IServiceRef NNeh::Wrap(const TServiceFunction& func) {
struct TWrapper: public IService {
inline TWrapper(const TServiceFunction& f)
: F(f)
{
}
void ServeRequest(const IRequestRef& request) override {
F(request);
}
TServiceFunction F;
};
return new TWrapper(func);
}
IServicesRef NNeh::CreateLoop() {
return new TServicesFace();
}
IServicesRef NNeh::CreateLoop(TCheck check) {
return new TServicesFace(check);
}
|