utils

package module
v1.17.1 Latest Latest
Warning

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

Go to latest
Published: Jan 19, 2023 License: MIT Imports: 64 Imported by: 22

README

Go-Utils

Many useful golang tools, support >= v1.16.

Commitizen friendly Go Report Card GoDoc Build Status codecov

Install:

go get github.com/Laisky/go-utils

Usage

import (
    gutils "github.com/Laisky/go-utils"
)

Modules

Contains some useful tools in different directories:

  • color.go: colorful code
  • compressor.go: compress and extract dir/files
  • configserver.go: load configs from file or config-server
  • email.go: SMTP email sdk
  • encrypt.go: some tools for encrypt and decrypt, support AES, RSA, ECDSA, MD5, SHA128, SHA256
  • fs.go: some tools to read, move, walk dir/files
  • http.go: some tools to send http request
  • jwt.go: some tools to generate and parse JWT
  • logger.go: enhanched zap logger
  • math.go: some math tools to deal with int, round
  • net.go: some tools to deal with tcp/udp
  • random.go: generate random string, int
  • settings.go: read configs from file or config-server
  • sort.go: easier to sort
  • sync.go: some locks depends on atomic
  • throttle.go: faster rate limiter
  • time.go: faster clock (if you do not enable vdso)
  • utils: some useful tools

Thanks

Thanks to JetBrain support OpenSource License.

https://www.jetbrains.com/community/opensource/#support

Documentation

Overview

Package utils some useful tools fo Golang

Modules

Contains some useful tools in different directories:

  • `color.go`: colorful code
  • `compressor.go`: compress and extract dir/files
  • `configserver.go`: load configs from file or config-server
  • `email.go`: SMTP email sdk
  • `encrypt.go`: some tools for encrypt and decrypt, support AES, RSA, ECDSA, MD5, SHA128, SHA256
  • `fs.go`: some tools to read, move, walk dir/files
  • `http.go`: some tools to send http request
  • `jwt.go`: some tools to generate and parse JWT
  • `logger.go`: enhanched zap logger
  • `math.go`: some math tools to deal with int, round
  • `net.go`: some tools to deal with tcp/udp
  • `random.go`: generate random string, int
  • `settings.go`: read configs from file or config-server
  • `sort.go`: easier to sort
  • `sync.go`: some locks depends on atomic
  • `throttle.go`: faster rate limiter
  • `time.go`: faster clock (if you do not enable vdso)
  • `utils`: some useful tools

Index

Examples

Constants

View Source
const (
	ANSIColorReset int = iota
	ANSIColorBold
	ANSIColorFaint
	ANSIColorItalic
	ANSIColorUnderline
	ANSIColorBlinkSlow
	ANSIColorBlinkRapid
	ANSIColorReverseVideo
	ANSIColorConcealed
	ANSIColorCrossedOut
)

Base attributes

View Source
const (
	ANSIColorFgBlack int = iota + 30
	ANSIColorFgRed
	ANSIColorFgGreen
	ANSIColorFgYellow
	ANSIColorFgBlue
	ANSIColorFgMagenta
	ANSIColorFgCyan
	ANSIColorFgWhite
)

Foreground text colors

View Source
const (
	ANSIColorFgHiBlack int = iota + 90
	ANSIColorFgHiRed
	ANSIColorFgHiGreen
	ANSIColorFgHiYellow
	ANSIColorFgHiBlue
	ANSIColorFgHiMagenta
	ANSIColorFgHiCyan
	ANSIColorFgHiWhite
)

Foreground Hi-Intensity text colors

View Source
const (
	ANSIColorBgBlack int = iota + 40
	ANSIColorBgRed
	ANSIColorBgGreen
	ANSIColorBgYellow
	ANSIColorBgBlue
	ANSIColorBgMagenta
	ANSIColorBgCyan
	ANSIColorBgWhite
)

Background text colors

View Source
const (
	ANSIColorBgHiBlack int = iota + 100
	ANSIColorBgHiRed
	ANSIColorBgHiGreen
	ANSIColorBgHiYellow
	ANSIColorBgHiBlue
	ANSIColorBgHiMagenta
	ANSIColorBgHiCyan
	ANSIColorBgHiWhite
)

Background Hi-Intensity text colors

View Source
const (

	// HTTPHeaderHost HTTP header name
	HTTPHeaderHost = "Host"
	// HTTPHeaderReferer HTTP header name
	HTTPHeaderReferer = "Referer"
	// HTTPHeaderContentType HTTP header name
	HTTPHeaderContentType = "Content-Type"

	// HTTPHeaderContentTypeValJSON HTTP header value
	HTTPHeaderContentTypeValJSON = "application/json"
)
View Source
const (
	// SampleRateDenominator sample rate = sample / SampleRateDenominator
	SampleRateDenominator = 1000

	// LoggerLevelInfo Logger level info
	LoggerLevelInfo string = "info"
	// LoggerLevelDebug Logger level debug
	LoggerLevelDebug string = "debug"
	// LoggerLevelWarn Logger level warn
	LoggerLevelWarn string = "warn"
	// LoggerLevelError Logger level error
	LoggerLevelError string = "error"
	// LoggerLevelFatal Logger level fatal
	LoggerLevelFatal string = "fatal"
	// LoggerLevelPanic Logger level panic
	LoggerLevelPanic string = "panic"
)
View Source
const (
	LoggerEncodingConsole = "console"
	LoggerEncodingJSON    = "json"
)
View Source
const (
	// TimeFormatDate "2006-01-02"
	TimeFormatDate = "2006-01-02"
	// Nano2Sec 1e9
	Nano2Sec = 1e9
	// BitSize64 64
	BitSize64 = 64
	// BaseHex 16
	BaseHex = 16
)
View Source
const ANSIColorEscape = "\x1b"

ANSIColorEscape escape string for ANSI color

Variables

View Source
var (
	// SignMethodHS256 use HS256 for jwt
	SignMethodHS256 = jwt.SigningMethodHS256
	// SignMethodES256 use ES256 for jwt
	SignMethodES256 = jwt.SigningMethodES256
	SignMethodRS256 = jwt.SigningMethodRS256
)
View Source
var (
	// ParseTs2UTC can parse unix timestamp(int64) to time.Time
	ParseTs2UTC = ParseUnix2UTC
	// ParseTs2String can parse unix timestamp(int64) to string
	ParseTs2String = ParseUnix2String
)
View Source
var (
	// ParseTs2Time can parse unix timestamp(int64) to time.Time
	ParseTs2Time = ParseTs2UTC
	// UnixNano2UTC convert unixnano to UTC time
	UnixNano2UTC = ParseUnixNano2UTC
)
View Source
var (
	// Clock high performance time utils, replace Clock1
	Clock = NewClock(context.Background(), defaultClockInterval)

	// Clock2 high performance time utils
	Clock2 = Clock
	// NewClock2 create new Clock
	NewClock2 = NewClock
)
View Source
var (
	// TimeZoneUTC timezone UTC
	TimeZoneUTC = time.UTC
	// TimeZoneShanghai timezone Shanghai
	// TimeZoneShanghai = time.FixedZone("Asia/Shanghai", 8*3600)
	TimeZoneShanghai *time.Location
)
View Source
var (
	// ForceGC force to start gc blocking
	ForceGC = ForceGCBlocking
	// TriggerGC force to start gc unblocking
	TriggerGC = ForceGCUnBlocking
)
View Source
var (
	ErrAsyncTask = errors.New("async task error")
)
View Source
var (

	// JSON effective json
	JSON = json
)
View Source
var Settings = NewSettings()

Settings is the settings for this project

enhance viper.Viper with threadsafe and richer features.

Basic Usage

  import gutils "github.com/Laisky/go-utils"

	 gutils.Settings.

Functions

func AESEncryptFilesInDir deprecated added in v1.13.5

func AESEncryptFilesInDir(dir string, secret []byte, opts ...SettingsEncryptOptf) (err error)

AESEncryptFilesInDir encrypt files in dir

will generate new encrypted files with <append> before ext

xxx.toml -> xxx.enc.toml

Deprecated: use AESEncryptFilesInDir2 instead

func AESEncryptFilesInDir2 added in v1.16.0

func AESEncryptFilesInDir2(dir string, secret []byte, opts ...SettingsEncryptOptf) (err error)

AESEncryptFilesInDir2 encrypt files in dir

will generate new encrypted files with <suffix> after ext

xxx.toml -> xxx.toml.enc

func AbsInt32 added in v1.14.7

func AbsInt32(v int32) int32

AbsInt32 abs(v)

ignore int exceeds limit error, abs(MinInt32) == MaxInt32

func AbsInt64 added in v1.14.7

func AbsInt64(v int64) int64

AbsInt64 abs(v)

ignore int exceeds limit error, abs(MinInt64) == MaxInt64

func AddFileToZip added in v1.11.5

func AddFileToZip(zipWriter *zip.Writer, filename, basedir string) error

AddFileToZip add file tp zip.Writer

https://golangcode.com/create-zip-files-in-go/

func AutoGC added in v1.10.2

func AutoGC(ctx context.Context, opts ...GcOptFunc) (err error)

AutoGC auto trigger GC when memory usage exceeds the custom ration

default to /sys/fs/cgroup/memory/memory.limit_in_bytes

Example
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
if err := AutoGC(
	ctx,
	WithGCMemRatio(85), // default
	WithGCMemLimitFilePath("/sys/fs/cgroup/memory/memory.limit_in_bytes"), // default
); err != nil {
	Logger.Error("enable autogc", zap.Error(err))
}
Output:

func Base64Decode added in v1.13.0

func Base64Decode(encoded string) ([]byte, error)

Base64Decode decode string to bytes use base64

func Base64Encode added in v1.13.0

func Base64Encode(raw []byte) string

Base64Encode encode bytes to string use base64

func Bytes2Str added in v1.14.5

func Bytes2Str(b []byte) string

Bytes2Str unsafe convert bytes to str

func CheckResp

func CheckResp(resp *http.Response) error

CheckResp check HTTP response's status code and return the error with body message

func CloseQuietly added in v1.17.0

func CloseQuietly(v io.Closer)

CloseQuietly closes `io.Closer` quietly, ignore errcheck linter

Example

defer CloseQuietly(fp)

func Color added in v1.12.9

func Color(color int, s string) string

