util

package
v0.0.0-...-bd9ca74 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2021 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrStopped = errors.New("RateCounter stopped")

ErrStopped means a read failed because the governing rate counter was stopped.

Functions

func VerifyStreamHash

func VerifyStreamHash(r io.Reader, md5, sha256 []byte) (bool, error)

VerifyStreamHash checksums the given io.Reader and compares the checksum against the provided md5 and sha256 checksums. It returns true if everything matches, and false otherwise. Pass in an empty slice to not verify a given checksum type. For example, to only verify the SHA256 hash of the reader, pass in []byte{} for the md5 parameter. The reader is not closed when finished.

Types

type HashWriter

type HashWriter struct {
	io.Writer // our io.MultiWriter
	// contains filtered or unexported fields
}

An HashWriter wraps an io.Writer and also calculate the MD5 and SHA256 hashes of the bytes written.

func NewHashWriter

func NewHashWriter(w io.Writer) *HashWriter

NewHashWriter returns a HashWriter wrapping w.

func NewHashWriterPlain

func NewHashWriterPlain() *HashWriter

NewHashWriterPlain return a HashWriter that does not wrap an output stream. It will just compute the checksums of the data written to it.

func NewMD5Writer

func NewMD5Writer(w io.Writer) *HashWriter

NewMD5Writer returns a HashWriter wrapping w and only computing an MD5 hash.

func (*HashWriter) CheckMD5

func (hw *HashWriter) CheckMD5(goal []byte) ([]byte, bool)

CheckMD5 returns the MD5 hash for this writer, and compares it for equality with the goal hash passed in. Returns true if goal matches the MD5 hash, false otherwise. If the goal is empty then it is treated as matching, and true is returned.

func (*HashWriter) CheckSHA256

func (hw *HashWriter) CheckSHA256(goal []byte) ([]byte, bool)

CheckSHA256 returns the SHA256 hash for this writer, and compares it for equality with the goal hash passed in. Returns true if goal matches the SHA256 hash, false otherwise. If the goal is empty then it is treated as matching, and true is returned.

type RateCounter

type RateCounter struct {
	// contains filtered or unexported fields
}

A RateCounter tracks how many bytes we have checksummed and makes sure we keep under the rate limit given. Every so often we increment our pool. As we checksum we remove credits from the pool. If the pool goes negative, then we wait until it goes positive.

func NewRateCounter

func NewRateCounter(rate float64) *RateCounter

NewRateCounter returns a counter where credits accumulate at the given credits per second. However, the credits are not accumulated every second. Instead the entire amount due is added every 20 minutes.

func (*RateCounter) OK

func (r *RateCounter) OK() <-chan struct{}

OK returns a channel to wait on. It will receive an empty struct when it is OK to resume reading. The channel will be closed if the RateCounter is Stopped.

func (*RateCounter) Stop

func (r *RateCounter) Stop()

Stop the background goroutine refilling the RateCounter. Will panic if called twice.

func (*RateCounter) Use

func (r *RateCounter) Use(count int64)

Use some number of units. It is okay if it takes this counter negative.

func (*RateCounter) Wrap

func (r *RateCounter) Wrap(reader io.Reader) io.Reader

Wrap takes an io.Reader and returns a new one where reads are limited by this RateCounter. Reads will block until the RateCounter says the current usage is ok. It is okay for more than one goroutine to use the same RateCounter. If the RateCounter was stopped, the returned reader will cause an ErrStopped.

Jump to

Keyboard shortcuts

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