#pragma once #include #include #include #include namespace DB { /** Quota for resources consumption for specific interval. * Used to limit resource usage by user. * Quota is applied "softly" - could be slightly exceed, because it is checked usually only on each block of processed data. * Accumulated values are not persisted and are lost on server restart. * Quota is local to server, * but for distributed queries, accumulated values for read rows and bytes * are collected from all participating servers and accumulated locally. */ struct Quota : public IAccessEntity { /// Amount of resources available to consume for each duration. struct Limits { std::optional max[static_cast(QuotaType::MAX)]; std::chrono::seconds duration = std::chrono::seconds::zero(); /// Intervals can be randomized (to avoid DoS if intervals for many users end at one time). bool randomize_interval = false; friend bool operator ==(const Limits & lhs, const Limits & rhs); friend bool operator !=(const Limits & lhs, const Limits & rhs) { return !(lhs == rhs); } }; std::vector all_limits; /// Key to share quota consumption. /// Users with the same key share the same amount of resource. QuotaKeyType key_type = QuotaKeyType::NONE; /// Which roles or users should use this quota. RolesOrUsersSet to_roles; bool equal(const IAccessEntity & other) const override; std::shared_ptr clone() const override { return cloneImpl(); } static constexpr const auto TYPE = AccessEntityType::QUOTA; AccessEntityType getType() const override { return TYPE; } }; using QuotaPtr = std::shared_ptr; }