Color wrap with ANSI color

inspired by github.com/fatih/color

func ConvertMap2StringKey added in v1.14.7

func ConvertMap2StringKey(inputMap interface{}) map[string]interface{}

ConvertMap2StringKey convert any map to `map[string]interface{}`

func CopyFile added in v1.13.4

func CopyFile(src, dst string) (err error)

CopyFile copy file content from src to dst

func DecodeECDSAPrivateKey added in v1.11.3

func DecodeECDSAPrivateKey(pemEncoded []byte) (*ecdsa.PrivateKey, error)

DecodeECDSAPrivateKey decode ecdsa private key from pem bytes

func DecodeECDSAPublicKey added in v1.11.3

func DecodeECDSAPublicKey(pemEncodedPub []byte) (*ecdsa.PublicKey, error)

DecodeECDSAPublicKey decode ecdsa public key from pem bytes

func DecodeES256SignByBase64 added in v1.12.3

func DecodeES256SignByBase64(sign string) (a, b *big.Int, err error)

DecodeES256SignByBase64 parse ecdsa signature string to two *big.Int

func DecodeES256SignByHex added in v1.12.3

func DecodeES256SignByHex(sign string) (a, b *big.Int, err error)

DecodeES256SignByHex parse ecdsa signature string to two *big.Int

func DecodeRSAPrivateKey added in v1.13.0

func DecodeRSAPrivateKey(pemEncoded []byte) (*rsa.PrivateKey, error)

DecodeRSAPrivateKey decode rsa private key from pem bytes

func DecodeRSAPublicKey added in v1.13.0

func DecodeRSAPublicKey(pemEncodedPub []byte) (*rsa.PublicKey, error)

DecodeRSAPublicKey decode rsa public key from pem bytes

func DecryptByAes added in v1.12.7

func DecryptByAes(secret []byte, encrypted []byte) ([]byte, error)

DecryptByAes encrypt bytes by aes with key

inspired by https://tutorialedge.net/golang/go-encrypt-decrypt-aes-tutorial/

func Dedent added in v1.16.0

func Dedent(v string, optfs ...DedentOptFunc) string

Dedent removes leading whitespace or tab from the beginning of each line

will replace all tab to 4 blanks.

func DeepClone added in v1.16.0

func DeepClone(src interface{}) (dst interface{})

DeepClone deep clone a struct

will ignore all unexported fields

func DirSize added in v1.10.1

func DirSize(path string) (size int64, err error)

DirSize calculate directory size. https://stackoverflow.com/a/32482941/2368737

Example
dirPath := "."
size, err := DirSize(dirPath)
if err != nil {
	Logger.Error("get dir size", zap.Error(err), zap.String("path", dirPath))
}
Logger.Info("got size", zap.Int64("size", size), zap.String("path", dirPath))
Output:

func EncodeECDSAPrivateKey added in v1.11.3

func EncodeECDSAPrivateKey(privateKey *ecdsa.PrivateKey) ([]byte, error)

EncodeECDSAPrivateKey encode ecdsa private key to pem bytes

func EncodeECDSAPublicKey added in v1.11.3

func EncodeECDSAPublicKey(publicKey *ecdsa.PublicKey) ([]byte, error)

EncodeECDSAPublicKey encode ecdsa public key to pem bytes

func EncodeES256SignByBase64 added in v1.12.3

func EncodeES256SignByBase64(a, b *big.Int) string

EncodeES256SignByBase64 format ecdsa signature to stirng

func EncodeES256SignByHex added in v1.12.3

func EncodeES256SignByHex(a, b *big.Int) string

EncodeES256SignByHex format ecdsa sign to stirng

func EncodeRSAPrivateKey added in v1.13.0

func EncodeRSAPrivateKey(privateKey *rsa.PrivateKey) ([]byte, error)

EncodeRSAPrivateKey encode rsa private key to pem bytes

func EncodeRSAPublicKey added in v1.13.0

func EncodeRSAPublicKey(publicKey *rsa.PublicKey) ([]byte, error)

EncodeRSAPublicKey encode rsa public key to pem bytes

func EncryptByAes added in v1.13.4

func EncryptByAes(secret []byte, cnt []byte) ([]byte, error)

EncryptByAes encrypt bytes by aes with key

inspired by https://tutorialedge.net/golang/go-encrypt-decrypt-aes-tutorial/

func FallBack

func FallBack(orig func() interface{}, fallback interface{}) (ret interface{})

FallBack return the fallback when orig got error utils.FallBack(func() interface{} { return getIOStatMetric(fs) }, &IOStat{}).(*IOStat)

Example
targetFunc := func() interface{} {
	panic("someting wrong")
}

FallBack(targetFunc, 10) // got 10
Output:

func FlattenMap

func FlattenMap(data map[string]interface{}, delimiter string)

FlattenMap make embedded map into flatten map

Example
data := map[string]interface{}{
	"a": "1",
	"b": map[string]interface{}{
		"c": 2,
		"d": map[string]interface{}{
			"e": 3,
		},
	},
}
FlattenMap(data, "__")
fmt.Println(data)
Output:

map[a:1 b__c:2 b__d__e:3]

func ForceGCBlocking added in v1.7.4

func ForceGCBlocking()

ForceGCBlocking force to run blocking manual gc.

Example
ForceGCBlocking()
Output:

func ForceGCUnBlocking added in v1.7.4

func ForceGCUnBlocking()

ForceGCUnBlocking trigger GC unblocking

Example
ForceGCUnBlocking()
Output:

func FormatBig2Base64 added in v1.12.6

func FormatBig2Base64(b *big.Int) string

FormatBig2Base64 format big to base64 string

func FormatBig2Hex added in v1.11.4

func FormatBig2Hex(b *big.Int) string

FormatBig2Hex format big to hex string

func GeneratePasswordHash added in v1.2.0

func GeneratePasswordHash(password []byte) ([]byte, error)

GeneratePasswordHash generate hashed password by origin password

Example
// generate hashed password
rawPassword := []byte("1234567890")
hashedPassword, err := GeneratePasswordHash(rawPassword)
if err != nil {
	Logger.Error("try to generate password got error", zap.Error(err))
	return
}
fmt.Printf("got new hashed pasword: %v\n", string(hashedPassword))

// validate passowrd
if !ValidatePasswordHash(hashedPassword, rawPassword) {
	Logger.Error("password invalidate", zap.Error(err))
	return
}
Output:

func GetFuncName

func GetFuncName(f interface{}) string

GetFuncName return the name of func

Example
GetFuncName(testFoo) // "github.com/Laisky/go-utils.testFoo"
Output:

func GetStructFieldByName added in v1.14.3

func GetStructFieldByName(st interface{}, fieldName string) interface{}

GetStructFieldByName get struct field by name

func GracefulCancel added in v1.17.1

func GracefulCancel(cancel func())

GracefulCancel is a function that will be called when the process is about to be terminated.

func HTTPInvalidStatusError

func HTTPInvalidStatusError(statusCode int) error

HTTPInvalidStatusError return error about status code

func HasField added in v1.17.0

func HasField(st interface{}, fieldName string) bool

HasField check is struct has field

inspired by https://mrwaggel.be/post/golang-reflect-if-initialized-struct-has-member-method-or-fields/

func HasMethod added in v1.17.0

func HasMethod(st interface{}, methodName string) bool

HasMethod check is struct has method

inspired by https://mrwaggel.be/post/golang-reflect-if-initialized-struct-has-member-method-or-fields/

func HashSHA128String added in v1.10.2

func HashSHA128String(val string) string

HashSHA128String calculate string's hash by sha256

Example
val := testhashraw
got := HashSHA128String(val)
Logger.Info("hash", zap.String("got", got))
Output:

func HashSHA256String added in v1.10.1

func HashSHA256String(val string) string

HashSHA256String calculate string's hash by sha256

Example
val := testhashraw
got := HashSHA256String(val)
Logger.Info("hash", zap.String("got", got))
Output:

func HashXxhashString added in v1.10.1

func HashXxhashString(val string) string

HashXxhashString calculate string's hash by sha256

Example
val := testhashraw
got := HashXxhashString(val)
Logger.Info("hash", zap.String("got", got))
Output:

func HumanReadableByteCount added in v1.14.7

func HumanReadableByteCount(bytes int64, si bool) (ret string)

HumanReadableByteCount convert bytes to human readable string

Args:

  • bytes:
  • si: `si ? 1024 : 1000`

Example:

`HumanReadableByteCount(1005, false) -> "1.01KB"`

func InArray added in v1.13.10

func InArray(collection interface{}, ele interface{}) bool

InArray if collection contains ele

func IsDir added in v1.13.4

func IsDir(path string) (bool, error)

IsDir is path exists as dir

func IsDirWritable added in v1.13.9

func IsDirWritable(dir string) (err error)

IsDirWritable if dir is writable

func IsFile added in v1.13.4

func IsFile(path string) (bool, error)

IsFile is path exists as file

func IsHasField deprecated added in v1.12.2

func IsHasField(st interface{}, fieldName string) bool

IsHasField check is struct has field

inspired by https://mrwaggel.be/post/golang-reflect-if-initialized-struct-has-member-method-or-fields/

Deprecated: use HasField instead

func IsHasMethod deprecated added in v1.12.2

func IsHasMethod(st interface{}, methodName string) bool

IsHasMethod check is struct has method

inspired by https://mrwaggel.be/post/golang-reflect-if-initialized-struct-has-member-method-or-fields/

Deprecated: use HasMethod instead

func IsPanic added in v1.14.7

func IsPanic(f func()) (isPanic bool)

IsPanic is `f()` throw panic

func IsPtr added in v1.12.0

func IsPtr(t interface{}) bool

IsPtr check if t is pointer

func IsRemoteUDPPortOpen added in v1.14.3

func IsRemoteUDPPortOpen(addr string) error

IsRemoteUDPPortOpen check is remote udp port open. return err if remote port is not open.

Args:

addr: "scanme.nmap.org:53"

func JumpHash added in v1.15.0

func JumpHash(key uint64, numBuckets int) (int32, error)

JumpHash fatest consistent hashing created by google. inspired by https://medium.com/@dgryski/consistent-hashing-algorithmic-tradeoffs-ef6b8e2fcae8

func ListFilesInDir added in v1.13.5

