util

package
v0.14.3 Latest Latest
Warning

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

Go to latest
Published: May 31, 2019 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const RaceEnabled = false

RaceEnabled indicates that program/tests are running with race detection enabled or not. Some tests may chose to skip execution when race detection is on.

Variables

View Source
var (
	ErrInvalidSubject = errors.New("sublist: invalid subject")
	ErrNotFound       = errors.New("sublist: no match found")
)

Sublist related errors

View Source
var ByteOrder binary.ByteOrder

ByteOrder specifies how to convert byte sequences into 16-, 32-, or 64-bit unsigned integers.

View Source
var ErrUnableToLockNow = errors.New("unable to acquire the lock at the moment")

ErrUnableToLockNow is used to indicate that a lock cannot be immediately acquired.

Functions

func CloseFile added in v0.4.0

func CloseFile(err error, f io.Closer) error

CloseFile closes the given file and report the possible error only if the given error `err` is not already set.

func DecodeChannels added in v0.9.0

func DecodeChannels(data []byte) ([]string, error)

DecodeChannels decodes from the given byte array the list of channel names and return them as an array of strings.

func EnsureBufBigEnough

func EnsureBufBigEnough(buf []byte, needed int) []byte

EnsureBufBigEnough checks that given buffer is big enough to hold 'needed' bytes, otherwise returns a buffer of a size of at least 'needed' bytes.

func FriendlyBytes added in v0.4.0

func FriendlyBytes(bytes int64) string

FriendlyBytes returns a string with the given bytes int64 represented as a size, such as 1KB, 10MB, etc...

func IsChannelNameLiteral added in v0.6.0

func IsChannelNameLiteral(channel string) bool

IsChannelNameLiteral returns true if the channel name is a literal (that is, it does not contain any wildcard). The channel name is assumed to be valid.

func IsChannelNameValid added in v0.6.0

func IsChannelNameValid(channel string, wildcardsAllowed bool) bool

IsChannelNameValid returns false if any of these conditions for the channel name apply: - is empty - contains the `/` character - token separator `.` is first or last - there are two consecutives token separators `.` if wildcardsAllowed is false: - contains wildcards `*` or `>` if wildcardsAllowed is true: - '*' or '>' are not a token in their own - `>` is not the last token

func ReadInt

func ReadInt(r io.Reader) (int, error)

ReadInt reads an int (4 bytes) from the reader using ByteOrder.

func SendChannelsList added in v0.9.0

func SendChannelsList(channels []string, sendInbox, replyInbox string, nc *nats.Conn, serverID string) error

SendsChannelsList sends the list of channels to the given subject, possibly splitting the list in several requests if it cannot fit in a single message.

func WriteInt

func WriteInt(w io.Writer, v int) error

WriteInt writes an int (4 bytes) to the given writer using ByteOrder.

Types

type BackoffTimeCheck added in v0.4.0

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

BackoffTimeCheck allows to execute some code, but not too often.

func NewBackoffTimeCheck added in v0.4.0

func NewBackoffTimeCheck(minFrequency time.Duration, factor int, maxFrequency time.Duration) (*BackoffTimeCheck, error)

NewBackoffTimeCheck creates an instance of BackoffTimeCheck. The `minFrequency` indicates how frequently BackoffTimeCheck.Ok() can return true. When Ok() returns true, the allowed frequency is multiplied by `factor`. The resulting frequency is capped by `maxFrequency`.

func (*BackoffTimeCheck) Ok added in v0.4.0

func (bp *BackoffTimeCheck) Ok() bool

Ok returns true for the first time it is invoked after creation of the object or call to Reset(), or after an amount of time (based on the last success and the allowed frequency) has elapsed. When at the maximum frequency, if this call is made after a delay at least equal to 3x the max frequency (or in other words, 2x after what was the target for the next print), then the object is auto-reset.

func (*BackoffTimeCheck) Reset added in v0.4.0

func (bp *BackoffTimeCheck) Reset()

Reset the state so that next call to BackoffPrint.Ok() will return true.

type LockFile added in v0.4.0

type LockFile interface {
	io.Closer
	IsClosed() bool
}

LockFile is an interface for lock files utility.

func CreateLockFile added in v0.4.0

func CreateLockFile(file string) (LockFile, error)

CreateLockFile attempt to lock the given file, creating it if necessary. On success, the file is returned, otherwise an error is returned. The file returned should be closed to release the lock quicker than if left to the operating system.

type Sublist added in v0.5.0

type Sublist struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

A Sublist stores and efficiently retrieves subscriptions.

func NewSublist added in v0.5.0

func NewSublist() *Sublist

NewSublist creates a default sublist

func (*Sublist) CacheCount added in v0.5.0

func (s *Sublist) CacheCount() int

CacheCount returns the number of result sets in the cache.

func (*Sublist) Count added in v0.5.0

func (s *Sublist) Count() uint32

Count returns the number of subscriptions.

func (*Sublist) Insert added in v0.5.0

func (s *Sublist) Insert(subject string, element interface{}) error

Insert adds a subscription into the sublist

func (*Sublist) Match added in v0.5.0

func (s *Sublist) Match(subject string) []interface{}

Match will match all entries to the literal subject. It will return a set of results.

func (*Sublist) NumLevels added in v0.5.0

func (s *Sublist) NumLevels() int

NumLevels returns the maximum number of levels in the sublist.

func (*Sublist) Remove added in v0.5.0

func (s *Sublist) Remove(subject string, element interface{}) error

Remove will remove an element from the sublist.

func (*Sublist) Subjects added in v0.5.0

func (s *Sublist) Subjects() []string

Subjects returns an array of all subjects in this sublist ordered from the widest to the narrowest of subjects. Order between non wildcard tokens in a given level is random though.

For instance, if the sublist contains (in any inserted order):

*.*, foo.>, *.>, foo.*.>, >, bar.>, foo.bar.>, bar.baz

the returned array will be one of the two possibilities:

>, *.>, *.*, foo.>, foo.*.>, foo.bar.>, bar.>, bar.baz

or

>, *.>, *.*, bar.>, bar.baz, foo.>, foo.*.>, foo.bar.>

For a given level, the order will still always be from wider to narrower, that is, foo.> comes before foo.*.> which comes before foo.bar.>, and bar.> always comes before bar.baz, but all the "bar" subjects may be before or after all the "foo" subjects.

Jump to

Keyboard shortcuts

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