utils

package
v0.0.0-...-432d548 Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2024 License: Apache-2.0 Imports: 35 Imported by: 0

Documentation

Index

Constants

View Source
const (
	HTTPContentTypeWWW   = "application/x-www-form-urlencoded"
	HTTPContentTypeJSON  = "application/json"
	HTTPContentTypeXML   = "application/xml"
	HTTPContentTypePLAIN = "text/plain"
	HTTPContentTypeHTML  = "text/html"
)

HttpContentTypes

View Source
const FieldSep = "/"

FieldSep is the seperator for object property full key.

Variables

View Source
var (
	ErrTimeout    = fmt.Errorf("get ticket timeout")
	ErrExeTimeout = fmt.Errorf("execute timeout")
	ErrStoped     = fmt.Errorf("executor stoped")
	ErrNoTicket   = fmt.Errorf("no ticket right now")
)

Err defines

Functions

func AbsInt32

func AbsInt32(src int32) int32

AbsInt32 AbsInt32

func Addr

func Addr(sv reflect.Value) reflect.Value

Addr return address of value

func AppendString

func AppendString(strs ...string) string

AppendString link strings

func BoolPtr

func BoolPtr(v bool) *bool

BoolPtr get ptr of value

func ByteCastString

func ByteCastString(b []byte) string

ByteCastString change byte to string

func ClientIP

func ClientIP(req *http.Request) string

ClientIP get http request client ip

func CompressStringToString

func CompressStringToString(content string) string

CompressStringToString compress string to base64String used gzip

func ConvertToStringKV

func ConvertToStringKV(i map[interface{}]interface{}) map[string]interface{}

ConvertToStringKV convert map[interface{}]interface{} to map[string]interface{}

func DecodeMap

func DecodeMap(decoder *json.Decoder) (map[string]interface{}, error)

DecodeMap ...

func DecodeMapString

func DecodeMapString(s string) (map[string]interface{}, error)

DecodeMapString ...

func DecompressStringToString

func DecompressStringToString(compressedContent string) (string, error)

DecompressStringToString decompress base64String to string used gzip

func DeepCopyObject

func DeepCopyObject(st interface{}, dst interface{}) error

DeepCopyObject deepcopy by reflect copy

func ElemType

func ElemType(v interface{}) reflect.Type

ElemType return the elem type of slice or ptr

func FindField

func FindField(st interface{}, ks []string) (reflect.Value, error)

FindField find field value of st

func FindFieldByTag

func FindFieldByTag(rv reflect.Value, tagType, key string) (reflect.Value, bool)

FindFieldByTag find field by name or json tag

func FixHTTPUrl

func FixHTTPUrl(url string) string

FixHTTPUrl app http:// to url

func FloatRound

func FloatRound(f float64, n int) float64

FloatRound is float64 precesion convert

func GetFieldElemValue

func GetFieldElemValue(st interface{}, fullKey string) (interface{}, error)

GetFieldElemValue return the elem Value of field

func GetFieldValue

func GetFieldValue(st interface{}, fullKey string) (interface{}, error)

GetFieldValue return the Value of field

func GetLocalIP

func GetLocalIP() (string, error)

GetLocalIP get ip by lookup dns

func GetMapFieldValue

func GetMapFieldValue(kvs map[string]interface{}, fullKey string) (interface{}, error)

GetMapFieldValue GetMapFieldValue

func GetMaxLengthCommonString

func GetMaxLengthCommonString(str1, str2 string) string

GetMaxLengthCommonString GetMaxLengthCommonString

func GetMaxLengthSubRepeatString

func GetMaxLengthSubRepeatString(str string) string

GetMaxLengthSubRepeatString GetMaxLengthSubRepeatString

func GobDeepCopy

func GobDeepCopy(src interface{}, dst interface{}) error

GobDeepCopy deepcopy by gob marshal and unmarshal

func HTTPRecover

func HTTPRecover(panicReason interface{}, httpWriter http.ResponseWriter)

HTTPRecover send 500 code response when panic

func HashString64

func HashString64(b []byte) uint64

HashString64 hash string to uint64

func Indirect

func Indirect(v reflect.Value) reflect.Value

Indirect returns the value that v points to,or the interface contains If v is a nil pointer, Indirect returns a zero Value. If v is not a pointer, Indirect returns v.

func Int32Ptr

func Int32Ptr(v int32) *int32

Int32Ptr get ptr of value

func Int64Ptr

func Int64Ptr(v int64) *int64

Int64Ptr get ptr of value

func IsFieldZero

func IsFieldZero(st interface{}, fullKey string) error

