// ExecOnLimitReached is thread-safe way of executing after-rejection function when limit is reached.
420767
func (l *Limiter) ExecOnLimitReached(w http.ResponseWriter, r *http.Request) {
	l.RLock()
	defer l.RUnlock()

	fn := l.onLimitReached
	if fn != nil {
		fn(w, r)
	}
}
1309656
func rateLimitExceeded(err *googleapi.Error) bool {
	for _, err := range err.Errors {
		if strings.Contains(err.Message, "Rate Limit Exceeded") {
			return true
		}
	}
	return false
}
1192264
func NewThrottledExecutor(limit int) *ThrottledExecutor {
	if limit == 0 {
		panic("Limit must be > 0")
	}
	return &ThrottledExecutor{limiter: make(chan struct{}, limit)}
}
466272
func IsRateLimit(err error) bool {
	re, ok := err.(rateLimitError)
	return ok && re.IsRateLimit()
}
1007010
func (limiter *Limiter) Limit() {
	for {
		if limiter.requestCount < limiter.MaxConcurrentRequests {
			break
		}
		limiter.Wait()
	}
	limiter.requestCount++
}
181942
func ExtractLimit(opts ...interface{}) *opt.LimitOption {
	for _, o := range opts {
		if v, ok := o.(*opt.LimitOption); ok {
			return v
		}
	}
	return nil
}
196741
func (o *ListOAuth2ClientsParams) WithLimit(limit *int64) *ListOAuth2ClientsParams {
	o.SetLimit(limit)
	return o
}
1054043
func LimitByRequest(limiter *config.Limiter, r engine.Request) *echo.HTTPError {
	sliceKeys := BuildKeys(limiter, r)

	// Loop sliceKeys and check if one of them has error.
	for _, keys := range sliceKeys {
		httpError := LimitByKeys(limiter, keys)
		if httpError != nil {
			return httpError
		}
	}

	return nil
}
628062
func (l *Limiter) LimitReached(key string) bool {
	ttl := l.GetTokenBucketExpirationTTL()

	if ttl <= 0 {
		ttl = l.generalExpirableOptions.DefaultExpirationTTL
	}

	return l.limitReachedWithTokenBucketTTL(key, ttl)
}
93676
func validateLimit(v, min, max int) error {

	// ensure the value for `limit` is valid
	if v < min || v > max {
		err := fmt.Errorf("%d is not a valid value for `limit`, must be between %d-%d (inclusive).", v, min, max)
		return err
	}

	return nil
}
230525
func (sm *SyncManager) limitMap(m map[chainhash.Hash]struct{}, limit int) {
	if len(m)+1 > limit {
		// Remove a random entry from the map.  For most compilers, Go's
		// range statement iterates starting at a random item although
		// that is not 100% guaranteed by the spec.  The iteration order
		// is not important here because an adversary would have to be
		// able to pull off preimage attacks on the hashing function in
		// order to target eviction of specific entries anyways.
		for txHash := range m {
			delete(m, txHash)
			return
		}
	}
}
69893
func containsLimit(execs []*tipb.Executor) bool {
	for _, exec := range execs {
		if exec.Limit != nil {
			return true
		}
	}
	return false
}
1371319
func (h RequestLimitHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
	// Limit the concurrent requests that can run through the sem channel
	h.sem <- 1
	defer func() {
		<-h.sem
	}()

	return h.Next.ServeHTTP(w, r)
}
182487
func (c *Client) IntervalsWithLimit(limit int) (intervals []contract.Interval, err error) {
	conn := c.Pool.Get()
	defer conn.Close()

	objects, err := getObjectsByRange(conn, models.IntervalKey, 0, limit-1)
	if err != nil {
		if err != redis.ErrNil {
			return intervals, err
		}
	}

	intervals = make([]contract.Interval, len(objects))
	for i, object := range objects {
		err = json.Unmarshal(object, &intervals[i])
		if err != nil {
			return []contract.Interval{}, err
		}
	}

	return intervals, nil
}
349084
func BoundedExponentialBackoff(baseSleepTime time.Duration, maxSleepTime time.Duration, maxRetries int) Policy {
	return boundedExponentialBackoff{
		exponentialBackoff: exponentialBackoff{
			baseSleepTime: baseSleepTime,
			maxRetries:    maxRetries,
		},
		maxSleepTime: maxSleepTime,
	}
}
253683
func DefaultLimitReachedHandler(w http.ResponseWriter, r *http.Request) {
	http.Error(w, "Limit exceeded", http.StatusTooManyRequests)
}
677258
func IsErrorLimitExceeded(err error) bool {
	if isForbidden := apierrs.IsForbidden(err); isForbidden || apierrs.IsInvalid(err) {
		lowered := strings.ToLower(err.Error())
		// the limit error message can be accompanied only by Invalid reason
		if strings.Contains(lowered, errLimitsMessageString) {
			return true
		}
	}
	return false
}