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
|
#pragma once
#include <library/cpp/actors/core/actor.h>
#include <deque>
namespace NActors {
static constexpr ui32 DEFAULT_INFLIGHT = 100;
class THandshakeBroker : public TActor<THandshakeBroker> {
private:
std::deque<TActorId> Waiting;
ui32 Capacity;
void Handle(TEvHandshakeBrokerTake::TPtr &ev) {
if (Capacity > 0) {
Capacity -= 1;
Send(ev->Sender, new TEvHandshakeBrokerPermit());
} else {
Waiting.push_back(ev->Sender);
}
}
void Handle(TEvHandshakeBrokerFree::TPtr& ev) {
Y_UNUSED(ev);
if (Capacity == 0 && !Waiting.empty()) {
Send(Waiting.front(), new TEvHandshakeBrokerPermit());
Waiting.pop_front();
} else {
Capacity += 1;
}
}
void PassAway() override {
while (!Waiting.empty()) {
Send(Waiting.front(), new TEvHandshakeBrokerPermit());
Waiting.pop_front();
}
TActor::PassAway();
}
public:
THandshakeBroker(ui32 inflightLimit = DEFAULT_INFLIGHT)
: TActor(&TThis::StateFunc)
, Capacity(inflightLimit)
{
}
static constexpr char ActorName[] = "HANDSHAKE_BROKER_ACTOR";
STFUNC(StateFunc) {
Y_UNUSED(ctx);
switch(ev->GetTypeRewrite()) {
hFunc(TEvHandshakeBrokerTake, Handle);
hFunc(TEvHandshakeBrokerFree, Handle);
cFunc(TEvents::TSystem::Poison, PassAway);
}
}
void Bootstrap() {
Become(&TThis::StateFunc);
};
};
inline IActor* CreateHandshakeBroker() {
return new THandshakeBroker();
}
inline TActorId MakeHandshakeBrokerOutId() {
char x[12] = {'I', 'C', 'H', 's', 'h', 'k', 'B', 'r', 'k', 'O', 'u', 't'};
return TActorId(0, TStringBuf(std::begin(x), std::end(x)));
}
inline TActorId MakeHandshakeBrokerInId() {
char x[12] = {'I', 'C', 'H', 's', 'h', 'k', 'B', 'r', 'k', 'r', 'I', 'n'};
return TActorId(0, TStringBuf(std::begin(x), std::end(x)));
}
};
|