utils

package
v0.78.5 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2020 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func All

func All(actions ...func()) func()

All run all actions concurrently, returns the wait function for all actions.

func DefaultBackoff

func DefaultBackoff(interval time.Duration) time.Duration

DefaultBackoff algorithm: A(n) = A(n-1) * random[1.9, 2.1)

func Dump

func Dump(list ...interface{}) string

Dump values for debugging

func E

func E(args ...interface{}) []interface{}

E if the last arg is error, panic it

func EscapeGoString

func EscapeGoString(s string) string

EscapeGoString not using encoding like base64 or gzip because of they will make git diff every large for small change

func Exec

func Exec(name string, args ...string)

Exec command

func FileExists

func FileExists(path string) bool

FileExists checks if file exists, only for file, not for dir

func Mkdir

func Mkdir(path string) error

Mkdir makes dir recursively

func MustToJSON

func MustToJSON(data interface{}) string

MustToJSON encode data to json string

func MustToJSONBytes

func MustToJSONBytes(data interface{}) []byte

MustToJSONBytes encode data to json bytes

func OutputFile

func OutputFile(p string, data interface{}) error

OutputFile auto creates file if not exists, it will try to detect the data type and auto output binary, string or json

func Pause

func Pause()

Pause the goroutine forever

func RandString

func RandString(len int) string

RandString generate random string with specified string length

func ReadString

func ReadString(p string) (string, error)

ReadString reads file as string

func Retry

func Retry(ctx context.Context, s Sleeper, fn func() (stop bool, err error)) error

Retry fn and sleeper until fn returns true or s returns error

func S

func S(tpl string, params ...interface{}) string

S Template render, the params is key-value pairs

func SetCmdStdPipe

func SetCmdStdPipe(cmd *exec.Cmd)

SetCmdStdPipe command

func Sleep

func Sleep(seconds float64)

Sleep the goroutine for specified seconds, such as 2.3 seconds

Types

type IdleCounter

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

IdleCounter is similar to sync.WaitGroup but it only resolves if no jobs for specified duration.

func NewIdleCounter

func NewIdleCounter(d time.Duration) *IdleCounter

NewIdleCounter ...

func (*IdleCounter) Add

func (de *IdleCounter) Add()

Add ...

func (*IdleCounter) Done

func (de *IdleCounter) Done()

Done ...

func (*IdleCounter) Wait

func (de *IdleCounter) Wait(ctx context.Context)

Wait ...

type Log

type Log func(msg ...interface{})

Log type for Println

func MultiLogger

func MultiLogger(list ...Logger) Log

MultiLogger is similar to https://golang.org/pkg/io/#MultiWriter

func (Log) Println

func (l Log) Println(msg ...interface{})

Println interface

type Logger

type Logger interface {
	// Same as fmt.Printf
	Println(...interface{})
}

Logger interface

var LoggerQuiet Logger = loggerQuiet{}

LoggerQuiet does nothing

type Sleeper

type Sleeper func(context.Context) error

Sleeper sleeps the current gouroutine for sometime, returns the reason to wake, if ctx is done release resource

func BackoffSleeper

func BackoffSleeper(init, maxInterval time.Duration, algorithm func(time.Duration) time.Duration) Sleeper

BackoffSleeper returns a sleeper that sleeps in a backoff manner every time get called. If algorithm is nil, DefaultBackoff will be used. Set interval and maxInterval to the same value to make it a constant sleeper. If maxInterval is not greater than 0, the sleeper will wake immediately.

func CountSleeper

func CountSleeper(max int) Sleeper

CountSleeper wake immediately. When counts to the max returns errors.New("max sleep count")

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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