IsFieldZero find field from st and check if it is zero

func IsFieldsZero

func IsFieldsZero(st interface{}, fullKeys []string) error

IsFieldsZero find fields from st and check if they are zero

func IsSimpleType

func IsSimpleType(t reflect.Type) bool

IsSimpleType return is the type is simple type

func IsZero

func IsZero(v reflect.Value) bool

IsZero copy from go1.13 reflect value.go

func JSONDeepCopy

func JSONDeepCopy(st interface{}, dst interface{}) error

JSONDeepCopy deepcopy by json marshal and unmarshal

func ObjJSON

func ObjJSON(o interface{}) string

ObjJSON get obj json string. easy to use but not safety, used for log

func PProfHandlers

func PProfHandlers() map[string]http.Handler

PProfHandlers get all pprof path and handlers to new http handler

func ParseJSONResp

func ParseJSONResp(resp *http.Response, err error, result interface{}) error

ParseJSONResp parse http json resp to interface

func ParseRespToString

func ParseRespToString(resp *http.Response, err error) (string, error)

ParseRespToString parse http resp to string

func RenderJSON

func RenderJSON(w http.ResponseWriter, code int, data interface{}) error

RenderJSON send json response

func RenderPlain

func RenderPlain(w http.ResponseWriter, code int, format string, args ...interface{}) error

RenderPlain send plain response

func ReplaceStderr

func ReplaceStderr(name string, writeHeader bool) error

ReplaceStderr replaces os.Stderr by a custom file. Usually to write panics

func SetFieldValue

func SetFieldValue(st interface{}, fullKey string, value interface{}) error

SetFieldValue set value to field

func SetFieldValueFromJSON

func SetFieldValueFromJSON(st interface{}, fullKey string, decoder *json.Decoder) (interface{}, error)

SetFieldValueFromJSON SetFieldValueFromJSON

func SetMaxStdFileSize

func SetMaxStdFileSize(size int64)

SetMaxStdFileSize SetMaxStdFileSize

func Signature

func Signature(ins ...interface{}) (string, error)

Signature compute signature for objs

func SignatureShort

func SignatureShort(ins ...interface{}) (string, error)

SignatureShort compute signature for objs and return a short signature

func SignatureWithMD5

func SignatureWithMD5(ins ...interface{}) (string, error)

SignatureWithMD5 compute signature for objs with md5

func Stack

func Stack(skip int) string

Stack returns a nicely formated stack frame, skipping skip frames

func StringCastByte

func StringCastByte(s string) []byte

StringCastByte change string to byte

func StringPtr

func StringPtr(v string) *string

StringPtr get ptr of value

func StripJSONNullValue

func StripJSONNullValue(kv map[string]interface{})

StripJSONNullValue strip "some-key": null

func ToSlice

func ToSlice(arr interface{}) []interface{}

ToSlice change interface{} to []interface{}

func ValidateJsonizeValue

func ValidateJsonizeValue(i interface{}) interface{}

ValidateJsonizeValue used when you want to marshal map[interface{}]interface{}

func ValidateStringKV

func ValidateStringKV(is map[string]interface{}) map[string]interface{}

ValidateStringKV ValidateStringKV

func WriteHistoryRecord

func WriteHistoryRecord(path string, key string, suffix string, keepCnt int, totalCnt int, obj interface{}) error

WriteHistoryRecord records any object to a list of history files.

Types

type AsyncExecutor

type AsyncExecutor struct {
	Max     int
	Tickets chan *struct{}

	sync.RWMutex
	// contains filtered or unexported fields
}

AsyncExecutor is groutine pool

func NewExecutor

func NewExecutor(max int) *AsyncExecutor

NewExecutor create groutine pool

func (*AsyncExecutor) ActiveCount

func (p *AsyncExecutor) ActiveCount() int

ActiveCount get active count

func (*AsyncExecutor) Close

func (p *AsyncExecutor) Close()

Close the pool

func (*AsyncExecutor) CreateRunFunc

func (p *AsyncExecutor) CreateRunFunc(run interface{}) func(params ...interface{}) (results []interface{}, err error)

CreateRunFunc from run func

func (*AsyncExecutor) Do

func (p *AsyncExecutor) Do(ctx context.Context, try bool, run interface{}, params ...interface{}) ([]interface{}, error)

Do runs your function in a synchronous manner,

func (*AsyncExecutor) GetTicket

func (p *AsyncExecutor) GetTicket(ctx context.Context) (*struct{}, error)

GetTicket from pool

func (*AsyncExecutor) Go

