util

package
v0.0.0-...-f49aa48 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2024 License: Apache-2.0 Imports: 28 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BoolToInt64

func BoolToInt64(b bool) int64

BoolToInt64 converts bool to int64

func BuildPeriod

func BuildPeriod(interval int64) int64

func BuildTagsKey

func BuildTagsKey(tags map[string]string) string

BuildTagsKey build string from tags "key1=value1 key2=value2 key3=value3" key1 ... keyn is sorted

func ConvertStringSliceToHashSet

func ConvertStringSliceToHashSet(a []string) map[string]struct{}

func CopyConn

func CopyConn(ctx context.Context, conn1 io.ReadWriter, conn2 io.ReadWriter, timeout time.Duration) error

func CopyFileUsingCp

func CopyFileUsingCp(ctx context.Context, src, dst string) error

CopyFileUsingCp copies file using cp binary (in agent container)

func CopyStringMap

func CopyStringMap(m map[string]string) map[string]string

func CopyStringMapCap

func CopyStringMapCap(m map[string]string, cap int) map[string]string

func CopyStringSlice

func CopyStringSlice(src []string) []string

func CreateDirIfNotExists

func CreateDirIfNotExists(dir string, perm os.FileMode) error

CreateDirIfNotExists creates dir if it does not exist

func CreateHttpClientWithDialContext

func CreateHttpClientWithDialContext(dial func(ctx context.Context, network, addr string) (net.Conn, error)) (*http.Transport, *http.Client, error)

CreateHttpClientWithDialContext create a httpclient with dialcontext func

func CurrentMS

func CurrentMS() int64

func DeepCopyString

func DeepCopyString(s string) string

DeepCopyString deep copy string

func DeepCopyStringSlice

func DeepCopyStringSlice(a []string) []string

func FirstNotEmpty

func FirstNotEmpty(strs ...string) string

返回第一个非空的字符串, 如果全部为空则返回空

func GetEnvOrDefault

func GetEnvOrDefault(name, defaultValue string) string

func GetHostname

func GetHostname() string

GetHostname

func GetLocalIp

func GetLocalIp() string

GetLocalIp get local ipv4

func GetLocalTimezone

func GetLocalTimezone() string

func GobDecode

func GobDecode(b []byte, obj interface{}) error

func GobEncode

func GobEncode(obj interface{}) ([]byte, error)

func IsContextDone

func IsContextDone(ctx context.Context) bool

func IsContextNotDone

func IsContextNotDone(ctx context.Context) bool

func IsLinux

func IsLinux() bool

func IsNotImplemented

func IsNotImplemented(err error) bool

func MaybeCloseRead

func MaybeCloseRead(conn interface{}) error

func MaybeCloseWrite

func MaybeCloseWrite(conn interface{}) error

func MaybeIOClose

func MaybeIOClose(i interface{}) error

func MaybeSetDeadline

func MaybeSetDeadline(i interface{}, d time.Time) error

func MergeStringMapTo

func MergeStringMapTo(a map[string]string, to map[string]string, override bool) map[string]string

MergeStringMapTo merges one string map to another

func NewClientTLSFromBase64

func NewClientTLSFromBase64(certBase64, serverNameOverride string) (credentials.TransportCredentials, error)

func NextDelay

func NextDelay(interval time.Duration) time.Duration

NextDelay 返回下一个时间窗口的delay

func NextDelayFrom

func NextDelayFrom(now time.Time, interval time.Duration) time.Duration

func OpenFileReadonly

func OpenFileReadonly(file string) (*os.File, error)

func ParseDuration

func ParseDuration(d interface{}) (time.Duration, error)

ParseDuration parse any object that like a duration str to duration for example: "100ms" -> 100ms 100 -> 100ms 100.0 -> 100ms

func ParseDurationDefault

func ParseDurationDefault(s string, d time.Duration) time.Duration

func ParseStringSliceEnvToMap