func ListFilesInDir(dir string) (files []string, err error)

ListFilesInDir list files in dir

func MD5JSON added in v1.16.0

func MD5JSON(data interface{}) (string, error)

MD5JSON calculate md5(jsonify(data))

func MaxInt added in v1.11.1

func MaxInt(a, b int) int

MaxInt get the max int of two

func MinInt added in v1.11.1

func MinInt(a, b int) int

MinInt get the min int of two

func MoveFile added in v1.13.4

func MoveFile(src, dst string) (err error)

MoveFile move file from src to dst by copy

sometimes move file by `rename` not work. for example, you can not move file between docker volumes by `rename`.

func NewHTTPClient added in v1.12.8

func NewHTTPClient(opts ...HTTPClientOptFunc) (c *http.Client, err error)

NewHTTPClient create http client

func NewRand added in v1.15.0

func NewRand() *rand.Rand

NewRand new individual random to aviod global mutex

func NewSkiplist added in v1.15.0

func NewSkiplist() *skiplist.SkipList

NewSkiplist new skiplist

https://github.com/sean-public/fast-skiplist

func NewTmpFileForContent added in v1.17.1

func NewTmpFileForContent(content []byte) (path string, err error)

NewTmpFileForContent write content to tmp file and return path

func NilInterface added in v1.16.0

func NilInterface(data interface{}) bool

NilInterface make sure data is nil interface or another type with nil value

Example:

type foo struct{}
var f *foo
var v interface{}
v = f
v == nil // false
NilInterface(v) // true

func PanicIfErr added in v1.16.0

func PanicIfErr(err error)

PanicIfErr panic if err is not nil

func ParseBase642Big added in v1.12.6

func ParseBase642Big(raw string) (*big.Int, error)

ParseBase642Big parse base64 string to big

func ParseHex2Big added in v1.11.4

func ParseHex2Big(hex string) (b *big.Int, ok bool)

ParseHex2Big parse hex string to big

func ParseHex2UTC added in v1.8.0

func ParseHex2UTC(ts string) (t time.Time, err error)

ParseHex2UTC parse hex to UTC time

func ParseHexNano2UTC added in v1.8.0

func ParseHexNano2UTC(ts string) (t time.Time, err error)

ParseHexNano2UTC parse hex contains nano to UTC time

func ParseJWTTokenWithoutValidate added in v1.10.0

func ParseJWTTokenWithoutValidate(token string, payload jwt.Claims) (err error)

ParseJWTTokenWithoutValidate parse and get payload without validate jwt token

func ParseLoggerLevel added in v1.15.0

func ParseLoggerLevel(level string) (zapcore.Level, error)

ParseLoggerLevel

func ParseUnix2String added in v1.8.0

func ParseUnix2String(ts int64, layout string) string

ParseUnix2String can parse unix timestamp(int64) to string

func ParseUnix2UTC added in v1.8.0

func ParseUnix2UTC(ts int64) time.Time

ParseUnix2UTC convert unix to UTC time

func ParseUnixNano2UTC added in v1.8.0

func ParseUnixNano2UTC(ts int64) time.Time

ParseUnixNano2UTC convert unixnano to UTC time

func Race added in v1.17.0

func Race(gs ...func())

Race return when any goroutine returned

Example
startAt := time.Now()
Race(
	func() { time.Sleep(time.Millisecond) },
	func() { time.Sleep(time.Second) },
	func() { time.Sleep(time.Minute) },
)

fmt.Println(time.Since(startAt) < time.Second)
Output:

true

func RaceWithCtx added in v1.17.0

func RaceWithCtx(ctx context.Context, gs ...func())

RaceWithCtx return when any goroutine returned or ctx canceled

func RandomStringWithLength

func RandomStringWithLength(n int) string

RandomStringWithLength generate random string with specific length

func RegexNamedSubMatch

func RegexNamedSubMatch(r *regexp.Regexp, str string, subMatchMap map[string]string) error

RegexNamedSubMatch extract key:val map from string by group match

Example
reg := regexp.MustCompile(`(?P<key>\d+.*)`)
str := "12345abcde"
groups := map[string]string{}
if err := RegexNamedSubMatch(reg, str, groups); err != nil {
	Logger.Error("try to group match got error", zap.Error(err))
}

fmt.Println(groups)
Output:

map[key:12345abcde]

func RemoveEmpty added in v1.13.10

func RemoveEmpty(vs []string) (r []string)

RemoveEmpty remove duplicate string in slice

func RequestJSON

func RequestJSON(method, url string, request *RequestData, resp interface{}) (err error)

RequestJSON request JSON and return JSON by default client

func RequestJSONWithClient

func RequestJSONWithClient(httpClient *http.Client,
	method,
	url string,
	request *RequestData,
	resp interface{},
) (err error)

RequestJSONWithClient request JSON and return JSON with specific client

func Round

func Round(val float64, d int) float64

Round round float64

Round(1.005, 2) -> 1.01

func RunCMD added in v1.12.5

func RunCMD(ctx context.Context, app string, args ...string) (stdout []byte, err error)

RunCMD run command script

func RunWithTimeout added in v1.17.0

func RunWithTimeout(timeout time.Duration, f func())

RunWithTimeout run func with timeout

Example
slow := func() { time.Sleep(10 * time.Second) }
startAt := time.Now()
RunWithTimeout(5*time.Millisecond, slow)

fmt.Println(time.Since(startAt) < 10*time.Second)
Output:

true

func SecRandInt added in v1.14.3

func SecRandInt(n int) (int, error)

SecRandInt generate security int

func SecRandomStringWithLength added in v1.14.3

func SecRandomStringWithLength(n int) (string, error)

SecRandomStringWithLength generate random string with specific length

func SetInternalClock added in v1.15.0

func SetInternalClock(interval time.Duration)

SetInternalClock set internal Clock with refresh interval

func SetStructFieldsBySlice added in v1.11.1

func SetStructFieldsBySlice(structs, vals interface{}) (err error)

SetStructFieldsBySlice set field value of structs slice by values slice

Example
type ST struct{ A, B string }
var (
	err error
	ss  = []*ST{{}, {}}
	vs  = [][]string{
		{"x0", "y0"},
		{"x1", "y1"},
	}
)
if err = SetStructFieldsBySlice(ss, vs); err != nil {
	Logger.Error("set struct val", zap.Error(err))
	return
}

fmt.Printf("%+v\n", ss)
// ss = []*ST{{A: "x0", B: "y0"}, {A: "x1", B: "y1"}}
Output:

func SignByECDSAWithSHA256 added in v1.11.3

func SignByECDSAWithSHA256(priKey *ecdsa.PrivateKey, content []byte) (r, s *big.Int, err error)

SignByECDSAWithSHA256 generate signature by ecdsa private key use sha256

Example
priKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
	Logger.Panic("generate key", zap.Error(err))
}
priKey2, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
	Logger.Panic("generate key", zap.Error(err))
}

// case: correct key
cnt := []byte("fjijf23lijfl23ijrl32jra9pfie9wpfi")
r, s, err := SignByECDSAWithSHA256(priKey, cnt)
if err != nil {
	Logger.Panic("sign", zap.Error(err))
}
if !VerifyByECDSAWithSHA256(&priKey.PublicKey, cnt, r, s) {
	Logger.Panic("verify failed")
}

// generate string
encoded := EncodeES256SignByBase64(r, s)
if _, _, err = DecodeES256SignByBase64(encoded); err != nil {
	Logger.Panic("encode and decode", zap.Error(err))
}

// case: incorrect cnt
cnt = []byte("fjijf23lijfl23ijrl32jra9pfie9wpfi")
r, s, err = SignByECDSAWithSHA256(priKey, cnt)
if err != nil {
	Logger.Panic("sign", zap.Error(err))
}
if VerifyByECDSAWithSHA256(&priKey.PublicKey, append(cnt, '2'), r, s) {
	Logger.Panic("should not verify")
}

// case: incorrect key
r, s, err = SignByECDSAWithSHA256(priKey2, cnt)
if err != nil {
	Logger.Panic("sign", zap.Error(err))
}
if VerifyByECDSAWithSHA256(&priKey.PublicKey, cnt, r, s) {
	Logger.Panic("should not verify")
}
Output:

func SignByRSAWithSHA256 added in v1.13.0

func SignByRSAWithSHA256(priKey *rsa.PrivateKey, content []byte) ([]byte, error)

SignByRSAWithSHA256 generate signature by rsa private key use sha256

func SignReaderByECDSAWithSHA256 added in v1.11.4

func SignReaderByECDSAWithSHA256(priKey *ecdsa.PrivateKey, reader io.Reader) (r, s *big.Int, err error)

SignReaderByECDSAWithSHA256 generate signature by ecdsa private key use sha256

func SignReaderByRSAWithSHA256 added in v1.13.0

func SignReaderByRSAWithSHA256(priKey *rsa.PrivateKey, reader io.Reader) (sig []byte, err error)

SignReaderByRSAWithSHA256 generate signature by rsa private key use sha256

func SleepWithContext added in v1.17.0

func SleepWithContext(ctx context.Context, duration time.Duration)

SleepWithContext sleep duration with context, if context is done, return

func StopSignal added in v1.14.8

func StopSignal(optfs ...StopSignalOptFunc) (stopCh <-chan struct{})

StopSignal registered for SIGTERM and SIGINT. A stop channel is returned which is closed on one of these signals. If a second signal is caught, the program is terminated with exit code 1.

Copied from https://github.com/kubernetes/sample-controller

func Str2Bytes added in v1.14.5

func Str2Bytes(s string) []byte

Str2Bytes unsafe convert str to bytes

func TemplateWithMap added in v1.5.4

func TemplateWithMap(tpl string, data map[string]interface{}) string

TemplateWithMap replace `${var}` in template string

func TemplateWithMapAndRegexp added in v1.5.4

func TemplateWithMapAndRegexp(tplReg *regexp.Regexp, tpl string, data map[string]interface{}) string

TemplateWithMapAndRegexp replace `${var}` in template string

func TrimEleSpaceAndRemoveEmpty added in v1.13.10

func TrimEleSpaceAndRemoveEmpty(vs []string) (r []string)

TrimEleSpaceAndRemoveEmpty remove duplicate string in slice

func URLMasking added in v1.9.3

func URLMasking(url, mask string) string

URLMasking masking password in url

