/** * Configures as a sliding window rate limit. Imagine the duration window divided into a number of smaller buckets, each with it's own count. * The number of smaller buckets is defined by the precision. * * @param precision Defines the number of buckets that will be used to approximate the sliding window. * @return a limit rule */ public RequestLimitRule withPrecision(int precision) { return new RequestLimitRule(this.durationSeconds, this.limit, precision, this.name, this.keys); }
/** * Applies a name to the rate limit that is useful for metrics. * * @param name Defines a descriptive name for the rule limit. * @return a limit rule */ public RequestLimitRule withName(String name) { return new RequestLimitRule(this.durationSeconds, this.limit, this.precision, name, this.keys); }
/** * Applies a key to the rate limit that defines to which keys, the rule applies, null for any unmatched key. * * @param keys Defines a set of keys to which the rule applies. * @return a limit rule */ public RequestLimitRule matchingKeys(Set<String> keys) { return new RequestLimitRule(this.durationSeconds, this.limit, this.precision, this.name, keys); }
/** * Initialise a request rate limit. Imagine the whole duration window as being one large bucket with a single count. * * @param duration The time the limit will be applied over. * @param limit A number representing the maximum operations that can be performed in the given duration. * @return A limit rule. */ public static RequestLimitRule of(Duration duration, long limit) { requireNonNull(duration, "duration can not be null"); if (limit < 0) { throw new IllegalArgumentException("limit must be greater than zero."); } int durationSeconds = (int) duration.getSeconds(); return new RequestLimitRule(durationSeconds, limit, durationSeconds); }