-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHttpConnectionManager.cc
More file actions
105 lines (87 loc) · 2.82 KB
/
HttpConnectionManager.cc
File metadata and controls
105 lines (87 loc) · 2.82 KB
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
/*
* Copyright (C) 2021 Frank Mertens.
*
* Distribution and use is allowed under the terms of the GNU General Public License version 3
* (see CoreComponents/LICENSE-gpl-3.0).
*
*/
#include <cc/HttpConnectionManager>
#include <cc/httpDebug>
#include <cc/System>
namespace cc {
struct HttpConnectionManager::State: public Object::State
{
State(const HttpServerConfig &nodeConfig):
nodeConfig_{nodeConfig},
serviceWindow_{nodeConfig.serviceWindow()},
connectionLimit_{nodeConfig.connectionLimit()}
{
}
const HttpLoggingServiceInstance &errorLoggingInstance() const
{
return nodeConfig_.errorLoggingInstance();
}
void cycle()
{
HttpClientConnection visit;
while (closedConnections_.count() > 0) {
closedConnections_.popFront(&visit);
visits_.append(visit);
}
if (visits_.count() > 0)
{
double t1 = System::now() - serviceWindow_;
while (visits_.count() > 0)
{
if (visits_.at(0).departureTime() >= t1) break;
HttpClientConnection visit = visits_.first();
visits_.popFront();
uint64_t origin = visit.peerAddress().networkPrefix();
Locator target;
if (!connectionCounts_.find(origin, &target)) continue;
if (connectionCounts_.at(target).value() == 1) connectionCounts_.removeAt(target);
else --connectionCounts_.at(target).value();
}
}
}
bool accept(InOut<HttpClientConnection> client)
{
uint64_t origin = client->peerAddress().networkPrefix();
Locator target;
if (!connectionCounts_.insert(origin, 1, &target)) {
if (connectionLimit_ > 0) {
if (connectionCounts_.at(target).value() >= connectionLimit_)
return false;
}
++connectionCounts_.at(target).value();
}
client->setPriority(connectionCounts_.at(target).value() < 8 ? 0 : -connectionCounts_.at(target).value());
return true;
}
Channel<HttpClientConnection> closedConnections_;
HttpServerConfig nodeConfig_;
Map<uint64_t, int> connectionCounts_;
List<HttpClientConnection> visits_;
long serviceWindow_;
long connectionLimit_;
};
HttpConnectionManager::HttpConnectionManager(const HttpServerConfig &config):
Object{new State{config}}
{}
Channel<HttpClientConnection> &HttpConnectionManager::closedConnections()
{
return me().closedConnections_;
}
void HttpConnectionManager::cycle()
{
return me().cycle();
}
bool HttpConnectionManager::accept(InOut<HttpClientConnection> client)
{
return me().accept(client);
}
HttpConnectionManager::State &HttpConnectionManager::me()
{
return Object::me.as<State>();
}
} // namespace cc