Example
originURL := "http://12ijij:3j23irj@jfjlwef.ffe.com"
newURL := URLMasking(originURL, "*****")
fmt.Println(newURL)
Output:

http://12ijij:*****@jfjlwef.ffe.com

func UTCNow

func UTCNow() time.Time

UTCNow get current time in utc

func UniqueStrings added in v1.11.2

func UniqueStrings(vs []string) (r []string)

UniqueStrings remove duplicate string in slice

func Unzip added in v1.11.5

func Unzip(src string, dest string) (filenames []string, err error)

Unzip will decompress a zip archive, moving all files and folders within the zip file (parameter 1) to an output directory (parameter 2).

https://golangcode.com/unzip-files-in-go/

func ValidateFileHash added in v1.12.1

func ValidateFileHash(filepath string, hashed string) error

ValidateFileHash validate file content with hashed string

Args:

  • filepath: file path to check
  • hashed: hashed string, like `sha256: xxxx`

func ValidatePasswordHash added in v1.2.0

func ValidatePasswordHash(hashedPassword, password []byte) bool

ValidatePasswordHash validate password is match with hashedPassword

func VerifyByECDSAWithSHA256 added in v1.11.3

func VerifyByECDSAWithSHA256(pubKey *ecdsa.PublicKey, content []byte, r, s *big.Int) bool

VerifyByECDSAWithSHA256 verify signature by ecdsa public key use sha256

func VerifyByRSAWithSHA256 added in v1.13.0

func VerifyByRSAWithSHA256(pubKey *rsa.PublicKey, content []byte, sig []byte) error

VerifyByRSAWithSHA256 verify signature by rsa public key use sha256

func VerifyReaderByECDSAWithSHA256 added in v1.11.4

func VerifyReaderByECDSAWithSHA256(pubKey *ecdsa.PublicKey, reader io.Reader, r, s *big.Int) (bool, error)

VerifyReaderByECDSAWithSHA256 verify signature by ecdsa public key use sha256

func VerifyReaderByRSAWithSHA256 added in v1.13.0

func VerifyReaderByRSAWithSHA256(pubKey *rsa.PublicKey, reader io.Reader, sig []byte) error

VerifyReaderByRSAWithSHA256 verify signature by rsa public key use sha256

func ZipFiles added in v1.11.5

func ZipFiles(output string, files []string) (err error)

ZipFiles compresses one or many files into a single zip archive file.

Args:

  • output: is the output zip file's name.
  • files: is a list of files to add to the zip. files can be directory.

https://golangcode.com/create-zip-files-in-go/

Types

type AesReaderWrapper added in v1.13.4

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

AesReaderWrapper used to decrypt encrypted reader

func NewAesReaderWrapper added in v1.13.4

func NewAesReaderWrapper(in io.Reader, key []byte) (*AesReaderWrapper, error)

NewAesReaderWrapper wrap reader by aes

func (*AesReaderWrapper) Read added in v1.13.4

func (w *AesReaderWrapper) Read(p []byte) (n int, err error)

type AlertHookOptFunc added in v1.10.0

type AlertHookOptFunc func(*alertHookOption)

AlertHookOptFunc option for create AlertHook

func WithAlertHookLevel added in v1.9.0

func WithAlertHookLevel(level zapcore.Level) AlertHookOptFunc

WithAlertHookLevel level to trigger AlertHook

func WithAlertPushTimeout added in v1.9.0

func WithAlertPushTimeout(timeout time.Duration) AlertHookOptFunc

WithAlertPushTimeout set AlertPusher HTTP timeout

type AlertPusher added in v1.9.0

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

AlertPusher send alert to laisky's alert API

https://github.com/Laisky/laisky-blog-graphql/tree/master/telegram

Example
pusher, err := NewAlertPusherWithAlertType(
	context.Background(),
	"https://blog.laisky.com/graphql/query/",
	"hello",
	"rwkpVuAgaBZQBASKndHK",
)
if err != nil {
	Logger.Panic("create alert pusher", zap.Error(err))
}
defer pusher.Close()
logger := Logger.WithOptions(
	zap.Fields(zap.String("logger", "test")),
	zap.HooksWithFields(pusher.GetZapHook()),
)

logger.Debug("DEBUG", zap.String("yo", "hello"))
logger.Info("Info", zap.String("yo", "hello"))
logger.Warn("Warn", zap.String("yo", "hello"))
logger.Error("Error", zap.String("yo", "hello"))

time.Sleep(1 * time.Second)
Output:

func NewAlertPusher added in v1.9.0

func NewAlertPusher(ctx context.Context, pushAPI string, opts ...AlertHookOptFunc) (a *AlertPusher, err error)

NewAlertPusher create new AlertPusher

func NewAlertPusherWithAlertType added in v1.9.0

func NewAlertPusherWithAlertType(ctx context.Context,
	pushAPI string,
	alertType,
	pushToken string,
	opts ...AlertHookOptFunc,
) (a *AlertPusher, err error)

NewAlertPusherWithAlertType create new AlertPusher with default type and token

func (*AlertPusher) Close added in v1.9.0

func (a *AlertPusher) Close()

Close close AlertPusher

func (*AlertPusher) GetZapHook added in v1.10.1

func (a *AlertPusher) GetZapHook() func(zapcore.Entry, []zapcore.Field) (err error)

GetZapHook get hook for zap logger

func (*AlertPusher) Send added in v1.9.0

func (a *AlertPusher) Send(msg string) (err error)

Send send with default alertType and pushToken

func (*AlertPusher) SendWithType added in v1.9.0

func (a *AlertPusher) SendWithType(alertType, pushToken, msg string) (err error)

SendWithType send alert with specific type, token and msg

type AsyncTask added in v1.17.0

type AsyncTask interface {
	// ID get task id
	ID() string
	// Status get task status, pending/done/failed
	Status() AsyncTaskStatus
	// SetDone set task done with result data
	SetDone(ctx context.Context, data string) (err error)
	// SetError set task error with err message
	SetError(ctx context.Context, errMsg string) (err error)
}

asyncTask async task

func NewAsyncTask added in v1.17.0

func NewAsyncTask(ctx context.Context, store AsyncTaskStore) (AsyncTask, error)

NewTask new async task

type AsyncTaskResult added in v1.17.0

type AsyncTaskResult struct {
	TaskID string          `json:"task_id"`
	Status AsyncTaskStatus `json:"status"`
	Data   string          `json:"data"`
	Err    string          `json:"err"`
}

AsyncTaskResult result of async task

type AsyncTaskStatus added in v1.17.0

type AsyncTaskStatus string
const (
	// AsyncTaskStatusPending task pending
	AsyncTaskStatusPending AsyncTaskStatus = "pending"
	// AsyncTaskStatusDone task done
	AsyncTaskStatusDone AsyncTaskStatus = "done"
	// AsyncTaskStatusFailed task failed
	AsyncTaskStatusFailed AsyncTaskStatus = "failed"
)

func (AsyncTaskStatus) String added in v1.17.1

func (s AsyncTaskStatus) String() string

type AsyncTaskStore added in v1.17.0

type AsyncTaskStore interface {
	// New create new AsyncTaskResult with id
	New(ctx context.Context) (result *AsyncTaskResult, err error)
	// Set set AsyncTaskResult
	Set(ctx context.Context, taskID string, result *AsyncTaskResult) (err error)
	// Heartbeat refresh async task's updated time to mark this task is still alive
	Heartbeat(ctx context.Context, taskID string) (alived bool, err error)
}

AsyncTaskStore persistency storage for async task

type AtomicFieldBool added in v1.16.0

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

AtomicFieldBool is a bool field which is goroutine-safe

Example
type foo struct {
	v AtomicFieldBool
}

f := new(foo)
f.v.SetTrue()
fmt.Println(f.v.True())
Output:

true

func (*AtomicFieldBool) SetFalse added in v1.16.0

func (a *AtomicFieldBool) SetFalse()

SetFalse set false

func (*AtomicFieldBool) SetTrue added in v1.16.0

func (a *AtomicFieldBool) SetTrue()

SetTrue set true

func (*AtomicFieldBool) True added in v1.16.0

func (a *AtomicFieldBool) True() bool

True value == true

type ChildParallelCounter added in v1.6.3

type ChildParallelCounter struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

ChildParallelCounter child of ParallelCounter

func (*ChildParallelCounter) Count added in v1.6.3

func (c *ChildParallelCounter) Count() (r int64)

Count count 1

func (*ChildParallelCounter) CountN added in v1.6.3

func (c *ChildParallelCounter) CountN(n int64) (r int64)

CountN count n

func (*ChildParallelCounter) Get added in v1.6.3

func (c *ChildParallelCounter) Get() int64

Get get current count

type Clock2Type added in v1.7.3

type Clock2Type ClockType

Clock2Type high performance clock with lazy refreshing

type ClockItf added in v1.7.3

type ClockItf interface {
	GetTimeInRFC3339Nano() string
	GetUTCNow() time.Time
	SetupInterval(time.Duration)
	Interval() time.Duration
	Close()
}

ClockItf high performance lazy clock

type ClockType

type ClockType struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

ClockType high performance clock with lazy refreshing

func NewClock

func NewClock(ctx context.Context, refreshInterval time.Duration) *ClockType

NewClock create new Clock

func (*ClockType) Close added in v1.7.3

func (c *ClockType) Close()

Close stop Clock update

func (*ClockType) GetDate added in v1.13.8

func (c *ClockType) GetDate() (time.Time, error)

GetDate return "yyyy-mm-dd"

func (*ClockType) GetNanoTimeInHex added in v1.8.0

func (c *ClockType) GetNanoTimeInHex() string

GetNanoTimeInHex return current time with nano in hex

func (*ClockType) GetTimeInHex added in v1.8.0

func (c *ClockType) GetTimeInHex() string

GetTimeInHex return current time in hex

func (*ClockType) GetTimeInRFC3339Nano

func (c *ClockType) GetTimeInRFC3339Nano() string

GetTimeInRFC3339Nano return Clock current time in string

func (*ClockType) GetUTCNow

func (c *ClockType) GetUTCNow() time.Time

GetUTCNow return Clock current time.Time

func (*ClockType) Interval added in v1.15.0

func (c *ClockType) Interval() time.Duration

Interval get current interval

