Documentation
¶
Index ¶
- Variables
- type SpeedLimit
- type Speedometer
- func NewCappedLimitedSpeedometer(w io.Writer, speedLimit *SpeedLimit, capacity int64) (*Speedometer, error)
- func NewCappedSpeedometer(w io.Writer, capacity int64) (*Speedometer, error)
- func NewLimitedSpeedometer(w io.Writer, speedLimit *SpeedLimit) (*Speedometer, error)
- func NewReadingSpeedometer(r io.Reader) (*Speedometer, error)
- func NewSpeedometer(w io.Writer) (*Speedometer, error)
Constants ¶
This section is empty.
Variables ¶
var ( ErrWriteOnly = errors.New("not a reader") ErrReadOnly = errors.New("not a writer") )
var ErrLimitReached = errors.New("limit reached")
Functions ¶
This section is empty.
Types ¶
type SpeedLimit ¶
type SpeedLimit struct {
// Burst is the number of bytes that can be written to the underlying writer per Frame.
Burst int64
// Frame is the duration of the frame in which Burst can be written to the underlying writer.
Frame time.Duration
// CheckEveryBytes is the number of bytes written before checking if the speed limit has been exceeded.
CheckEveryBytes int64
// Delay is the duration to delay writing if the speed limit has been exceeded during a Write call. (blocking)
Delay time.Duration
}
SpeedLimit is used to limit the rate at which data is written to the underlying writer.
func NewBytesPerSecondLimit ¶
func NewBytesPerSecondLimit(bytes int64) *SpeedLimit
type Speedometer ¶
type Speedometer struct {
// contains filtered or unexported fields
}
Speedometer is an io.Writer wrapper that will limit the rate at which data is written to the underlying target.
It is safe for concurrent use, but writers will block when slowed down.
Optionally, it can be given;
a capacity, which will cause it to return an error if the capacity is exceeded.
a speed limit, causing slow downs of data written to the underlying writer if the speed limit is exceeded.
func NewCappedLimitedSpeedometer ¶
func NewCappedLimitedSpeedometer(w io.Writer, speedLimit *SpeedLimit, capacity int64) (*Speedometer, error)
NewCappedLimitedSpeedometer creates a new Speedometer that wraps the given io.Writer. It is a combination of NewLimitedSpeedometer and NewCappedSpeedometer.
func NewCappedSpeedometer ¶
func NewCappedSpeedometer(w io.Writer, capacity int64) (*Speedometer, error)
NewCappedSpeedometer creates a new Speedometer that wraps the given io.Writer. If len(written) bytes exceeds cap, writes to the underlying writer will be ceased permanently for the Speedometer.
func NewLimitedSpeedometer ¶
func NewLimitedSpeedometer(w io.Writer, speedLimit *SpeedLimit) (*Speedometer, error)
NewLimitedSpeedometer creates a new Speedometer that wraps the given io.Writer. If the speed limit is exceeded, writes to the underlying writer will be limited. See SpeedLimit for more information.
func NewReadingSpeedometer ¶ added in v1.3.5
func NewReadingSpeedometer(r io.Reader) (*Speedometer, error)
func NewSpeedometer ¶
func NewSpeedometer(w io.Writer) (*Speedometer, error)
NewSpeedometer creates a new Speedometer that wraps the given io.Writer. It will not limit the rate at which data is written to the underlying writer, it only measures it.
func (*Speedometer) Close ¶
func (s *Speedometer) Close() error
Close stops the Speedometer. No additional writes will be accepted.
func (*Speedometer) Rate ¶
func (s *Speedometer) Rate() float64
Rate returns the bytes per second rate at which data is being written to the underlying writer.
func (*Speedometer) Running ¶
func (s *Speedometer) Running() bool
Running returns true if the Speedometer is still running.
func (*Speedometer) Total ¶
func (s *Speedometer) Total() int64
Total returns the total number of bytes written to the underlying writer.