|
| 1 | +// Copyright (c) 2024, Tencent Inc. |
| 2 | +// All rights reserved. |
| 3 | + |
| 4 | +#pragma once |
| 5 | + |
| 6 | +#include <string> |
| 7 | +#include <unordered_map> |
| 8 | + |
| 9 | +#include "trpc/log/trpc_log.h" |
| 10 | + |
| 11 | +namespace trpc::overload_control { |
| 12 | + |
| 13 | +/// @brief Factory of overload control strategy(as template T). |
| 14 | +template <class T> |
| 15 | +class OverloadControlFactory { |
| 16 | + public: |
| 17 | + /// @brief Singleton |
| 18 | + static OverloadControlFactory* GetInstance() { |
| 19 | + static OverloadControlFactory instance; |
| 20 | + return &instance; |
| 21 | + } |
| 22 | + |
| 23 | + /// @brief Can't construct by user. |
| 24 | + OverloadControlFactory(const OverloadControlFactory&) = delete; |
| 25 | + OverloadControlFactory& operator=(const OverloadControlFactory&) = delete; |
| 26 | + |
| 27 | + /// @brief Register the overload control strategy. |
| 28 | + /// @param obj strategy |
| 29 | + /// @note Non-thread-safe |
| 30 | + bool Register(const T& obj); |
| 31 | + |
| 32 | + /// @brief Get the overload control strategy by name |
| 33 | + /// @param name name of strategy |
| 34 | + /// @return strategy |
| 35 | + T Get(const std::string& name); |
| 36 | + |
| 37 | + /// @brief Get number of strategies. |
| 38 | + /// @return number of strategies |
| 39 | + /// @note Non-thread-safe |
| 40 | + size_t Size() const { return objs_map_.size(); } |
| 41 | + |
| 42 | + /// @brief Stop all of overload control strategies. |
| 43 | + // Mainly used to stop inner thread createdy by each strategy |
| 44 | + /// @note Non-thread-safe. |
| 45 | + void Stop(); |
| 46 | + |
| 47 | + /// @brief Destroy resource of overload control strategies. |
| 48 | + /// @note Non-thread-safe. |
| 49 | + void Destroy(); |
| 50 | + |
| 51 | + /// @brief Clear overload control strategies in this factory. |
| 52 | + /// @note Non-thread-safe. |
| 53 | + void Clear() { objs_map_.clear(); } |
| 54 | + |
| 55 | + private: |
| 56 | + OverloadControlFactory() = default; |
| 57 | + |
| 58 | + private: |
| 59 | + // strategies mapping(name->stratege obj) |
| 60 | + std::unordered_map<std::string, T> objs_map_; |
| 61 | +}; |
| 62 | + |
| 63 | +template <class T> |
| 64 | +bool OverloadControlFactory<T>::Register(const T& obj) { |
| 65 | + if (!obj) { |
| 66 | + TRPC_FMT_ERROR("register object is nullptr"); |
| 67 | + return false; |
| 68 | + } |
| 69 | + if (Get(obj->Name())) { |
| 70 | + return false; |
| 71 | + } |
| 72 | + if (obj->Init()) { |
| 73 | + objs_map_.emplace(obj->Name(), obj); |
| 74 | + return true; |
| 75 | + } |
| 76 | + TRPC_FMT_ERROR("{} is `Init` failed ", obj->Name()); |
| 77 | + return false; |
| 78 | +} |
| 79 | + |
| 80 | +template <class T> |
| 81 | +T OverloadControlFactory<T>::Get(const std::string& name) { |
| 82 | + T obj = nullptr; |
| 83 | + auto iter = objs_map_.find(name); |
| 84 | + if (iter != objs_map_.end()) { |
| 85 | + obj = iter->second; |
| 86 | + } |
| 87 | + return obj; |
| 88 | +} |
| 89 | + |
| 90 | +template <class T> |
| 91 | +void OverloadControlFactory<T>::Stop() { |
| 92 | + for (auto& obj : objs_map_) { |
| 93 | + obj.second->Stop(); |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +template <class T> |
| 98 | +void OverloadControlFactory<T>::Destroy() { |
| 99 | + for (auto& obj : objs_map_) { |
| 100 | + obj.second->Destroy(); |
| 101 | + } |
| 102 | + Clear(); |
| 103 | +} |
| 104 | + |
| 105 | +} // namespace trpc::overload_control |
0 commit comments