func (*ClockType) SetInterval added in v1.14.3

func (c *ClockType) SetInterval(interval time.Duration)

SetInterval setup update interval

type CompressOptFunc added in v1.10.0

type CompressOptFunc func(*compressOption) error

CompressOptFunc options for compressor

func WithCompressBufSizeByte added in v1.10.0

func WithCompressBufSizeByte(n int) CompressOptFunc

WithCompressBufSizeByte set compressor buf size

func WithCompressLevel added in v1.10.0

func WithCompressLevel(n int) CompressOptFunc

WithCompressLevel set compressor compress level

func WithPGzipBlockSize added in v1.10.0

func WithPGzipBlockSize(bytes int) CompressOptFunc

WithPGzipBlockSize set compressor blocks

func WithPGzipNBlocks added in v1.10.0

func WithPGzipNBlocks(nBlock int) CompressOptFunc

WithPGzipNBlocks set compressor blocks

type CompressorItf added in v1.10.0

type CompressorItf interface {
	Write([]byte) (int, error)
	WriteString(string) (int, error)
	// write footer and flust to lower writer
	Flush() error
	// write footer without flush
	WriteFooter() error
}

CompressorItf interface of compressor

type Config

type Config struct {
	Name     string          `json:"name"`
	Profiles []string        `json:"profiles"`
	Label    string          `json:"label"`
	Version  string          `json:"version"`
	Sources  []*ConfigSource `json:"propertySources"`
}

Config whole configuation return by config-server

type ConfigSource

type ConfigSource struct {
	Name   string                 `json:"name"`
	Source map[string]interface{} `json:"source"`
}

ConfigSource config item in config-server

type ConfigSrv

type ConfigSrv struct {
	RemoteCfg *Config
	// contains filtered or unexported fields
}

ConfigSrv can load configuration from Spring-Cloud-Config-Server

Example
var (
	url     = "http://config-server.un.org"
	app     = "appname"
	profile = "sit"
	label   = "master"
)

c := NewConfigSrv(url, app, profile, label)
c.Get("management.context-path")
c.GetString("management.context-path")
c.GetBool("endpoints.health.sensitive")
c.GetInt("spring.cloud.config.retry")
Output:

func NewConfigSrv

func NewConfigSrv(url, app, profile, label string) *ConfigSrv

NewConfigSrv create ConfigSrv

func (*ConfigSrv) Fetch

func (c *ConfigSrv) Fetch() error

Fetch load data from config-server

func (*ConfigSrv) Get

func (c *ConfigSrv) Get(name string) (interface{}, bool)

Get get `interface{}` from the localcache of config-server

func (*ConfigSrv) GetBool

func (c *ConfigSrv) GetBool(name string) (val bool, ok bool)

GetBool get `bool` from the localcache of config-server

func (*ConfigSrv) GetInt

func (c *ConfigSrv) GetInt(name string) (val int, ok bool)

GetInt get `int` from the localcache of config-server

func (*ConfigSrv) GetString

func (c *ConfigSrv) GetString(name string) (string, bool)

GetString get `string` from the localcache of config-server

func (*ConfigSrv) Map

func (c *ConfigSrv) Map(set func(string, interface{}))

Map interate `set(k, v)`

type Counter

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

Counter int64 counter

Example
counter := NewCounter()
counter.Count()
counter.CountN(10)
counter.Get() // get current count
Output:

func NewCounter

func NewCounter() *Counter

NewCounter create Counter from 0

func NewCounterFromN

func NewCounterFromN(n int64) *Counter

NewCounterFromN create Counter from custom number

func (*Counter) Count

func (c *Counter) Count() int64

Count increse and return the result

func (*Counter) CountN

func (c *Counter) CountN(n int64) int64

CountN increse N and return the result

func (*Counter) Get

func (c *Counter) Get() int64

Get return current counter's number

func (*Counter) GetSpeed

func (c *Counter) GetSpeed() (r float64)

GetSpeed return increasing speed from lastest invoke `GetSpeed`

func (*Counter) Set

func (c *Counter) Set(n int64)

Set overwrite the counter's number

type CtxKeyT added in v1.7.0

type CtxKeyT struct{}

CtxKeyT type of context key

type DedentOptFunc added in v1.16.0

type DedentOptFunc func(opt *dedentOpt)

DedentOptFunc dedent option

func WithReplaceTabBySpaces added in v1.16.0

func WithReplaceTabBySpaces(spaces int) DedentOptFunc

WithReplaceTabBySpaces replace tab to spaces

type Deque added in v1.17.0

type Deque interface {
	PushBack(interface{})
	PushFront(interface{})
	PopFront() interface{}
	PopBack() interface{}
	Len() int
	Front() interface{}
	Back() interface{}
}

Deque

https://pkg.go.dev/github.com/gammazero/deque#Deque

func NewDeque added in v1.17.0

func NewDeque(optfs ...DequeOptFunc) (Deque, error)

NewDeque new deque

type DequeOptFunc added in v1.17.0

type DequeOptFunc func(*dequeOpt) error

DequeOptFunc optional arguments for deque

func WithDequeCurrentCapacity added in v1.17.0

func WithDequeCurrentCapacity(size int) DequeOptFunc

WithDequeCurrentCapacity preallocate memory for deque

func WithDequeMinimalCapacity added in v1.17.0

func WithDequeMinimalCapacity(size int) DequeOptFunc

WithDequeMinimalCapacity set deque minimal capacity

type EmailDialer added in v1.17.0

type EmailDialer interface {
	DialAndSend(m ...*gomail.Message) error
}

EmailDialer create gomail.Dialer

type ExpCache added in v1.13.11

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

ExpCache cache with expires

can Store/Load like map

Example
cc := NewExpCache(context.Background(), 100*time.Millisecond)
cc.Store("key", "val")
cc.Load("key") // return "val"

// data expired
time.Sleep(200 * time.Millisecond)
data, ok := cc.Load("key")
fmt.Println(data)
fmt.Println(ok)
Output:

<nil>
false

func NewExpCache added in v1.13.11

func NewExpCache(ctx context.Context, ttl time.Duration) *ExpCache

NewExpCache new cache manager

func (*ExpCache) Delete added in v1.15.0

func (c *ExpCache) Delete(key interface{})

Delete remove key

func (*ExpCache) Load added in v1.13.11

func (c *ExpCache) Load(key interface{}) (data interface{}, ok bool)

Load load val from cache

func (*ExpCache) Store added in v1.13.11

func (c *ExpCache) Store(key, val interface{})

Store store new key and val into cache

type ExpiredRLock added in v1.14.1

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

ExpiredRLock Lock with expire time

func NewExpiredRLock added in v1.14.1

func NewExpiredRLock(ctx context.Context, exp time.Duration) (el *ExpiredRLock, err error)

NewExpiredRLock new ExpiredRLock

func (*ExpiredRLock) GetLock added in v1.14.1

func (e *ExpiredRLock) GetLock(key string) *sync.RWMutex

GetLock get lock

type FIFO added in v1.15.0

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

FIFO is a lock-free First-In-First-Out queue

paper: https://1drv.ms/b/s!Au45o0W1gVVLuNxYkPzfBo4fOssFPQ?e=TYxHKl

Example
f := NewFIFO()
f.Put(1)
v := f.Get()
if v == nil {
	panic(v)
}

fmt.Println(v.(int))
Output:

1

func NewFIFO added in v1.15.0

func NewFIFO() *FIFO

NewFIFO create a new FIFO queue

func (*FIFO) Get added in v1.15.0

func (f *FIFO) Get() interface{}

Get pop data from the head of queue

func (*FIFO) Len added in v1.15.0

func (f *FIFO) Len() int

Len return the length of queue

func (*FIFO) Put added in v1.15.0

func (f *FIFO) Put(d interface{})

Put put an data into queue's tail

type FLock added in v1.17.0

type FLock interface {
	Lock() error
	Unlock() error
}

FLock lock by file

func NewFlock added in v1.17.0

func NewFlock(lockFilePath string) FLock

NewFlock new file lock

type GZCompressor added in v1.6.0

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

GZCompressor compress by gz with buf

Example
originText := testCompressraw
writer := &bytes.Buffer{}

var err error
// writer
c, err := NewGZCompressor(
	writer,
	WithCompressLevel(defaultGzCompressLevel),           // default
	WithCompressBufSizeByte(defaultCompressBufSizeByte), // default
)
if err != nil {
	Logger.Error("new compressor", zap.Error(err))
	return
}
if _, err = c.WriteString(originText); err != nil {
	Logger.Error("write string to compressor", zap.Error(err))
	return
}
if err = c.Flush(); err != nil {
	Logger.Error("flush compressor", zap.Error(err))
	return
}

// reader
var gz *gzip.Reader
if gz, err = gzip.NewReader(writer); err != nil {
	Logger.Error("new compressor", zap.Error(err))
	return
}

var bs []byte
if bs, err = ioutil.ReadAll(gz); err != nil {
	Logger.Error("read from compressor", zap.Error(err))
	return
}

got := string(bs)
if got != originText {
	Logger.Error("extract compressed text invalidate",
		zap.String("got", got),
		zap.ByteString("expect", bs))
	return
}
Output:

func NewGZCompressor added in v1.6.0

func NewGZCompressor(writer io.Writer, opts ...CompressOptFunc) (c *GZCompressor, err error)

NewGZCompressor create new GZCompressor

func (*GZCompressor) Flush added in v1.6.0

func (c *GZCompressor) Flush() (err error)

Flush flush buffer bytes into bottom writer with gz meta footer

func (*GZCompressor) Write added in v1.6.0

func (c *GZCompressor) Write(d []byte) (int, error)

Write write bytes via compressor

func (*GZCompressor) WriteFooter added in v1.6.2

func (c *GZCompressor) WriteFooter() (err error)

WriteFooter write gz footer

func (*GZCompressor) WriteString added in v1.6.0

func (c *GZCompressor) WriteString(d string) (int, error)

WriteString write string via compressor

type GcOptFunc added in v1.10.2

type GcOptFunc func(*gcOption) error

GcOptFunc option for GC utils

func WithGCMemLimitFilePath added in v1.10.2

func WithGCMemLimitFilePath(path string) GcOptFunc

WithGCMemLimitFilePath set memory limit file

func WithGCMemRatio added in v1.10.2

