utils

package
v1.6.0 Latest Latest
Warning

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

Go to latest
Published: May 25, 2023 License: Apache-2.0 Imports: 15 Imported by: 37

Documentation

Index

Constants

View Source
const JsonExt = ".json"
View Source
const NeverExpire time.Duration = -1

Variables

View Source
var ErrIgnore = errors.New("error ignore")

Functions

func CacheTime

func CacheTime() string

CacheTime returns a time cache in seconds. we use a cache to reduce the format

func Dup

func Dup(from, to int) error

func GenerateUUID

func GenerateUUID() string

GenerateUUID generates an uuid https://tools.ietf.org/html/rfc4122 crypto.rand use getrandom(2) or /dev/urandom It is maybe occur an error due to system error panic if an error occurred

func GoWithRecover

func GoWithRecover(handler func(), recoverHandler func(r interface{}))

GoWithRecover wraps a `go func()` with recover()

func ReadJsonFile

func ReadJsonFile(file string, v interface{}) error

ReadJsonFile reads a json into interface If a file is not .json ignore it If a file is empty, ignore it

func RegisterRecoverLogger

func RegisterRecoverLogger(f func(w io.Writer, r interface{}))

RegisterRecoverLogger replace the log handler when go with recover catch a panic notice the replaced handler should be simple. if the handler panic, the recover handler will be failed.

func ResetHjiackStdPipeline

func ResetHjiackStdPipeline()

func SetHijackStdPipeline

func SetHijackStdPipeline(filepath string, stdout, stderr bool)

SetHijackStdPipeline hijacks stdout and stderr outputs into the file path

func WriteFileSafety

func WriteFileSafety(filename string, data []byte, perm os.FileMode) (err error)

WriteFileSafety trys to over write a file safety.

Types

type ExpiredMap

type ExpiredMap struct {

	// When the cache expires, it is used to update the cache.
	UpdateHandler func(interface{}) (interface{}, bool)
	// contains filtered or unexported fields
}

func NewExpiredMap

func NewExpiredMap(handler func(interface{}) (interface{}, bool), syncMod bool) *ExpiredMap

handler is used to update the data if the cache is invalid during Get. syncMod is set true means that the handler is called synchronously, and the others are asynchronous.

func (*ExpiredMap) Get

func (e *ExpiredMap) Get(key interface{}) (interface{}, bool)

Get the cache indexed by key. If the cache is hit, the bool value indicates whether the cache is expired.

func (*ExpiredMap) Set

func (e *ExpiredMap) Set(key, val interface{}, valid time.Duration)

Set a key and val with an expiration time. key and val represent cached index and user data. valid is used to set the expire time of the cache. For example, if valid=10 means the data expires after 10 Duration.

type Mutex

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

func NewMutex

func NewMutex() *Mutex

func (*Mutex) Lock

func (m *Mutex) Lock()

func (*Mutex) TryLock

func (m *Mutex) TryLock(timeout time.Duration) bool

func (*Mutex) Unlock

func (m *Mutex) Unlock()

type SyncList

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

SyncList similar with the builtin List container while it is concurrency safe.

func NewSyncList

func NewSyncList() *SyncList

NewSyncList returns an initialized SyncList.

func (*SyncList) Len

func (l *SyncList) Len() int

Len returns the number of elements of list l.

func (*SyncList) PushBack

func (l *SyncList) PushBack(v interface{}) *list.Element

PushBack inserts a new element e with value v at the back of list l and returns e.

func (*SyncList) Remove

func (l *SyncList) Remove(e *list.Element) interface{}

Remove removes e from l if e is an element of list l. It returns the element value e.Value. The element must not be nil.

func (*SyncList) VisitSafe

func (l *SyncList) VisitSafe(f func(v interface{}))

VisitSafe means the visit function f can visit each element safely even f may block some time. Also, it won't block other operations(e.g. Remove) when the visit function f is blocked. But, it can not run parallel since there is an instance level curr point.

type Ticker

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

Ticker is a thread-safe reusable ticker

func NewTicker

func NewTicker(callback func()) *Ticker

func (*Ticker) Close

func (t *Ticker) Close()

Close closes the ticker.

func (*Ticker) Start

func (t *Ticker) Start(interval time.Duration)

Start starts a ticker running if it is not started

func (*Ticker) Stop

func (t *Ticker) Stop()

Stop stops the ticker.

type Timer

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

func NewTimer

func NewTimer(d time.Duration, callback func()) *Timer

func (*Timer) Reset

func (t *Timer) Reset(d time.Duration) bool

func (*Timer) Stop

func (t *Timer) Stop()

Stop stops the timer.

Jump to

Keyboard shortcuts

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