utils

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Apr 14, 2023 License: AGPL-3.0 Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AnyError

func AnyError(errors []error) error

Returns the first non nil error in the slice or nil

func BinarySize

func BinarySize(v interface{}) int

BinarySize gets the amount of bytes required to write the value, validating that all types are fixed-sized.

func CollectErrors

func CollectErrors(channels []chan error) []error

Collects single messages from each channel

func ContainsString

func ContainsString(values []string, key string) bool

func ContainsToken

func ContainsToken(values []types.TokenRanges, key types.Token) bool

func CreateErrAndLog

func CreateErrAndLog(format string, v ...interface{}) error

func FindGenByToken

func FindGenByToken(generations []types.Generation, token types.Token) int

func FromUnixMillis

func FromUnixMillis(millis int64) time.Time

func GetServiceAddress

func GetServiceAddress(port int, localInfo *types.BrokerInfo, config conf.BasicConfig) string

GetServiceAddress determines whether it should be bind to all interfaces or it should use a single host name

func IfEmpty

func IfEmpty(value string, defaultValue string) string

func InParallel

func InParallel(length int, f func(int) error) []chan error

Runs in parallel and collects the results in channels

func InParallelAnyError

func InParallelAnyError(length int, f func(int) error) error

Runs in parallel and returns an error if any is found

func IsSuccess

func IsSuccess(code int) bool

Returns true for 2xx status code

func Jitter

func Jitter(t time.Duration) time.Duration

Adds a +-5% jitter to the duration with millisecond resolution

func MaxVersion

func MaxVersion(values ...*types.Generation) types.GenVersion

MaxVersion gets the maximum version value of the non-nil generations provided. Defaults to zero.

func Min

func Min(a, b int) int

func NewBufferCap

func NewBufferCap(initialCap int) *bytes.Buffer

NewBufferCap returns a buffer with the initial provided initial capacity

func NewSingleConnListener

func NewSingleConnListener(conn net.Conn) net.Listener

func NoContentResponse

func NoContentResponse(w http.ResponseWriter, retryAfter int)

Sets the response status as 204 (NoContent) w/ no cache and optionally setting the retry after header.

func PanicIfErr

func PanicIfErr(err error, message string)

For fatal errors, it logs and exists

func ReadBodyClose

func ReadBodyClose(resp *http.Response) (string, error)

Reads the body as a string and closes it

func ReadIntoBuffers added in v0.7.0

func ReadIntoBuffers(reader io.Reader, buffers [][]byte, length int) error

Reads from the reader into the provided buffers until length, returning an error otherwise

func RespondText

func RespondText(w http.ResponseWriter, message string)

Writes a text message in the response

func ToBlob

func ToBlob(v uuid.UUID) []byte

func ToCsv

func ToCsv(values []int) string

func ToHandle

func ToHandle(he HandleWithError) httprouter.Handle

ToHandle wraps a handle func with error and converts it to a `httprouter.Handle`

func ToPostHandle

func ToPostHandle(he HandleWithError) httprouter.Handle

ToPostHandle wraps a handle func with error, returns plain text "OK" and converts it to a `httprouter.Handle`

func ToUnixMillis

func ToUnixMillis(t time.Time) int64

func ValidRingLength

func ValidRingLength(length int) int

For a given number, it returns the last ring length that can contain it. For example: given 3 it returns 3; for 4 -> 3; for 5 -> 3; for 7 -> 6

func WriteBytes

func WriteBytes(w io.Writer, buf []byte) error

Writes the buffer, validating for "short writes"

Types

type CopyOnWriteMap

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

CopyOnWriteMap provides basic functionality of a copy-on-write dictionary that uses a valueCreator function (instead of a value like sync.Map)

func NewCopyOnWriteMap

func NewCopyOnWriteMap() *CopyOnWriteMap

func (*CopyOnWriteMap) LoadOrStore

func (c *CopyOnWriteMap) LoadOrStore(key interface{}, valueCreator func() (interface{}, error)) (value interface{}, loaded bool, err error)

type Debouncer

type Debouncer func(func())

func Debounce

func Debounce(delay time.Duration, threshold float64) Debouncer

Creates a debouncer that will stop the previous timer when the time that has passed is below the threshold. For example: if we set it to 5 mins and only few seconds passed, it will stop the previous one and create a new timer. On the other hand, if several minutes passed it will not stop the previous and only issue a new one.

type HandleWithError

type HandleWithError func(http.ResponseWriter, *http.Request, httprouter.Params) error

type MultiBufferReader added in v0.7.0

type MultiBufferReader interface {
	io.Reader
	ReadUint8() (uint8, error)
	ReadUint32() (uint32, error)
	ReadUint64() (uint64, error)
	ReadString(length int) (string, error)
	ReadStringBytes() (string, error)

	// Returns the unread portion of the buffers and the total length
	Bytes() ([][]byte, int)
}

Represents a reader that attempts to read without allocating new buffers.

func NewMultiBufferReader added in v0.7.0

func NewMultiBufferReader(buffers [][]byte, bufferSize int, length int) MultiBufferReader

Returns a reader that wraps multiple []byte buffers and tries to read without allocating new buffers.

Jump to

Keyboard shortcuts

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