github.com/richardwilkes/toolbox@v1.121.0/rate/interface.go (about)

     1  // Copyright (c) 2016-2024 by Richard A. Wilkes. All rights reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the Mozilla Public
     4  // License, version 2.0. If a copy of the MPL was not distributed with
     5  // this file, You can obtain one at http://mozilla.org/MPL/2.0/.
     6  //
     7  // This Source Code Form is "Incompatible With Secondary Licenses", as
     8  // defined by the Mozilla Public License, version 2.0.
     9  
    10  package rate
    11  
    12  // Limiter provides a rate limiter.
    13  type Limiter interface {
    14  	// New returns a new limiter that is subordinate to this limiter, meaning that its cap rate is also capped by its
    15  	// parent.
    16  	New(capacity int) Limiter
    17  
    18  	// Cap returns the capacity per time period.
    19  	Cap(applyParentCaps bool) int
    20  
    21  	// SetCap sets the capacity.
    22  	SetCap(capacity int)
    23  
    24  	// LastUsed returns the capacity used in the last time period.
    25  	LastUsed() int
    26  
    27  	// Use returns a channel that will return nil when the request is successful, or an error if the request cannot be
    28  	// fulfilled.
    29  	Use(amount int) <-chan error
    30  
    31  	// Closed returns true if the limiter is closed.
    32  	Closed() bool
    33  
    34  	// Close this limiter and any children it may have.
    35  	Close()
    36  }