func (p *AsyncExecutor) Go(ctx context.Context, try bool, run interface{}, params ...interface{}) (*Command, error)

Go call run

func (*AsyncExecutor) Queue

func (p *AsyncExecutor) Queue(ctx context.Context, try bool, f simpaleRunFunc) error

Queue add a func to pool. in this condition, all user logic must be in f.

func (*AsyncExecutor) Return

func (p *AsyncExecutor) Return(ticket *struct{})

Return ticket to pool

func (*AsyncExecutor) TryGetTicket

func (p *AsyncExecutor) TryGetTicket(ctx context.Context, try bool) (*struct{}, error)

TryGetTicket with no wait

type Batcher

type Batcher struct {
	sync.Mutex
	// contains filtered or unexported fields
}

Batcher exectue f batch and wait result together

func NewBatcher

func NewBatcher(max int) *Batcher

NewBatcher create batcher

func NewBatcherWithExecutor

func NewBatcherWithExecutor(executor *AsyncExecutor) *Batcher

NewBatcherWithExecutor create batcher

func (*Batcher) Go

func (b *Batcher) Go(ctx context.Context, try bool, run interface{}, params ...interface{}) (*Command, error)

Go execute a func async

func (*Batcher) Wait

func (b *Batcher) Wait()

Wait batche executed funcs done

type Command

type Command struct {
	sync.Mutex

	Start         time.Time
	End           time.Time
	ExecutorError error
	FuncError     error
	Results       []interface{}
	// contains filtered or unexported fields
}

Command is the return model, for user to get results and errinfo

type DNSClient

type DNSClient interface {
	Subscribe(name string) error
	Query(name string) (string, error)
}

DNSClient ...

type DiffLogger

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

DiffLogger write logs/record files if object content is different.

func NewDiffLogger

func NewDiffLogger(size int, writer LoggerWriter, keyKeepCount, totalKeepCount int) (*DiffLogger, error)

NewDiffLogger create a new diff logger.

func (*DiffLogger) Log

func (l *DiffLogger) Log(key string, format string, args ...interface{})

Log writes log if log message different.

func (*DiffLogger) WriteHistoryFile

func (l *DiffLogger) WriteHistoryFile(path string, key string, suffix string, obj interface{}, cmpFn func(prev, obj interface{}) (bool, error)) error

WriteHistoryFile write a history record file for object.

type DirtyObject

type DirtyObject struct {
	UpdateAt int64
	CommitAt int64
	// contains filtered or unexported fields
}

DirtyObject tracks its state which expected to persist. When the state changed, it's dirty, and after it's committed, it's not dirty.

func NewDirtyObject

func NewDirtyObject() *DirtyObject

NewDirtyObject creates a new dirty object.

func (*DirtyObject) GetRoot

func (d *DirtyObject) GetRoot() *DirtyObject

GetRoot get the root dirty object.

func (*DirtyObject) IsDirty

func (d *DirtyObject) IsDirty() bool

IsDirty check if this object is dirty.

func (*DirtyObject) LinkDirty

func (d *DirtyObject) LinkDirty(child *DirtyObject)

LinkDirty links 2 dirty objects.

func (*DirtyObject) SetDirty

func (d *DirtyObject) SetDirty()

SetDirty marks this object dirty.

func (*DirtyObject) Stringify

func (d *DirtyObject) Stringify() string

Stringify for logging.

func (*DirtyObject) TransactDirty

func (d *DirtyObject) TransactDirty(fn func() error) error

TransactDirty transact commit if you want to persist dirty object state into db or somewhere...

type Errors

type Errors []error

Errors used for collect errors of parallel process

func FromErrors

func FromErrors(errs []error) Errors

FromErrors create Errors by a list of errors

func NewErrors

func NewErrors() Errors

NewErrors create errors

func (*Errors) Add

func (e *Errors) Add(err error)

Add new error to errors

func (*Errors) Empty

func (e *Errors) Empty() bool

Empty is errors empty

func (*Errors) Error

func (e *Errors) Error() error

String is errors empty

func (*Errors) String

func (e *Errors) String() string

String is errors empty

type FreqLimiter

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

FreqLimiter limits to run a function.

func NewFreqLimiter

func NewFreqLimiter(max int32) *FreqLimiter

NewFreqLimiter creates a limiter

func (*FreqLimiter) Run

func (f *FreqLimiter) Run(fn func()) bool

Run runs the function

type GracefulSigHandler

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

GracefulSigHandler GracefulSigHandler

func NewGracefulSigHandler