func ParseStringSliceEnvToMap(envs []string) map[string]string

ParseStringSliceEnvToMap parse envs from []string to map[string]string

func RandBetween

func RandBetween(min, max int) int

func RandBetween64

func RandBetween64(min, max int64) int64

func ReplaceHost

func ReplaceHost(hostport string, host string) string

func ReplaceHostToLocalhost

func ReplaceHostToLocalhost(hostport string) string

func ReverseStringSlice

func ReverseStringSlice(a []string)

func SetLocalIp

func SetLocalIp(ip string)

func String

func String(b []byte) string

zero cost string conversion this may cause unexpected behavior, use this carefully

func StringSliceContains

func StringSliceContains(elems []string, elem string) bool

func StringSliceFind

func StringSliceFind(elems []string, elem string) int

func SubBytesMax

func SubBytesMax(bs []byte, max int) []byte

SubBytesMax returns a sub slice with max length limit

func SubContextTimeout

func SubContextTimeout(ctx context.Context, timeout time.Duration, callback func(context.Context))

func SubContextTimeoutE

func SubContextTimeoutE(ctx context.Context, timeout time.Duration, callback func(context.Context) error) error

func SubstringMax

func SubstringMax(s string, n int) string

func SyncMapSize

func SyncMapSize(m *sync.Map) int

func TimeTruncateToDay

func TimeTruncateToDay(t time.Time) time.Time

func ToJsonBuffer

func ToJsonBuffer(v interface{}) *bytes.Buffer

func ToJsonBufferE

func ToJsonBufferE(v interface{}) (*bytes.Buffer, error)

func ToJsonBytes

func ToJsonBytes(v interface{}) []byte

func ToJsonString

func ToJsonString(v interface{}) string

ToJsonString convert v to json string ignoring any error

func ToMS

func ToMS(t time.Time) int64

func TransformStringSlice

func TransformStringSlice(strs []string, transformer func(string) string) []string

func ZeroCopyStringToBytes

func ZeroCopyStringToBytes(s string) (b []byte)

Types

type AlignedTimer

type AlignedTimer struct {

	// C returns the time chan.
	// Note that the value in chan may be different with the value returned by Next().
	// Please refer to the value returned by Next().
	C <-chan time.Time
	// contains filtered or unexported fields
}

AlignedTimer emit aligned time.Time. [align+offset, 2*align+offset, 3*align+offset, ...] When skipExpiredWindow is true, if wall time is after next 'n*align+offset' when calling Next(), it is skipped, and the next emit time will be (n+1)*align+offset. When skipExpiredWindow is false, if wall time is after next 'n*align+offset' when calling Next(), it will trigger an emit right now. If skipFirstTimer is true, it ensures that the (first emit time - now) >= align. Otherwise, first emit may be trigger within 'align' time. Example: align=5s offset=2s, skipExpiredWindow=true, skipFirstTimer=false Now is 00:00:6s, the emitted times will be: 00:00:07, 00:00:12, 00:00:17, ... Assuming task executing at 00:00:07 is very heavy, and when it is completed, the time exceeds 00:00:12. So the next emit time is 00:00:17. The emitted times will be: 00:00:07, 00:00:17, ...

func NewAlignedTimer

func NewAlignedTimer(align, offset time.Duration, skipExpiredWindow bool, skipFirstTimer bool) (*AlignedTimer, time.Time)

func (*AlignedTimer) LoadState

func (t *AlignedTimer) LoadState(b []byte) error

LoadState loads timer state from []byte

func (*AlignedTimer) Next

func (t *AlignedTimer) Next() time.Time

Next triggers emit, returns next emit time. The returned time may before time.Now() if 'skipExpiredWindow' is false.

func (*AlignedTimer) NextEmitTime

func (t *AlignedTimer) NextEmitTime() time.Time

NextEmitTime returns expected next emit time. It may have: 'next emit time' < 'time.Now()'

