common

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Aug 4, 2021 License: Apache-2.0 Imports: 18 Imported by: 1

Documentation

Index

Constants

View Source
const (
	// CallbacksInitCapacity is the capacity of callback functions.
	CallbacksInitCapacity = 20

	// NormalPriorityCallback is the name of normal priority callback.
	NormalPriorityCallback = "__NoRmAl_PrIoRiTy_CaLlBaCk"
	// CriticalPriorityCallback is the name of critical priority callback.
	CriticalPriorityCallback = "__CrItIcAl_PrIoRiTy_CaLlBaCk"
)
View Source
const TokenEscapeChar string = `\`

TokenEscapeChar is the escape char

Variables

View Source
var (
	// TrueStrings contains all string represents true
	TrueStrings = []string{"1", "t", "true", "on", "y", "yes"}
	// FalseStrings contains all string represents false
	FalseStrings = []string{"0", "f", "false", "off", "n", "no"}
)
View Source
var URLFriendlyCharactersRegex = regexp.MustCompile(`^[A-Za-z0-9\-_\.~]{1,253}$`)

URLFriendlyCharactersRegex - safe characters for friendly url, rfc3986 section 2.3

Functions

func B2s

func B2s(b []byte) string

B2s converts byte slice to a string without memory allocation. See https://groups.google.com/forum/#!msg/Golang-Nuts/ENgbUzYvCuU/90yGx7GUAgAJ .

Note it may break if string and/or slice header will change in the future go versions.

func BackupAndCleanDir

func BackupAndCleanDir(dir string) error

BackupAndCleanDir cleans old stuff in both dir and backupDir, and backups dir to backupDir. The backupDir generated by appending postfix `_bak` for dir. It does nothing if dir does not exist.

func Exit

func Exit(code int, msg string)

Exit the call exit system call with exit code and message

func ExpandDir

func ExpandDir(dir string) string

ExpandDir cleans the dir, and returns itself if it's absolute, otherwise prefix with the current/working directory.

func GraphiteSplit

func GraphiteSplit(s string, lensep string, sep string) []string

GraphiteSplit slices s into all substrings separated by sep returns a slice of the substrings without length prefix separated by lensep. The routine does its best to split without error returned.

func IsDirEmpty

func IsDirEmpty(name string) bool

IsDirEmpty returns true if a directory is empty.

func MkdirAll

func MkdirAll(path string) error

MkdirAll wraps os.MakeAll with fixed perm.

func NextNumberPowerOf2

func NextNumberPowerOf2(v uint64) uint64

NextNumberPowerOf2 return the number of power of 2 Via: https://stackoverflow.com/a/466242/1705845

https://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2

func NormalizeZapLogPath added in v1.2.0

func NormalizeZapLogPath(path string) string

NormalizeZapLogPath is a workaround for https://github.com/uber-go/zap/issues/621 the workaround is from https://github.com/ipfs/go-log/issues/73

func NotifySignal added in v1.2.0

func NotifySignal(c chan<- Signal, sig ...Signal) error

NotifySignal is identical to os/signal.Notify on Linux param is mapped to abstract Signal and nil is not allowed

func Now

func Now() time.Time

Now is the current time

func NowUnixNano

func NowUnixNano() int64

NowUnixNano is the current Unix Nano time

func PanicToErr

func PanicToErr(f func(), err *error) (failed bool)

PanicToErr try to recover panic and returns if it happened or not.

func RaiseSignal added in v1.2.0

func RaiseSignal(pid int, sig Signal) error

RaiseSignal is identical to syscall.Kill on Linux param is mapped to abstract Signal

any chan passed to NotifySignal will receive the sig

func RemoveAll

func RemoveAll(path string) error

RemoveAll wraps os.RemoveAll.

func RemoveRepeatedByte

func RemoveRepeatedByte(s string, needRemoveByte byte) string

RemoveRepeatedByte removes the repeated bytes.

func S2b

func S2b(s string) []byte

S2b converts string to a byte slice without memory allocation.

Note it may break if string and/or slice header will change in the future go versions.

func ScanTokens

func ScanTokens(str string, removeEscapeChar bool, visitor TokenVisitor) (string, error)

ScanTokens scans tokens.

func Since

func Since(t time.Time) time.Duration

Since returns the elapsed time.

func SysProcAttr

func SysProcAttr() *syscall.SysProcAttr

SysProcAttr returns system process attribute

func UUID

func UUID() (string, error)

UUID returns an UUID

func ValidateName

func ValidateName(name string) error

ValidateName validates the name.

Types

type ExpDecaySample

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

ExpDecaySample is the structure use for monitoring metrics(such as: P90, P50 etc)

func NewExpDecaySample

func NewExpDecaySample(timeRange time.Duration, secondsForEachBucket int) *ExpDecaySample

NewExpDecaySample creates ExpDecaySample structure

func (*ExpDecaySample) Close

func (s *ExpDecaySample) Close()

Close cleans the ExpDecaySample.

func (*ExpDecaySample) Count

func (s *ExpDecaySample) Count() int64

Count returns the count value

func (*ExpDecaySample) Max

func (s *ExpDecaySample) Max() int64

Max returns the max value

func (*ExpDecaySample) Min

func (s *ExpDecaySample) Min() int64

Min returns the min value

func (*ExpDecaySample) Percentile

func (s *ExpDecaySample) Percentile(q float64) float64

Percentile calculates the percentile

func (*ExpDecaySample) StdDev

func (s *ExpDecaySample) StdDev() float64

StdDev return the standard deviation value

func (*ExpDecaySample) Sum

func (s *ExpDecaySample) Sum() int64

Sum returns the sum value

func (*ExpDecaySample) Update

func (s *ExpDecaySample) Update(v int64)

Update updates the ExpDecaySample

func (*ExpDecaySample) Variance

func (s *ExpDecaySample) Variance() float64

Variance returns the variance value

type Lazybuf

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

A Lazybuf is a lazily constructed path buffer. It supports append, reading previously appended bytes, and retrieving the final string. It does not allocate a buffer to hold the output until that output diverges from s.

func NewLazybuf

func NewLazybuf(s string) *Lazybuf

NewLazybuf returns lazy buffer

func (*Lazybuf) Append

func (b *Lazybuf) Append(c byte)

Append appends a byte into buffer

func (*Lazybuf) Index

func (b *Lazybuf) Index(i int) byte

Index returns a byte by its index

func (*Lazybuf) String

func (b *Lazybuf) String() string

String returns the string of lazybuf

type NamedCallback

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

NamedCallback is the named callback struct

func NewNamedCallback

func NewNamedCallback(name string, callback interface{}) *NamedCallback

NewNamedCallback returns a new named callback.

func (*NamedCallback) Callback

func (cb *NamedCallback) Callback() interface{}

Callback returns callback function

func (*NamedCallback) Name

func (cb *NamedCallback) Name() string

Name returns callback name

func (*NamedCallback) SetCallback

func (cb *NamedCallback) SetCallback(callback interface{}) interface{}

SetCallback sets the callback function

type NamedCallbackSet

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

NamedCallbackSet is a set of NamedCallback

func AddCallback

func AddCallback(cbs *NamedCallbackSet, name string, callback interface{}, priority string) *NamedCallbackSet

AddCallback adds a callback into a callback set with priority

func DeleteCallback

func DeleteCallback(cbs *NamedCallbackSet, name string) *NamedCallbackSet

DeleteCallback deletes a callback in callback set

func NewNamedCallbackSet

func NewNamedCallbackSet() *NamedCallbackSet

NewNamedCallbackSet returns a new NamedCallbackSet

func (*NamedCallbackSet) CopyCallbacks

func (cbs *NamedCallbackSet) CopyCallbacks() []*NamedCallback

CopyCallbacks copies the current callbacks

func (*NamedCallbackSet) GetCallbacks

func (cbs *NamedCallbackSet) GetCallbacks() []*NamedCallback

GetCallbacks get callbacks

type NumericAvgAggregator

type NumericAvgAggregator struct {
	NumericSumAggregator
	// contains filtered or unexported fields
}

NumericAvgAggregator is the structure with average value

func (*NumericAvgAggregator) Aggregate

func (a *NumericAvgAggregator) Aggregate(num interface{}) error

Aggregate records the number of values and the sum of values

func (*NumericAvgAggregator) Result

func (a *NumericAvgAggregator) Result() interface{}

Result returns the average values

func (*NumericAvgAggregator) String

func (a *NumericAvgAggregator) String() string

String returns the name.

type NumericMaxAggregator

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

NumericMaxAggregator is the structure with max value

func (*NumericMaxAggregator) Aggregate

func (a *NumericMaxAggregator) Aggregate(num interface{}) error

Aggregate records the max of value

func (*NumericMaxAggregator) Result

func (a *NumericMaxAggregator) Result() interface{}

Result return the max value

func (*NumericMaxAggregator) String

func (a *NumericMaxAggregator) String() string

String returns the name.

type NumericMinAggregator

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

NumericMinAggregator is the structure with min value

func (*NumericMinAggregator) Aggregate

func (a *NumericMinAggregator) Aggregate(num interface{}) error

Aggregate records the min value

func (*NumericMinAggregator) Result

func (a *NumericMinAggregator) Result() interface{}

Result returns the min value.

func (*NumericMinAggregator) String

func (a *NumericMinAggregator) String() string

String returns the name.

type NumericSumAggregator

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

NumericSumAggregator is the structure with sum value

func (*NumericSumAggregator) Aggregate

func (a *NumericSumAggregator) Aggregate(num interface{}) error

Aggregate records the sum value

func (*NumericSumAggregator) Result

func (a *NumericSumAggregator) Result() interface{}

Result returns the sum value.

func (*NumericSumAggregator) String

func (a *NumericSumAggregator) String() string

String returns the name.

type Signal added in v1.2.0

type Signal string

Signal is cross platform abstract type of os.Signal

const (
	// SignalInt represents quit in Easegress
	SignalInt Signal = "int"
	// SignalTerm represents force quit in Easegress
	SignalTerm Signal = "term"

	// SingalUsr2 represents reload signal in Easegress
	SingalUsr2 Signal = "usr2"
)

type StatAggregator

type StatAggregator interface {
	Aggregate(interface{}) error
	Result() interface{}
	String() string
}

StatAggregator is the interface

type TokenVisitor

type TokenVisitor func(pos int, token string) (care bool, replacement string)

TokenVisitor uses for visit token

Jump to

Keyboard shortcuts

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