Line data Source code
1 : #pragma once 2 : 3 : #include "envoy/matcher/matcher.h" 4 : 5 : #include "source/common/common/hash.h" 6 : 7 : namespace Envoy { 8 : namespace Extensions { 9 : namespace Matching { 10 : namespace InputMatchers { 11 : namespace ConsistentHashing { 12 : 13 : class Matcher : public Envoy::Matcher::InputMatcher { 14 : public: 15 : Matcher(uint32_t threshold, uint32_t modulo, uint64_t seed) 16 0 : : threshold_(threshold), modulo_(modulo), seed_(seed) {} 17 0 : bool match(const Envoy::Matcher::MatchingDataType& input) override { 18 : // Only match if the value is present. 19 0 : if (absl::holds_alternative<absl::monostate>(input)) { 20 0 : return false; 21 0 : } 22 : 23 : // Otherwise, match if (hash(input) % modulo) >= threshold. 24 0 : return HashUtil::xxHash64(absl::get<std::string>(input), seed_) % modulo_ >= threshold_; 25 0 : } 26 : 27 : private: 28 : const uint32_t threshold_; 29 : const uint32_t modulo_; 30 : const uint64_t seed_; 31 : }; 32 : } // namespace ConsistentHashing 33 : } // namespace InputMatchers 34 : } // namespace Matching 35 : } // namespace Extensions 36 : } // namespace Envoy