func (*AlignedTimer) SaveState

func (t *AlignedTimer) SaveState() ([]byte, error)

SaveState saves current timer state into []byte

func (*AlignedTimer) Stop

func (t *AlignedTimer) Stop()

Stop tops the timer

type DnsCacheHelper

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

func NewDnsCacheHelper

func NewDnsCacheHelper() *DnsCacheHelper

func (*DnsCacheHelper) Dial

func (h *DnsCacheHelper) Dial(ctx context.Context, network, addr string) (net.Conn, error)

func (*DnsCacheHelper) NewHttpClient

func (h *DnsCacheHelper) NewHttpClient() *http.Client

func (*DnsCacheHelper) Start

func (h *DnsCacheHelper) Start()

func (*DnsCacheHelper) Stop

func (h *DnsCacheHelper) Stop()

type ReadWriterConn

type ReadWriterConn struct {
	io.Reader
	io.Writer
	CloseFunc context.CancelFunc
}

func (*ReadWriterConn) Close

func (r *ReadWriterConn) Close() error

func (*ReadWriterConn) CloseRead

func (r *ReadWriterConn) CloseRead() error

func (*ReadWriterConn) CloseWrite

func (r *ReadWriterConn) CloseWrite() error

func (*ReadWriterConn) LocalAddr

func (r *ReadWriterConn) LocalAddr() net.Addr

func (*ReadWriterConn) RemoteAddr

func (r *ReadWriterConn) RemoteAddr() net.Addr

func (*ReadWriterConn) SetDeadline

func (r *ReadWriterConn) SetDeadline(t time.Time) error

func (*ReadWriterConn) SetReadDeadline

func (r *ReadWriterConn) SetReadDeadline(t time.Time) error

func (*ReadWriterConn) SetWriteDeadline

func (r *ReadWriterConn) SetWriteDeadline(t time.Time) error

type ReaderCloserFunc

type ReaderCloserFunc struct {
	Reader io.Reader
	Closer func()
}

ReaderCloserFunc wraps an io.Reader. It calls Closer func when reader meets io.EOF.

func (*ReaderCloserFunc) Read

func (s *ReaderCloserFunc) Read(p []byte) (int, error)

type ReaderReadHook

type ReaderReadHook struct {
	Reader io.Reader
	Before func()
	After  func(p []byte, n int, err error)
}

func (*ReaderReadHook) Close

func (s *ReaderReadHook) Close() error

func (*ReaderReadHook) Read

func (s *ReaderReadHook) Read(p []byte) (int, error)

type Signal

type Signal struct {
}

func NewSignal

func NewSignal() *Signal

type SortStringsByLength

type SortStringsByLength []string

func (SortStringsByLength) Len

func (s SortStringsByLength) Len() int

func (SortStringsByLength) Less

func (s SortStringsByLength) Less(i, j int) bool

func (SortStringsByLength) Swap

func (s SortStringsByLength) Swap(i, j int)

type StopSignal

type StopSignal struct {
	C chan struct{}
	// contains filtered or unexported fields
}

func NewStopSignal

func NewStopSignal() *StopSignal

func (*StopSignal) IsStopAsked

func (s *StopSignal) IsStopAsked() bool

func (*StopSignal) Stop

func (s *StopSignal) Stop()

Stop must only be call once

func (*StopSignal) StopAndWait

func (s *StopSignal) StopAndWait()

func (*StopSignal) StopDone

func (s *StopSignal) StopDone()

StopDone must only be call once

func (*StopSignal) WaitStopped

func (s *StopSignal) WaitStopped()

type WriterWriteHook

type WriterWriteHook struct {
	Writer io.Writer
	Before func()
	After  func(p []byte, n int, err error)
}

func (*WriterWriteHook) Close

func (s *WriterWriteHook) Close() error

func (*WriterWriteHook) Write

func (s *WriterWriteHook) Write(p []byte) (int, error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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