func NewGracefulSigHandler(pctx context.Context) (*GracefulSigHandler, context.Context)

NewGracefulSigHandler NewGracefulSigHandler

func (*GracefulSigHandler) NotifySigs

func (g *GracefulSigHandler) NotifySigs(signals ...os.Signal)

NotifySigs NotifySigs

func (*GracefulSigHandler) Wait

func (g *GracefulSigHandler) Wait(maxWait, blockingTime time.Duration, closeAllRoutine func()) error

Wait Wait

type HTTPClient

type HTTPClient interface {
	Get(url string) (*LiteHTTPResp, error)
	Post(url string, body string) (*LiteHTTPResp, error)
	Put(url string, body string) (*LiteHTTPResp, error)
	Delete(url string) (*LiteHTTPResp, error)
}

HTTPClient HTTPClient

type HTTPClientOptions

type HTTPClientOptions struct {
	Timeout                      time.Duration
	Headers                      map[string]string
	TransportMaxIdleConns        int
	TransportMaxIdleConnsPerHost int
}

HTTPClientOptions is options of client scop

type LiteHTTPClient

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

LiteHTTPClient LiteHTTPClient

func NewLiteHTTPClient

func NewLiteHTTPClient(timeout time.Duration) *LiteHTTPClient

NewLiteHTTPClient create new LiteHTTPClient

func NewLiteHTTPClientOpts

func NewLiteHTTPClientOpts(opts *HTTPClientOptions) *LiteHTTPClient

NewLiteHTTPClientOpts create new LiteHTTPClient

func NewLiteHTTPClientWithClient

func NewLiteHTTPClientWithClient(httpClient *http.Client) *LiteHTTPClient

NewLiteHTTPClientWithClient create new LiteHTTPClient

func (*LiteHTTPClient) Delete

func (c *LiteHTTPClient) Delete(url string) (*LiteHTTPResp, error)

Delete query with DELETE method

func (*LiteHTTPClient) Get

func (c *LiteHTTPClient) Get(url string) (*LiteHTTPResp, error)

Get query with GET method

func (*LiteHTTPClient) Post

func (c *LiteHTTPClient) Post(url string, body string) (*LiteHTTPResp, error)

Post query with POST method

func (*LiteHTTPClient) ProxyRequest

func (c *LiteHTTPClient) ProxyRequest(host string, wr http.ResponseWriter, r *http.Request) error

ProxyRequest proxy request to target url

func (*LiteHTTPClient) Put

func (c *LiteHTTPClient) Put(url string, body string) (*LiteHTTPResp, error)

Put query with PUT method

func (*LiteHTTPClient) Request

func (c *LiteHTTPClient) Request(ctx context.Context, url string, body string, method string, headers map[string]string) (*LiteHTTPResp, error)

Request send query

type LiteHTTPResp

type LiteHTTPResp struct {
	Body       []byte
	StatusCode int
	Headers    http.Header
}

LiteHTTPResp contain resp body and status

func SendFormRequest

func SendFormRequest(ctx context.Context, method, urls string, reqForm map[string]string, headers map[string]string) (*LiteHTTPResp, error)

SendFormRequest send http request with form format

func SendJSONRequest

func SendJSONRequest(ctx context.Context, method, url string, req interface{}, headers map[string]string) (*LiteHTTPResp, error)

SendJSONRequest send http request with json format

func SendRawRequest

func SendRawRequest(ctx context.Context, url string, body string, method string, headers map[string]string) (*LiteHTTPResp, error)

type LoggerWriter

type LoggerWriter func(msg string)

LoggerWriter is a function to write logs, e.g: glog.Infof

type LoopJob

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

LoopJob do job periodic

func (*LoopJob) CheckStop

func (l *LoopJob) CheckStop(timeout int) bool

CheckStop executed in loop, check if it should exit

func (*LoopJob) MarkStop

func (l *LoopJob) MarkStop()

MarkStop mark job stop, executed when you want to stop loopjob

func (*LoopJob) Start

func (l *LoopJob) Start()

Start job

func (*LoopJob) Stop

func (l *LoopJob) Stop()

Stop job

type Version

type Version struct {
	GitBranch string
	GitCommit string
	BuildDate string
	GoVersion string
	Compiler  string
	Platform  string
	Version   string
}

Version is the pkg build version

func GetVersion

func GetVersion() *Version

GetVersion get pkg build version

func (*Version) String

func (v *Version) String() string

Directories

Path Synopsis
Package mock_utils is a generated GoMock package.
Package mock_utils is a generated GoMock package.

Jump to

Keyboard shortcuts

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