func WithGCMemRatio(ratio int) GcOptFunc

WithGCMemRatio set mem ratio trigger for GC

default to 85

type HTTPClientOptFunc added in v1.13.0

type HTTPClientOptFunc func(*httpClientOption) error

HTTPClientOptFunc http client options

func WithHTTPClientInsecure added in v1.12.3

func WithHTTPClientInsecure() HTTPClientOptFunc

WithHTTPClientInsecure set http client igonre ssl issue

default to false

func WithHTTPClientMaxConn added in v1.12.3

func WithHTTPClientMaxConn(maxConn int) HTTPClientOptFunc

WithHTTPClientMaxConn set http client max connection

default to 20

func WithHTTPClientTimeout added in v1.12.3

func WithHTTPClientTimeout(timeout time.Duration) HTTPClientOptFunc

WithHTTPClientTimeout set http client timeout

default to 30s

type HeapItemItf added in v1.15.0

type HeapItemItf interface {
	GetKey() interface{}
	GetPriority() int
}

HeapItemItf items need to sort

func GetLargestNItems added in v1.15.0

func GetLargestNItems(inputChan <-chan HeapItemItf, topN int) ([]HeapItemItf, error)

GetLargestNItems get N highest priority items

Example
var (
	itemsWaitToSort = HeapSlice{
		&heapItem{p: 1},
		&heapItem{p: 3},
		&heapItem{p: 55},
		&heapItem{p: 2},
		&heapItem{p: 4441},
		&heapItem{p: 15555},
		&heapItem{p: 122},
	}
	itemChan = make(chan HeapItemItf)
)

go func() {
	for _, item := range itemsWaitToSort {
		itemChan <- item
	}

	close(itemChan)
}()

items, err := GetLargestNItems(itemChan, 3)
if err != nil {
	panic(err)
}

for _, item := range items {
	// 15555
	// 4441
	// 112
	fmt.Println(item.GetPriority())
}
Output:

func GetSmallestNItems added in v1.15.0

func GetSmallestNItems(inputChan <-chan HeapItemItf, topN int) ([]HeapItemItf, error)

GetSmallestNItems get N smallest priority items

Example
var (
	itemsWaitToSort = HeapSlice{
		&heapItem{p: 1},
		&heapItem{p: 3},
		&heapItem{p: 55},
		&heapItem{p: 2},
		&heapItem{p: 4441},
		&heapItem{p: 15555},
		&heapItem{p: 122},
	}
	itemChan = make(chan HeapItemItf)
)

go func() {
	for _, item := range itemsWaitToSort {
		itemChan <- item
	}

	close(itemChan)
}()

items, err := GetSmallestNItems(itemChan, 3)
if err != nil {
	panic(err)
}

for _, item := range items {
	// 1
	// 2
	// 3
	fmt.Println(item.GetPriority())
}
Output:

func GetTopKItems added in v1.15.0

func GetTopKItems(inputChan <-chan HeapItemItf, topN int, isHighest bool) ([]HeapItemItf, error)

GetTopKItems calculate topN by heap

  • use min-heap to calculates topN Highest items.
  • use max-heap to calculates topN Lowest items.

type HeapSlice added in v1.15.0

type HeapSlice []HeapItemItf

type Int64CounterItf added in v1.7.8

type Int64CounterItf interface {
	Count() int64
	CountN(n int64) int64
}

Int64CounterItf counter for int64

type JWT

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

JWT is token utils that support HS256/ES256

Example
secret = []byte("4738947328rh3ru23f32hf238f238fh28f")
j, err := NewJWT(
	WithJWTSignMethod(SignMethodHS256),
	WithJWTSecretByte(secret),
)
if err != nil {
	Logger.Panic("new jwt", zap.Error(err))
}

type jwtClaims struct {
	jwt.RegisteredClaims
}

claims := &jwtClaims{
	jwt.RegisteredClaims{
		Subject: "laisky",
	},
}

// signing
token, err := j.Sign(claims)
if err != nil {
	Logger.Panic("sign jwt", zap.Error(err))
}

// verify
claims = &jwtClaims{}
if err := j.ParseClaims(token, claims); err != nil {
	Logger.Panic("sign jwt", zap.Error(err))
}
Output:

func NewJWT added in v1.3.3

func NewJWT(opts ...JWTOptFunc) (e *JWT, err error)

NewJWT create new JWT utils

func (*JWT) ParseClaims added in v1.12.0

func (e *JWT) ParseClaims(token string, claimsPtr jwt.Claims, opts ...JWTDiviceOptFunc) error

ParseClaims parse token to claims

func (*JWT) ParseClaimsByES256 added in v1.12.0

func (e *JWT) ParseClaimsByES256(token string, claimsPtr jwt.Claims, opts ...JWTDiviceOptFunc) error

ParseClaimsByES256 parse token to claims by ES256

func (*JWT) ParseClaimsByHS256 added in v1.12.0

func (e *JWT) ParseClaimsByHS256(token string, claimsPtr jwt.Claims, opts ...JWTDiviceOptFunc) error

ParseClaimsByHS256 parse token to claims by HS256

func (*JWT) ParseClaimsByRS256 added in v1.17.0

func (e *JWT) ParseClaimsByRS256(token string, claimsPtr jwt.Claims, opts ...JWTDiviceOptFunc) error

ParseClaimsByRS256 parse token to claims by rs256

func (*JWT) Sign added in v1.12.0

func (e *JWT) Sign(claims jwt.Claims, opts ...JWTDiviceOptFunc) (string, error)

Sign sign claims to token

func (*JWT) SignByES256 added in v1.12.0

func (e *JWT) SignByES256(claims jwt.Claims, opts ...JWTDiviceOptFunc) (string, error)

SignByES256 signing claims by ES256

func (*JWT) SignByHS256 added in v1.12.0

func (e *JWT) SignByHS256(claims jwt.Claims, opts ...JWTDiviceOptFunc) (string, error)

SignByHS256 signing claims by HS256

type JWTDiviceOptFunc added in v1.12.0

type JWTDiviceOptFunc func(*jwtDivideOpt) error

JWTDiviceOptFunc options to use separate secret for every user in parsing/signing

func WithJWTDividePriKey added in v1.12.0

func WithJWTDividePriKey(priKey []byte) JWTDiviceOptFunc

WithJWTDividePriKey set asymmetrical private key for each signning/verify

func WithJWTDividePubKey added in v1.12.0

func WithJWTDividePubKey(pubKey []byte) JWTDiviceOptFunc

WithJWTDividePubKey set asymmetrical public key for each signning/verify

func WithJWTDivideSecret added in v1.12.0

func WithJWTDivideSecret(secret []byte) JWTDiviceOptFunc

WithJWTDivideSecret set symmetric key for each signning/verify

type JWTOptFunc added in v1.10.0

type JWTOptFunc func(*JWT) error

JWTOptFunc options to setup JWT

func WithJWTPriKeyByte added in v1.12.0

func WithJWTPriKeyByte(prikey []byte) JWTOptFunc

WithJWTPriKeyByte set jwt asymmetrical private key

func WithJWTPubKeyByte added in v1.12.0

func WithJWTPubKeyByte(pubkey []byte) JWTOptFunc

WithJWTPubKeyByte set jwt asymmetrical public key

func WithJWTSecretByte added in v1.12.0

func WithJWTSecretByte(secret []byte) JWTOptFunc

WithJWTSecretByte set jwt symmetric signning key

func WithJWTSignMethod added in v1.10.0

func WithJWTSignMethod(method jwt.SigningMethod) JWTOptFunc

WithJWTSignMethod set jwt signing method

type LRUExpiredMap added in v1.15.0

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

LRUExpiredMap map with expire time, auto delete expired item.

`Get` will auto refresh item's expires.

func NewLRUExpiredMap added in v1.15.0

func NewLRUExpiredMap(ctx context.Context,
	ttl time.Duration,
	new func() interface{}) (el *LRUExpiredMap, err error)

NewLRUExpiredMap new ExpiredMap

func (*LRUExpiredMap) Get added in v1.15.0

func (e *LRUExpiredMap) Get(key string) interface{}

Get get item

will auto refresh key's ttl

type LimitSizeHeap added in v1.15.0

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

LimitSizeHeap heap with limit size

func NewLimitSizeHeap added in v1.15.0

func NewLimitSizeHeap(size int, isHighest bool) (h *LimitSizeHeap, err error)

NewLimitSizeHeap create new LimitSizeHeap

func (*LimitSizeHeap) Pop added in v1.15.0

func (h *LimitSizeHeap) Pop() HeapItemItf

Pop pop from the tail. if `isHighest=True`, pop the biggest item

func (*LimitSizeHeap) Push added in v1.15.0

func (h *LimitSizeHeap) Push(item HeapItemItf) HeapItemItf

Push push item into heap, return popped item if exceed size

type LoggerEncoding added in v1.15.0

type LoggerEncoding string

type LoggerItf added in v1.17.0

type LoggerItf interface {
	Level() zapcore.Level
	ChangeLevel(level string) (err error)
	DebugSample(sample int, msg string, fields ...zapcore.Field)
	InfoSample(sample int, msg string, fields ...zapcore.Field)
	WarnSample(sample int, msg string, fields ...zapcore.Field)
	Clone() LoggerItf
	Named(s string) LoggerItf
	With(fields ...zapcore.Field) LoggerItf
	WithOptions(opts ...zap.Option) LoggerItf
	// contains filtered or unexported methods
}
var (
	/*Logger logging tool.

	* Info(msg string, fields ...Field)
	* Debug(msg string, fields ...Field)
	* Warn(msg string, fields ...Field)
	* Error(msg string, fields ...Field)
	* Panic(msg string, fields ...Field)
	* DebugSample(sample int, msg string, fields ...zapcore.Field)
	* InfoSample(sample int, msg string, fields ...zapcore.Field)
	* WarnSample(sample int, msg string, fields ...zapcore.Field)
	 */
	Logger LoggerItf
)

func CreateNewDefaultLogger added in v1.10.1

func CreateNewDefaultLogger(name, level string, opts ...zap.Option) (l LoggerItf, err error)

CreateNewDefaultLogger set default utils.Logger

func NewConsoleLoggerWithName added in v1.12.8

func NewConsoleLoggerWithName(name, level string, opts ...zap.Option) (l LoggerItf, err error)

