]>
Commit | Line | Data |
---|---|---|
f67539c2 TL |
1 | /* |
2 | * Copyright (c) 2017 Uber Technologies, Inc. | |
3 | * | |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | * you may not use this file except in compliance with the License. | |
6 | * You may obtain a copy of the License at | |
7 | * | |
8 | * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | * | |
10 | * Unless required by applicable law or agreed to in writing, software | |
11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | * See the License for the specific language governing permissions and | |
14 | * limitations under the License. | |
15 | */ | |
16 | ||
17 | #ifndef JAEGERTRACING_BAGGAGE_REMOTERESTRICTIONMANAGER_H | |
18 | #define JAEGERTRACING_BAGGAGE_REMOTERESTRICTIONMANAGER_H | |
19 | ||
20 | #include <chrono> | |
21 | #include <condition_variable> | |
22 | #include <mutex> | |
23 | #include <string> | |
24 | #include <thread> | |
25 | #include <unordered_map> | |
26 | ||
27 | #include "jaegertracing/Compilers.h" | |
28 | ||
29 | #include "jaegertracing/Logging.h" | |
30 | #include "jaegertracing/baggage/Restriction.h" | |
31 | #include "jaegertracing/baggage/RestrictionManager.h" | |
32 | #include "jaegertracing/metrics/Metrics.h" | |
33 | #include "jaegertracing/net/IPAddress.h" | |
34 | #include "jaegertracing/net/URI.h" | |
35 | #include "jaegertracing/thrift-gen/BaggageRestrictionManager.h" | |
36 | ||
37 | namespace jaegertracing { | |
38 | namespace baggage { | |
39 | ||
40 | class RemoteRestrictionManager : public RestrictionManager { | |
41 | public: | |
42 | using Clock = std::chrono::steady_clock; | |
43 | using KeyRestrictionMap = std::unordered_map<std::string, Restriction>; | |
44 | ||
45 | static constexpr auto kDefaultDenyBaggageOnInitializationFailure = false; | |
46 | static constexpr auto kDefaultMaxValueLength = | |
47 | DefaultRestrictionManager::kDefaultMaxValueLength; | |
48 | ||
49 | static Clock::duration defaultRefreshInterval() | |
50 | { | |
51 | return std::chrono::minutes(1); | |
52 | } | |
53 | ||
54 | RemoteRestrictionManager(const std::string& serviceName, | |
55 | const std::string& hostPort, | |
56 | bool denyBaggageOnInitializationFailure, | |
57 | const Clock::duration& refreshInterval, | |
58 | logging::Logger& logger, | |
59 | metrics::Metrics& metrics); | |
60 | ||
61 | ~RemoteRestrictionManager() { close(); } | |
62 | ||
63 | Restriction getRestriction(const std::string& /* service */, | |
64 | const std::string& key) override; | |
65 | ||
66 | void close() noexcept override; | |
67 | ||
68 | const Clock::duration& refreshInterval() const { return _refreshInterval; } | |
69 | ||
70 | private: | |
71 | void poll() noexcept; | |
72 | ||
73 | void updateRestrictions(const net::URI& remoteURI) noexcept; | |
74 | ||
75 | std::string _serviceName; | |
76 | net::IPAddress _serverAddress; | |
77 | bool _denyBaggageOnInitializationFailure; | |
78 | Clock::duration _refreshInterval; | |
79 | logging::Logger& _logger; | |
80 | metrics::Metrics& _metrics; | |
81 | KeyRestrictionMap _restrictions; | |
82 | bool _running; | |
83 | bool _initialized; | |
84 | std::thread _thread; | |
85 | std::condition_variable _cv; | |
86 | std::mutex _mutex; | |
87 | }; | |
88 | ||
89 | } // namespace baggage | |
90 | } // namespace jaegertracing | |
91 | ||
92 | #endif // JAEGERTRACING_BAGGAGE_REMOTERESTRICTIONMANAGER_H |