github.com/muhammadn/cortex@v1.9.1-0.20220510110439-46bb7000d03d/pkg/distributor/ingestion_rate_strategy.go (about) 1 package distributor 2 3 import ( 4 "github.com/grafana/dskit/limiter" 5 "golang.org/x/time/rate" 6 7 "github.com/cortexproject/cortex/pkg/util/validation" 8 ) 9 10 // ReadLifecycler represents the read interface to the lifecycler. 11 type ReadLifecycler interface { 12 HealthyInstancesCount() int 13 } 14 15 type localStrategy struct { 16 limits *validation.Overrides 17 } 18 19 func newLocalIngestionRateStrategy(limits *validation.Overrides) limiter.RateLimiterStrategy { 20 return &localStrategy{ 21 limits: limits, 22 } 23 } 24 25 func (s *localStrategy) Limit(tenantID string) float64 { 26 return s.limits.IngestionRate(tenantID) 27 } 28 29 func (s *localStrategy) Burst(tenantID string) int { 30 return s.limits.IngestionBurstSize(tenantID) 31 } 32 33 type globalStrategy struct { 34 limits *validation.Overrides 35 ring ReadLifecycler 36 } 37 38 func newGlobalIngestionRateStrategy(limits *validation.Overrides, ring ReadLifecycler) limiter.RateLimiterStrategy { 39 return &globalStrategy{ 40 limits: limits, 41 ring: ring, 42 } 43 } 44 45 func (s *globalStrategy) Limit(tenantID string) float64 { 46 numDistributors := s.ring.HealthyInstancesCount() 47 48 if numDistributors == 0 { 49 return s.limits.IngestionRate(tenantID) 50 } 51 52 return s.limits.IngestionRate(tenantID) / float64(numDistributors) 53 } 54 55 func (s *globalStrategy) Burst(tenantID string) int { 56 // The meaning of burst doesn't change for the global strategy, in order 57 // to keep it easier to understand for users / operators. 58 return s.limits.IngestionBurstSize(tenantID) 59 } 60 61 type infiniteStrategy struct{} 62 63 func newInfiniteIngestionRateStrategy() limiter.RateLimiterStrategy { 64 return &infiniteStrategy{} 65 } 66 67 func (s *infiniteStrategy) Limit(tenantID string) float64 { 68 return float64(rate.Inf) 69 } 70 71 func (s *infiniteStrategy) Burst(tenantID string) int { 72 // Burst is ignored when limit = rate.Inf 73 return 0 74 }