forked from newton-blockchain/ton
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoken-manager.cpp
102 lines (86 loc) · 3.3 KB
/
token-manager.cpp
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
/*
This file is part of TON Blockchain Library.
TON Blockchain Library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
TON Blockchain Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with TON Blockchain Library. If not, see <http://www.gnu.org/licenses/>.
Copyright 2017-2020 Telegram Systems LLP
*/
#include "token-manager.h"
namespace ton {
namespace validator {
void TokenManager::get_download_token(size_t download_size, td::uint32 priority, td::Timestamp timeout,
td::Promise<std::unique_ptr<DownloadToken>> promise) {
if (free_priority_tokens_ > 0 && priority > 0) {
--free_priority_tokens_;
promise.set_value(gen_token(download_size, priority));
return;
}
if (free_tokens_ > 0) {
--free_tokens_;
promise.set_value(gen_token(download_size, priority));
return;
}
pending_.emplace(PendingPromiseKey{download_size, priority, seqno_++}, PendingPromise{timeout, std::move(promise)});
}
void TokenManager::download_token_cleared(size_t download_size, td::uint32 priority) {
(priority ? free_priority_tokens_ : free_tokens_)++;
if (free_priority_tokens_ > max_priority_tokens_) {
free_priority_tokens_--;
free_tokens_++;
}
for (auto it = pending_.begin(); it != pending_.end();) {
if (it->first.priority && (free_tokens_ || free_priority_tokens_)) {
it->second.promise.set_value(gen_token(download_size, priority));
auto it2 = it++;
pending_.erase(it2);
if (free_priority_tokens_ > 0) {
free_priority_tokens_--;
} else {
free_tokens_--;
}
} else if (!it->first.priority && free_tokens_) {
it->second.promise.set_value(gen_token(download_size, priority));
auto it2 = it++;
pending_.erase(it2);
free_tokens_--;
} else {
break;
}
}
}
void TokenManager::alarm() {
for (auto it = pending_.begin(); it != pending_.end(); it++) {
if (it->second.timeout.is_in_past()) {
it->second.promise.set_error(td::Status::Error(ErrorCode::timeout, "timeout in wait download token"));
auto it2 = it++;
pending_.erase(it2);
} else {
it++;
}
}
}
std::unique_ptr<DownloadToken> TokenManager::gen_token(size_t download_size, td::uint32 priority) {
class Token : public DownloadToken {
public:
Token(size_t download_size, td::uint32 priority, td::actor::ActorId<TokenManager> manager)
: download_size_(download_size), priority_(priority), manager_(manager) {
}
~Token() override {
td::actor::send_closure(manager_, &TokenManager::download_token_cleared, download_size_, priority_);
}
private:
size_t download_size_;
td::uint32 priority_;
td::actor::ActorId<TokenManager> manager_;
};
return std::make_unique<Token>(download_size, priority, actor_id(this));
}
} // namespace validator
} // namespace ton