#pragma once #include #include #include #include #include #include #include #include #include #include namespace DB { struct QuotaUsage; /// Instances of `EnabledQuota` are used to track resource consumption. class EnabledQuota : public boost::noncopyable { public: struct Params { UUID user_id; String user_name; boost::container::flat_set enabled_roles; Poco::Net::IPAddress client_address; String forwarded_address; String client_key; auto toTuple() const { return std::tie(user_id, enabled_roles, user_name, client_address, forwarded_address, client_key); } friend bool operator ==(const Params & lhs, const Params & rhs) { return lhs.toTuple() == rhs.toTuple(); } friend bool operator !=(const Params & lhs, const Params & rhs) { return !(lhs == rhs); } friend bool operator <(const Params & lhs, const Params & rhs) { return lhs.toTuple() < rhs.toTuple(); } friend bool operator >(const Params & lhs, const Params & rhs) { return rhs < lhs; } friend bool operator <=(const Params & lhs, const Params & rhs) { return !(rhs < lhs); } friend bool operator >=(const Params & lhs, const Params & rhs) { return !(lhs < rhs); } }; ~EnabledQuota(); /// Tracks resource consumption. If the quota exceeded and `check_exceeded == true`, throws an exception. void used(QuotaType quota_type, QuotaValue value, bool check_exceeded = true) const; void used(const std::pair & usage1, bool check_exceeded = true) const; void used(const std::pair & usage1, const std::pair & usage2, bool check_exceeded = true) const; void used(const std::pair & usage1, const std::pair & usage2, const std::pair & usage3, bool check_exceeded = true) const; void used(const std::vector> & usages, bool check_exceeded = true) const; /// Checks if the quota exceeded. If so, throws an exception. void checkExceeded() const; void checkExceeded(QuotaType quota_type) const; void reset(QuotaType quota_type) const; /// Returns the information about quota consumption. std::optional getUsage() const; /// Returns an instance of EnabledQuota which is never exceeded. static std::shared_ptr getUnlimitedQuota(); private: friend class QuotaCache; explicit EnabledQuota(const Params & params_); EnabledQuota() {} /// NOLINT const String & getUserName() const { return params.user_name; } struct Interval { mutable std::atomic used[static_cast(QuotaType::MAX)]; QuotaValue max[static_cast(QuotaType::MAX)]; std::chrono::seconds duration = std::chrono::seconds::zero(); bool randomize_interval = false; mutable std::atomic end_of_interval; Interval(std::chrono::seconds duration_, bool randomize_interval_, std::chrono::system_clock::time_point current_time_); Interval(const Interval & src) { *this = src; } Interval & operator =(const Interval & src); std::chrono::system_clock::time_point getEndOfInterval(std::chrono::system_clock::time_point current_time) const; std::chrono::system_clock::time_point getEndOfInterval(std::chrono::system_clock::time_point current_time, bool & counters_were_reset) const; }; struct Intervals { std::vector intervals; std::optional quota_id; String quota_name; String quota_key; std::optional getUsage(std::chrono::system_clock::time_point current_time) const; }; struct Impl; const Params params; boost::atomic_shared_ptr intervals; /// atomically changed by QuotaUsageManager }; }