blob: 944ba4cfec227acd6459a92489574221d3c87ad8 (
plain) (
blame)
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
|
#pragma once
#include <string.h>
#include <util/generic/ptr.h>
#include <util/generic/singleton.h>
#include <util/generic/string.h>
#include <library/cpp/deprecated/atomic/atomic.h>
#include <util/thread/lfqueue.h>
#include "http_common.h"
namespace NNeh {
namespace NHttp2 {
// TConn must be refcounted and contain methods:
// void SetCached(bool) noexcept
// bool IsValid() const noexcept
// void Close() noexcept
template <class TConn>
class TConnCache {
struct TCounter : TAtomicCounter {
inline void IncCount(const TConn* const&) {
Inc();
}
inline void DecCount(const TConn* const&) {
Dec();
}
};
public:
typedef TIntrusivePtr<TConn> TConnRef;
class TConnList: public TLockFreeQueue<TConn*, TCounter> {
public:
~TConnList() {
Clear();
}
inline void Clear() {
TConn* conn;
while (this->Dequeue(&conn)) {
conn->Close();
conn->UnRef();
}
}
inline size_t Size() {
return this->GetCounter().Val();
}
};
inline void Put(TConnRef& conn, size_t addrId) {
conn->SetCached(true);
ConnList(addrId).Enqueue(conn.Get());
conn->Ref();
Y_UNUSED(conn.Release());
CachedConn_.Inc();
}
bool Get(TConnRef& conn, size_t addrId) {
TConnList& connList = ConnList(addrId);
TConn* connTmp;
while (connList.Dequeue(&connTmp)) {
connTmp->SetCached(false);
CachedConn_.Dec();
if (connTmp->IsValid()) {
TConnRef(connTmp).Swap(conn);
connTmp->DecRef();
return true;
} else {
connTmp->UnRef();
}
}
return false;
}
inline size_t Size() const noexcept {
return CachedConn_.Val();
}
inline size_t Validate(size_t addrId) {
TConnList& cl = Lst_.Get(addrId);
return Validate(cl);
}
//close/remove part of the connections from cache
size_t Purge(size_t addrId, size_t frac256) {
TConnList& cl = Lst_.Get(addrId);
size_t qsize = cl.Size();
if (!qsize) {
return 0;
}
size_t purgeCounter = ((qsize * frac256) >> 8);
if (!purgeCounter && qsize >= 2) {
purgeCounter = 1;
}
size_t pc = 0;
{
TConn* conn;
while (purgeCounter-- && cl.Dequeue(&conn)) {
conn->SetCached(false);
if (conn->IsValid()) {
conn->Close();
}
CachedConn_.Dec();
conn->UnRef();
++pc;
}
}
pc += Validate(cl);
return pc;
}
private:
inline TConnList& ConnList(size_t addrId) {
return Lst_.Get(addrId);
}
inline size_t Validate(TConnList& cl) {
size_t pc = 0;
size_t nc = cl.Size();
TConn* conn;
while (nc-- && cl.Dequeue(&conn)) {
if (conn->IsValid()) {
cl.Enqueue(conn);
} else {
++pc;
conn->SetCached(false);
CachedConn_.Dec();
conn->UnRef();
}
}
return pc;
}
NNeh::NHttp::TLockFreeSequence<TConnList> Lst_;
TAtomicCounter CachedConn_;
};
}
}
|