NewConsoleLoggerWithName create new logger with name

func NewLogger added in v1.8.0

func NewLogger(optfs ...LoggerOptFunc) (l LoggerItf, err error)

NewLogger create new logger

func NewLoggerWithName added in v1.9.0

func NewLoggerWithName(name, level string, opts ...zap.Option) (l LoggerItf, err error)

NewLoggerWithName create new logger with name

type LoggerOptFunc added in v1.17.0

type LoggerOptFunc func(l *loggerOption) error

func WithLoggerEncoding added in v1.15.0

func WithLoggerEncoding(format LoggerEncoding) LoggerOptFunc

WithLoggerEncoding set logger encoding formet

func WithLoggerErrorOutputPaths added in v1.15.0

func WithLoggerErrorOutputPaths(paths []string) LoggerOptFunc

WithLoggerErrorOutputPaths set error logs output path

like "stderr"

func WithLoggerLevel added in v1.15.0

func WithLoggerLevel(level string) LoggerOptFunc

WithLoggerLevel set logger level

func WithLoggerName added in v1.15.0

func WithLoggerName(name string) LoggerOptFunc

WithLoggerName set logger name

func WithLoggerOutputPaths added in v1.15.0

func WithLoggerOutputPaths(paths []string) LoggerOptFunc

WithLoggerOutputPaths set output path

like "stdout"

func WithLoggerZapOptions added in v1.15.0

func WithLoggerZapOptions(opts ...zap.Option) LoggerOptFunc

WithLoggerZapOptions set logger with zap.Option

type LoggerType

type LoggerType struct {
	*zap.Logger
	// contains filtered or unexported fields
}

LoggerType extend from zap.Logger

func (*LoggerType) ChangeLevel added in v1.8.0

func (l *LoggerType) ChangeLevel(level string) (err error)

ChangeLevel change logger level

all children logger share the same level of their parent logger, so if you change any logger's level, all its parent and children logger's level will be changed.

func (*LoggerType) Clone added in v1.10.1

func (l *LoggerType) Clone() LoggerItf

Clone clone new Logger that inherit all config

func (*LoggerType) DebugSample

func (l *LoggerType) DebugSample(sample int, msg string, fields ...zapcore.Field)

DebugSample emit debug log with propability sample/SampleRateDenominator. sample could be [0, 1000], less than 0 means never, great than 1000 means certainly

func (*LoggerType) InfoSample

func (l *LoggerType) InfoSample(sample int, msg string, fields ...zapcore.Field)

InfoSample emit info log with propability sample/SampleRateDenominator

func (*LoggerType) Level added in v1.13.11

func (l *LoggerType) Level() zapcore.Level

Level get current level of logger

func (*LoggerType) Named added in v1.10.1

func (l *LoggerType) Named(s string) LoggerItf

Named adds a new path segment to the logger's name. Segments are joined by periods. By default, Loggers are unnamed.

func (*LoggerType) WarnSample

func (l *LoggerType) WarnSample(sample int, msg string, fields ...zapcore.Field)

WarnSample emit warn log with propability sample/SampleRateDenominator

func (*LoggerType) With added in v1.9.4

func (l *LoggerType) With(fields ...zapcore.Field) LoggerItf

With creates a child logger and adds structured context to it. Fields added to the child don't affect the parent, and vice versa.

func (*LoggerType) WithOptions added in v1.9.4

func (l *LoggerType) WithOptions(opts ...zap.Option) LoggerItf

WithOptions clones the current Logger, applies the supplied Options, and returns the resulting Logger. It's safe to use concurrently.

type Mail

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

Mail easy way to send basic email

Example
sender := NewMail("smtp_host", 53)
if err := sender.Send(
	"fromAddr",
	"toAddr",
	"frName",
	"toName",
	"Title",
	"Content",
); err != nil {
	Logger.Error("try to send email got error", zap.Error(err))
}
Output:

func NewMail

func NewMail(host string, port int) *Mail

NewMail create Mail with SMTP host and port

func (*Mail) BuildMessage

func (m *Mail) BuildMessage(msg string) string

BuildMessage implement

func (*Mail) Login

func (m *Mail) Login(username, password string)

Login login to SMTP server

func (*Mail) Send

func (m *Mail) Send(frAddr, toAddr, frName, toName, subject, content string, optfs ...MailSendOptFunc) (err error)

Send send email

type MailSendOptFunc added in v1.17.0

type MailSendOptFunc func(*mailSendOpt)

MailSendOptFunc is a function to set option for Mail.Send

func WithMailSendDialer added in v1.17.0

func WithMailSendDialer(dialerFact func(host string, port int, username, passwd string) EmailDialer) MailSendOptFunc

WithMailSendDialer set gomail.Dialer

type Mutex added in v1.3.7

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

Mutex mutex that support unblocking lock

Example
l := NewMutex()
if !l.TryLock() {
	Logger.Info("can not acquire lock")
	return
}
defer l.ForceRelease()
Output:

func NewMutex added in v1.3.7

func NewMutex() *Mutex

NewMutex create new mutex

func (*Mutex) ForceRelease added in v1.7.5

func (m *Mutex) ForceRelease()

ForceRelease force release lock

func (*Mutex) IsLocked added in v1.3.7

func (m *Mutex) IsLocked() bool

IsLocked return true if is locked

func (*Mutex) SpinLock added in v1.3.7

func (m *Mutex) SpinLock(step, timeout time.Duration)

SpinLock block until succee acquired lock

func (*Mutex) TryLock added in v1.3.7

func (m *Mutex) TryLock() bool

TryLock return true if succeed locked

func (*Mutex) TryRelease added in v1.3.7

func (m *Mutex) TryRelease() bool

TryRelease return true if succeed release

type PGZCompressor added in v1.10.0

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

PGZCompressor compress by gz with buf

Example
originText := testCompressraw
writer := &bytes.Buffer{}

var err error
// writer
c, err := NewPGZCompressor(writer)
if err != nil {
	Logger.Error("new compressor", zap.Error(err))
	return
}
if _, err = c.WriteString(originText); err != nil {
	Logger.Error("write string to compressor", zap.Error(err))
	return
}
if err = c.Flush(); err != nil {
	Logger.Error("flush compressor", zap.Error(err))
	return
}

// reader
var gz *gzip.Reader
if gz, err = gzip.NewReader(writer); err != nil {
	Logger.Error("new compressor", zap.Error(err))
	return
}

var bs []byte
if bs, err = ioutil.ReadAll(gz); err != nil {
	Logger.Error("read from compressor", zap.Error(err))
	return
}

got := string(bs)
if got != originText {
	Logger.Error("extract compressed text invalidate",
		zap.String("got", got),
		zap.ByteString("expect", bs))
	return
}
Output:

func NewPGZCompressor added in v1.10.0

func NewPGZCompressor(writer io.Writer, opts ...CompressOptFunc) (c *PGZCompressor, err error)

NewPGZCompressor create new PGZCompressor

func (*PGZCompressor) Flush added in v1.10.0

func (c *PGZCompressor) Flush() (err error)

Flush flush buffer bytes into bottom writer with gz meta footer

func (*PGZCompressor) Write added in v1.10.0

func (c *PGZCompressor) Write(d []byte) (int, error)

Write write bytes via compressor

func (*PGZCompressor) WriteFooter added in v1.10.0

func (c *PGZCompressor) WriteFooter() (err error)

WriteFooter write gz footer

func (*PGZCompressor) WriteString added in v1.10.0

func (c *PGZCompressor) WriteString(d string) (int, error)

WriteString write string via compressor

type PairList

type PairList []SortItemItf

PairList array of sort items

func SortBiggest

func SortBiggest(items PairList) PairList

SortBiggest sort from biggest to smallest

func SortSmallest

func SortSmallest(items PairList) PairList

SortSmallest sort from smallest to biggest

func (PairList) Len

func (p PairList) Len() int

Len return length of sort items

func (PairList) Less

func (p PairList) Less(i, j int) bool

Less compare two items

func (PairList) Swap

func (p PairList) Swap(i, j int)

Swap change two items

type ParallelCounter added in v1.6.3

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

ParallelCounter parallel count with child counter

func NewParallelCounter added in v1.6.3

func NewParallelCounter(quoteStep, rotatePoint int64) (*ParallelCounter, error)

NewParallelCounter get new parallel counter

func NewParallelCounterFromN added in v1.6.3

func NewParallelCounterFromN(n, quoteStep, rotatePoint int64) (*ParallelCounter, error)

NewParallelCounterFromN get new parallel counter

func (*ParallelCounter) GetChild added in v1.6.3

func (c *ParallelCounter) GetChild() *ChildParallelCounter

GetChild create new child

func (*ParallelCounter) GetQuote added in v1.6.3

func (c *ParallelCounter) GetQuote(step int64) (from, to int64)

GetQuote child request new quote from parent

type PriorityQ added in v1.15.0

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

PriorityQ lower structure used by heap

do not use this structure directly

func NewPriorityQ added in v1.15.0

func NewPriorityQ(isMaxTop bool) *PriorityQ

NewPriorityQ create new PriorityQ

func (*PriorityQ) Len added in v1.15.0

func (p *PriorityQ) Len() int

Len get length of items in heapq

func (*PriorityQ) Less added in v1.15.0

func (p *PriorityQ) Less(i, j int) bool

Less compare two items in heapq

func (*PriorityQ) Pop added in v1.15.0

func (p *PriorityQ) Pop() (popped interface{})

Pop pop from the tail. if `isMaxTop=True`, pop the biggest item

func (*PriorityQ) Push added in v1.15.0

func (p *PriorityQ) Push(x interface{})

Push push new item into heapq

func (*PriorityQ) Remove added in v1.15.0

func (p *PriorityQ) Remove(v HeapItemItf) (ok bool)

Remove remove an specific item

func (*PriorityQ) Swap added in v1.15.0

func (p *PriorityQ) Swap(i, j int)

Swap swat two items in heapq

type RequestData

type RequestData struct {
	Headers map[string]string
	Data    interface{}
}

RequestData http request

type RotateCounter

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

RotateCounter rotate counter

Example
counter, err := NewRotateCounter(10)
if err != nil {
	panic(err)
}

counter.Count()    // 1
counter.CountN(10) // 1
Output:

func NewRotateCounter

