The limiter
package provides reader/writer capabilities with transfer speed limiting in Go.
The Limiter
is designed to limit the total transfer speed of readers and writers simultaneously.
- Transfer Speed Limiting: Control the speed at which operations can be performed.
- Dynamic Configuration: Set and update the transfer speed limit and burst size as needed.
- Contextual Support: Perform operations with transfer speed limits within a specific context.
Import the package in your Go code:
import "github.com/sunshineplan/limiter"
Create a new Limiter
by calling the New
function:
limiter := limiter.New(100*1024)
The above code creates a Limiter
with a total transfer speed limit of 100KB per second and a burst size of 100KB.
To perform limited operations, you can use the provided Reader
and Writer
methods. Here's an example of using the Reader
method:
reader := limiter.Reader(yourReader)
The Reader
method takes an io.Reader
and returns a new reader that performs limited reads.
Similarly, you can use the Writer
method to create a limited writer:
writer := limiter.Writer(yourWriter)
You can update the limit and burst size of the Limiter
by calling the respective Set
methods:
limiter.SetLimit(200*1024) // 200KB
limiter.SetBurst(20*1024) // 20KB
These methods allow you to dynamically adjust the limiting behavior according to your requirements.
Contributions are welcome! If you find a bug or have a suggestion for improvement, please open an issue or submit a pull request.
This package is released under the MIT License.