@@ -38,6 +38,7 @@ type Config struct {
3838 AvailableShardsStr string
3939 AvailableShards []uint32
4040 CHMaxShardConnsRatio int
41+ chutil.RateLimitConfig
4142}
4243
4344func (argv * Config ) ValidateConfig () error {
@@ -116,13 +117,37 @@ func (argv *Config) Bind(f *flag.FlagSet, defaultI config.Config) {
116117 f .StringVar (& argv .BlockedUsersS , "blocked-users" , "" , "comma-separated list of users that are blocked" )
117118 f .StringVar (& argv .AvailableShardsStr , "available-shards" , default_ .AvailableShardsStr , "comma-separated list of default shards for metrics when namespace doesn't specify shards" )
118119 f .IntVar (& argv .CHMaxShardConnsRatio , "clickhouse-max-shard-conns-ratio" , default_ .CHMaxShardConnsRatio , "maximum number of ClickHouse connections per shard (%)" )
120+
121+ f .BoolVar (& argv .RateLimitDisable , "rate-limit-disable" , false , "disable rate limiting" )
122+ f .DurationVar (& argv .WindowDuration , "rate-limit-window-duration" , default_ .WindowDuration , "time window for analyzing ClickHouse requests" )
123+ f .Uint64Var (& argv .MaxErrorRate , "rate-limit-max-error-rate" , default_ .MaxErrorRate , "error rate threshold (%) to trigger sleep stage" )
124+ f .Uint64Var (& argv .MaxInflightWeight , "rate-limit-max-inflight-weight" , default_ .MaxInflightWeight , "maximum weight per inflight request (%)" )
125+ f .Uint64Var (& argv .RecoverWeightStep , "rate-limit-recover-weight-step" , default_ .RecoverWeightStep , "recover weight step (%) per recovery cycle" )
126+ f .DurationVar (& argv .RecoverGapDuration , "rate-limit-recover-gap-duration" , default_ .RecoverGapDuration , "recover time gap per recovery cycle" )
127+ f .Float64Var (& argv .SleepMultiplier , "rate-limit-sleep-multiplier" , default_ .SleepMultiplier , "sleep duration multiplier after failed check" )
128+ f .DurationVar (& argv .MaxSleepDuration , "rate-limit-max-sleep-duration" , default_ .MaxSleepDuration , "maximum sleep duration limit" )
129+ f .DurationVar (& argv .MinSleepDuration , "rate-limit-min-sleep-duration" , default_ .MinSleepDuration , "minimum sleep duration when entering sleep" )
130+ f .Uint64Var (& argv .CheckCount , "rate-limit-check-count" , default_ .CheckCount , "successful checks needed to exit check stage" )
131+ f .DurationVar (& argv .RecalcInterval , "recalc-interval-sleep-duration" , default_ .RecalcInterval , "rate limit state recalculation interval" )
119132}
120133
121134func DefaultConfig () * Config {
122135 return & Config {
123136 ApproxCacheMaxSize : 1_000_000 ,
124137 AvailableShardsStr : "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16" ,
125138 CHMaxShardConnsRatio : 20 ,
139+ RateLimitConfig : chutil.RateLimitConfig {
140+ WindowDuration : 2 * time .Minute ,
141+ MaxErrorRate : 20 ,
142+ MaxInflightWeight : 10 ,
143+ RecoverWeightStep : 1 ,
144+ RecoverGapDuration : 6 * time .Second ,
145+ SleepMultiplier : 1.5 ,
146+ MaxSleepDuration : 2 * time .Minute ,
147+ MinSleepDuration : 30 * time .Second ,
148+ CheckCount : 9 ,
149+ RecalcInterval : 1 * time .Second ,
150+ },
126151 }
127152}
128153
@@ -154,7 +179,7 @@ type HandlerOptions struct {
154179 timezone string
155180 protectedMetricPrefixesS string
156181 protectedMetricPrefixes []string
157- querySelectTimeout time.Duration
182+ QuerySelectTimeout time.Duration
158183 weekStartAt int
159184 location * time.Location
160185 utcOffset int64
@@ -167,7 +192,7 @@ func (argv *HandlerOptions) Bind(f *flag.FlagSet) {
167192 f .BoolVar (& argv .querySequential , "query-sequential" , false , "disables query parallel execution" )
168193 f .BoolVar (& argv .readOnly , "readonly" , false , "read only mode" )
169194 f .BoolVar (& argv .verbose , "verbose" , false , "verbose logging" )
170- f .DurationVar (& argv .querySelectTimeout , "query-select-timeout" , QuerySelectTimeoutDefault , "query select timeout" )
195+ f .DurationVar (& argv .QuerySelectTimeout , "query-select-timeout" , QuerySelectTimeoutDefault , "query select timeout" )
171196 f .StringVar (& argv .protectedMetricPrefixesS , "protected-metric-prefixes" , "" , "comma-separated list of metric prefixes that require access bits set" )
172197 f .StringVar (& argv .timezone , "timezone" , "Europe/Moscow" , "location of the desired timezone" )
173198 f .IntVar (& argv .weekStartAt , "week-start" , int (time .Monday ), "week day of beginning of the week (from sunday=0 to saturday=6)" )
0 commit comments