speedometer

package
v1.3.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 3, 2024 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrWriteOnly = errors.New("not a reader")
	ErrReadOnly  = errors.New("not a writer")
)
View Source
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) Read added in v1.3.5

func (s *Speedometer) Read(p []byte) (n int, err error)

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.

func (*Speedometer) Write

func (s *Speedometer) Write(p []byte) (n int, err error)

Write writes p to the underlying writer, following all defined speed limits.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL