util

package
v0.0.0-...-feddf00 Latest Latest
Warning

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

Go to latest
Published: Jan 9, 2018 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrorHandlers = []func(error){
	logError,
	(&rudimentaryErrorBackoff{
		lastErrorTime: time.Now(),

		minPeriod: time.Millisecond,
	}).OnError,
}

ErrorHandlers is a list of functions which will be invoked when an unreturnable error occurs. TODO(lavalamp): for testability, this and the below HandleError function should be packaged up into a testable and reusable object.

View Source
var INT32SIZE int = binary.Size(int32(0))
View Source
var INT64SIZE int = binary.Size(int64(0))
View Source
var NeverStop <-chan struct{} = make(chan struct{})

NeverStop may be passed to Until to make it never stop.

View Source
var PanicHandlers = []func(interface{}){logPanic}

PanicHandlers is a list of functions which will be invoked when a panic happens.

View Source
var (
	// ReallyCrash controls the behavior of HandleCrash and now defaults
	// true. It's still exposed so components can optionally set to false
	// to restore prior behavior.
	ReallyCrash = true
)
View Source
var UINT16SIZE int = binary.Size(uint16(0))
View Source
var UINT32SIZE int = binary.Size(uint32(0))
View Source
var UINT64SIZE int = binary.Size(uint64(0))
View Source
var Waitlock_Timeout = errors.New("waitlock timeout")

Functions

func AppendBytes

func AppendBytes(inputs ...[]byte) []byte

func CopyBytes

func CopyBytes(src []byte) []byte

func CopyStruct

func CopyStruct(dst interface{}, src interface{}) (err error)

dst should be a pointer to struct, src should be a struct

func Crc32

func Crc32(crc uint32, value []byte, skiplen int) uint32

func DecodeInt32

func DecodeInt32(buffer []byte, offset int, ret *int32)

func DecodeUint16

func DecodeUint16(buffer []byte, offset int, ret *uint16)

func DecodeUint32

func DecodeUint32(buffer []byte, offset int, ret *uint32)

func DecodeUint64

func DecodeUint64(buffer []byte, offset int, ret *uint64)

func DeleteDir

func DeleteDir(path string) error

func EncodeInt32

func EncodeInt32(buffer []byte, offset int, ret int32)

func EncodeUint16

func EncodeUint16(buffer []byte, offset int, ret uint16)

func EncodeUint32

func EncodeUint32(buffer []byte, offset int, ret uint32)

func EncodeUint64

func EncodeUint64(buffer []byte, offset int, ret uint64)

func Forever

func Forever(f func(), period time.Duration)

Forever calls f every period for ever.

Forever is syntactic sugar on top of Until.

func GetCaller

func GetCaller() string

GetCaller returns the caller of the function that calls it.

func HandleCrash

func HandleCrash(additionalHandlers ...func(interface{}))

HandleCrash simply catches a crash and logs an error. Meant to be called via defer. Additional context-specific handlers can be provided, and will be called in case of panic. HandleCrash actually crashes, after calling the handlers and logging the panic message.

TODO: remove this function. We are switching to a world where it's safe for apiserver to panic, since it will be restarted by kubelet. At the beginning of the Kubernetes project, nothing was going to restart apiserver and so catching panics was important. But it's actually much simpler for montoring software if we just exit when an unexpected panic happens.

func HandleError

func HandleError(err error)

HandlerError is a method to invoke when a non-user facing piece of code cannot return an error and needs to indicate it has been ignored. Invoking this method is preferable to logging the error - the default behavior is to log but the errors may be sent to a remote server for analysis.

func Inet_addr

func Inet_addr(ipaddr string) uint32

func IsDirectoryExist

func IsDirectoryExist(path string) bool

func IterDir

func IterDir(dirPath string, filePathList []string) error

func Jitter

func Jitter(duration time.Duration, maxFactor float64) time.Duration

Jitter returns a time.Duration between duration and duration + maxFactor * duration.

This allows clients to avoid converging on periodic behavior. If maxFactor is 0.0, a suggested default value will be chosen.

func JitterUntil

func JitterUntil(f func(), period time.Duration, jitterFactor float64, sliding bool, stopCh <-chan struct{})

JitterUntil loops until stop channel is closed, running f every period.

If jitterFactor is positive, the period is jittered before every run of f. If jitterFactor is not positive, the period is unchanged and not jittered.

If sliding is true, the period is computed after f runs. If it is false then period includes the runtime for f.

Close stopCh to stop. f may not be invoked if stop channel is already closed. Pass NeverStop to if you don't want it stop.

func NonSlidingUntil

func NonSlidingUntil(f func(), period time.Duration, stopCh <-chan struct{})

NonSlidingUntil loops until stop channel is closed, running f every period.

NonSlidingUntil is syntactic sugar on top of JitterUntil with zero jitter factor, with sliding = false (meaning the timer for period starts at the same time as the function starts).

func NowTimeMs

func NowTimeMs() uint64

func Rand

func Rand() int32

func RecoverFromPanic

func RecoverFromPanic(err *error)

RecoverFromPanic replaces the specified error with an error containing the original error, and the call tree when a panic occurs. This enables error handlers to handle errors and panics the same way.

func SleepMs

func SleepMs(ms int32)

func StartRoutine

func StartRoutine(f func())

func TestAssert

func TestAssert(tb testing.TB, condition bool, msg string, v ...interface{})

assert fails the test if the condition is false.

func Until

func Until(f func(), period time.Duration, stopCh <-chan struct{})

Until loops until stop channel is closed, running f every period.

Until is syntactic sugar on top of JitterUntil with zero jitter factor and with sliding = true (which means the timer for period starts after the f completes).

Types

type Deque

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

Deque is a head-tail linked list data structure implementation. It is based on a doubly linked list container, so that every operations time complexity is O(1).

every operations over an instiated Deque are synchronized and safe for concurrent usage.

func NewCappedDeque

func NewCappedDeque(capacity int) *Deque

NewCappedDeque creates a Deque with the specified capacity limit.

func NewDeque

func NewDeque() *Deque

NewDeque creates a Deque.

func (*Deque) Append

func (s *Deque) Append(item interface{}) bool

Append inserts element at the back of the Deque in a O(1) time complexity, returning true if successful or false if the deque is at capacity.

func (*Deque) Capacity

func (s *Deque) Capacity() int

Capacity returns the capacity of the deque, or -1 if unlimited

func (*Deque) Empty

func (s *Deque) Empty() bool

Empty checks if the deque is empty

func (*Deque) First

func (s *Deque) First() interface{}

First returns the first value stored in the deque in a O(1) time complexity

func (*Deque) Full

func (s *Deque) Full() bool

Full checks if the deque is full

func (*Deque) Last

func (s *Deque) Last() interface{}

Last returns the last value stored in the deque in a O(1) time complexity

func (*Deque) Pop

func (s *Deque) Pop() interface{}

Pop removes the last element of the deque in a O(1) time complexity

func (*Deque) Prepend

func (s *Deque) Prepend(item interface{}) bool

Prepend inserts element at the Deques front in a O(1) time complexity, returning true if successful or false if the deque is at capacity.

func (*Deque) Shift

func (s *Deque) Shift() interface{}

Shift removes the first element of the deque in a O(1) time complexity

func (*Deque) Size

func (s *Deque) Size() int

Size returns the actual deque size

type Queue

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

func NewQueue

func NewQueue() *Queue

func (*Queue) Add

func (self *Queue) Add(value interface{}) int

func (*Queue) Broadcast

func (self *Queue) Broadcast()

func (*Queue) Empty

func (self *Queue) Empty() bool

func (*Queue) Lock

func (self *Queue) Lock()

func (*Queue) Peek

func (self *Queue) Peek() interface{}

func (*Queue) PeekWithTimeout

func (self *Queue) PeekWithTimeout(ms int) interface{}

func (*Queue) Pop

func (self *Queue) Pop()

func (*Queue) Signal

func (self *Queue) Signal()

func (*Queue) Size

func (self *Queue) Size() int

func (*Queue) Unlock

func (self *Queue) Unlock()

type TimeStat

type TimeStat struct {
	Time uint64
}

func NewTimeStat

func NewTimeStat() *TimeStat

func (*TimeStat) Point

func (self *TimeStat) Point() uint64

type TimeoutCond

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

func NewTimeoutCond

func NewTimeoutCond(l sync.Locker) *TimeoutCond

func (*TimeoutCond) Broadcast

func (t *TimeoutCond) Broadcast()

func (*TimeoutCond) Lock

func (t *TimeoutCond) Lock()

func (*TimeoutCond) Signal

func (t *TimeoutCond) Signal()

func (*TimeoutCond) Unlock

func (t *TimeoutCond) Unlock()

func (*TimeoutCond) Wait

func (t *TimeoutCond) Wait()

func (*TimeoutCond) WaitFor

func (t *TimeoutCond) WaitFor(ms int) bool

false if timeout, else return true

type Timer

type Timer struct {
	Id        uint32
	Obj       TimerObj
	AbsTime   uint64
	TimerType int
}

type TimerObj

type TimerObj interface {
	OnTimeout(timer *Timer)
}

type TimerThread

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

func NewTimerThread

func NewTimerThread() *TimerThread

func (*TimerThread) AddTimer

func (self *TimerThread) AddTimer(timeoutMs uint32, timeType int, obj TimerObj) uint32

func (*TimerThread) DelTimer

func (self *TimerThread) DelTimer(timerId uint32)

func (*TimerThread) Stop

func (self *TimerThread) Stop()

type Waitlock

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

func NewWaitlock

func NewWaitlock() *Waitlock

func (*Waitlock) Lock

func (self *Waitlock) Lock(waitMs int) (int, error)

func (*Waitlock) Unlock

func (self *Waitlock) Unlock()

Jump to

Keyboard shortcuts

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