func NewRotateCounter(rotatePoint int64) (*RotateCounter, error)

NewRotateCounter create new RotateCounter with threshold from 0

func NewRotateCounterFromN

func NewRotateCounterFromN(n, rotatePoint int64) (*RotateCounter, error)

NewRotateCounterFromN create new RotateCounter with threshold from N

func NewRotateCounterFromNWithCtx added in v1.7.5

func NewRotateCounterFromNWithCtx(ctx context.Context, n, rotatePoint int64) (*RotateCounter, error)

NewRotateCounterFromNWithCtx create new RotateCounter with threshold from N

func NewRotateCounterWithCtx added in v1.7.5

func NewRotateCounterWithCtx(ctx context.Context, rotatePoint int64) (*RotateCounter, error)

NewRotateCounterWithCtx create new RotateCounter with threshold from 0

func (*RotateCounter) Close added in v1.7.5

func (c *RotateCounter) Close()

Close stop rorate runner

func (*RotateCounter) Count

func (c *RotateCounter) Count() int64

Count increse and return the result

func (*RotateCounter) CountN

func (c *RotateCounter) CountN(n int64) (r int64)

CountN increse N and return the result

func (*RotateCounter) Get added in v1.7.8

func (c *RotateCounter) Get() int64

Get return current counter's number

type SettingsEncryptOptf added in v1.13.5

type SettingsEncryptOptf func(*settingsAESEncryptOpt) error

SettingsEncryptOptf options to encrypt files in dir

func AESEncryptFilesInDirFileAppend deprecated added in v1.13.5

func AESEncryptFilesInDirFileAppend(append string) SettingsEncryptOptf

AESEncryptFilesInDirFileAppend will append in encrypted file'name before ext

xxx.toml -> xxx.enc.toml

Deprecated: use AESEncryptFilesInDirFileSuffix instead

func AESEncryptFilesInDirFileExt added in v1.13.5

func AESEncryptFilesInDirFileExt(ext string) SettingsEncryptOptf

AESEncryptFilesInDirFileExt only encrypt files with specific ext

func AESEncryptFilesInDirFileSuffix added in v1.16.0

func AESEncryptFilesInDirFileSuffix(suffix string) SettingsEncryptOptf

AESEncryptFilesInDirFileSuffix will append to encrypted's filename as suffix

xxx.toml -> xxx.toml.enc

type SettingsOptFunc added in v1.13.2

type SettingsOptFunc func(*settingsOpt) error

SettingsOptFunc opt for settings

func WithSettingsAesEncrypt added in v1.13.4

func WithSettingsAesEncrypt(key []byte) SettingsOptFunc

WithSettingsAesEncrypt decrypt config file by aes

func WithSettingsEnableInclude added in v1.16.0

func WithSettingsEnableInclude() SettingsOptFunc

WithSettingsEnableInclude enable `include` in config file

func WithSettingsEncryptedFileContain deprecated added in v1.13.5

func WithSettingsEncryptedFileContain(filenameMark string) SettingsOptFunc

WithSettingsEncryptedFileContain only decrypt files which name contains `filenameMark`

Deprecated: use WithSettingsEncryptedFileSuffix instead

func WithSettingsEncryptedFileSuffix added in v1.16.0

func WithSettingsEncryptedFileSuffix(suffix string) SettingsOptFunc

WithSettingsEncryptedFileSuffix only decrypt files which name ends with `encryptedSuffix`

func WithSettingsInclude deprecated added in v1.13.2

func WithSettingsInclude(enableInclude bool) SettingsOptFunc

WithSettingsInclude enable `include` in config file

Deprecated: use WithSettingsEnableInclude instead

type SettingsType

type SettingsType struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

SettingsType type of project settings

func NewSettings added in v1.16.0

func NewSettings() *SettingsType

NewSettings new settings

func (*SettingsType) BindPFlags

func (s *SettingsType) BindPFlags(p *pflag.FlagSet) error

BindPFlags bind pflags to settings

func (*SettingsType) Get

func (s *SettingsType) Get(key string) interface{}

Get get setting by key

func (*SettingsType) GetBool

func (s *SettingsType) GetBool(key string) bool

GetBool get setting by key

func (*SettingsType) GetDuration

func (s *SettingsType) GetDuration(key string) time.Duration

GetDuration get setting by key

func (*SettingsType) GetInt

func (s *SettingsType) GetInt(key string) int

GetInt get setting by key

func (*SettingsType) GetInt64

func (s *SettingsType) GetInt64(key string) int64

GetInt64 get setting by key

func (*SettingsType) GetString

func (s *SettingsType) GetString(key string) string

GetString get setting by key

func (*SettingsType) GetStringMap added in v1.5.4

func (s *SettingsType) GetStringMap(key string) map[string]interface{}

GetStringMap return map contains interface

func (*SettingsType) GetStringMapString added in v1.5.4

func (s *SettingsType) GetStringMapString(key string) map[string]string

GetStringMapString return map contains strings

func (*SettingsType) GetStringSlice

func (s *SettingsType) GetStringSlice(key string) []string

GetStringSlice get setting by key

func (*SettingsType) IsSet added in v1.5.4

func (s *SettingsType) IsSet(key string) bool

IsSet check whether exists

func (*SettingsType) LoadFromConfigServer added in v1.13.2

func (s *SettingsType) LoadFromConfigServer(url, app, profile, label string) (err error)

LoadFromConfigServer load configs from config-server,

endpoint `{url}/{app}/{profile}/{label}`

func (*SettingsType) LoadFromConfigServerWithRawYaml added in v1.13.2

func (s *SettingsType) LoadFromConfigServerWithRawYaml(url, app, profile, label, key string) (err error)

LoadFromConfigServerWithRawYaml load configs from config-server

endpoint `{url}/{app}/{profile}/{label}`

load raw yaml content and parse.

func (*SettingsType) LoadFromDir added in v1.13.2

func (s *SettingsType) LoadFromDir(dirPath string) error

LoadFromDir load settings from dir, default fname is `settings.yml`

func (*SettingsType) LoadFromFile added in v1.13.2

func (s *SettingsType) LoadFromFile(filePath string, opts ...SettingsOptFunc) (err error)

LoadFromFile load settings from file

func (*SettingsType) LoadSettings

func (s *SettingsType) LoadSettings()

LoadSettings load settings file

func (*SettingsType) Set

func (s *SettingsType) Set(key string, val interface{})

Set set setting by key

func (*SettingsType) Unmarshal added in v1.14.6

func (s *SettingsType) Unmarshal(obj interface{}) error

Unmarshal unmarshals the config into a Struct. Make sure that the tags on the fields of the structure are properly set.

func (*SettingsType) UnmarshalKey added in v1.14.6

func (s *SettingsType) UnmarshalKey(key string, obj interface{}) error

UnmarshalKey takes a single key and unmarshals it into a Struct.

type SingleItemExpCache added in v1.15.0

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

SingleItemExpCache single item with expires

func NewSingleItemExpCache added in v1.15.0

func NewSingleItemExpCache(ttl time.Duration) *SingleItemExpCache

NewSingleItemExpCache new expcache contains single data

func (*SingleItemExpCache) Get added in v1.15.0

func (c *SingleItemExpCache) Get() (data interface{}, ok bool)

Get get data

if data is expired, ok=false

func (*SingleItemExpCache) GetString added in v1.15.0

func (c *SingleItemExpCache) GetString() (data string, ok bool)

GetString same as Get, but return string

func (*SingleItemExpCache) GetUintSlice added in v1.15.0

func (c *SingleItemExpCache) GetUintSlice() (data []uint, ok bool)

GetUintSlice same as Get, but return []uint

func (*SingleItemExpCache) Set added in v1.15.0

func (c *SingleItemExpCache) Set(data interface{})

Set set data and refresh expires

type SortItemItf

type SortItemItf interface {
	GetValue() int
	GetData() interface{}
}

SortItemItf interface of sort item

type StopSignalOptFunc added in v1.14.8

type StopSignalOptFunc func(*stopSignalOpt)

func WithStopSignalCloseSignals added in v1.14.8

func WithStopSignalCloseSignals(signals ...os.Signal) StopSignalOptFunc

WithStopSignalCloseSignals set signals that will trigger close

type Throttle

type Throttle struct {
	*ThrottleCfg
	// contains filtered or unexported fields
}

Throttle current limitor

Example
ctx := context.Background()
throttle, err := NewThrottleWithCtx(ctx, &ThrottleCfg{
	NPerSec: 10,
	Max:     100,
})
if err != nil {
	Logger.Panic("new throttle")
}
defer throttle.Close()

inChan := make(chan int)

for msg := range inChan {
	if !throttle.Allow() {
		continue
	}

	// do something with msg
	fmt.Println(msg)
}
Output:

func NewThrottleWithCtx added in v1.9.0

func NewThrottleWithCtx(ctx context.Context, cfg *ThrottleCfg) (t *Throttle, err error)

NewThrottleWithCtx create new Throttle

90x faster than `rate.NewLimiter`

func (*Throttle) Allow

func (t *Throttle) Allow() bool

Allow check whether is allowed

func (*Throttle) Close added in v1.9.0

func (t *Throttle) Close()

Close stop throttle

type ThrottleCfg

type ThrottleCfg struct {
	Max, NPerSec int
}

ThrottleCfg Throttle's configuration

type Uint32Counter

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

Uint32Counter uint32 counter

func NewUint32Counter

func NewUint32Counter() *Uint32Counter

NewUint32Counter return new Uint32Counter from 0

func NewUint32CounterFromN

func NewUint32CounterFromN(n uint32) *Uint32Counter

NewUint32CounterFromN return new Uint32Counter from n

func (*Uint32Counter) Count

func (c *Uint32Counter) Count() uint32

Count increse and return the result

func (*Uint32Counter) CountN

func (c *Uint32Counter) CountN(n uint32) uint32

CountN increse N and return the result

func (*Uint32Counter) Get

func (c *Uint32Counter) Get() uint32

Get return current counter's number

func (*Uint32Counter) Set

func (c *Uint32Counter) Set(n uint32)

Set overwrite the counter's number

Directories

Path Synopsis
cmd
Package cmd some useful tools for command argument
Package cmd some useful tools for command argument
Package gorm some useful tools for gorm
Package gorm some useful tools for gorm

Jump to

Keyboard shortcuts

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