misc

package
v0.0.0-...-b015d54 Latest Latest
Warning

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

Go to latest
Published: May 22, 2023 License: GPL-3.0 Imports: 29 Imported by: 2

Documentation

Index

Constants

View Source
const (
	BP_StatusReady   = 0
	BP_StatusRunning = 1
	BP_StatusClosed  = -1
)
View Source
const (
	RFC3339ms = "2006-01-02T15:04:05.000Z07:00"
)

Variables

This section is empty.

Functions

func Base64DecodeFilename

func Base64DecodeFilename(src string) ([]byte, error)

replace +/ with -_

func Base64DecodeMap

func Base64DecodeMap(str string) (data map[string]any, err error)

func Base64EncodeFilename

func Base64EncodeFilename(src string) string

replace +/ with -_

func Base64EncodeMap

func Base64EncodeMap(data map[string]any) string

func BasenameWithoutExt

func BasenameWithoutExt(cf string) (base string)

func BuildInfo

func BuildInfo() (info map[string]any)

func BuildInfoText

func BuildInfoText(info map[string]any) string

func Caller

func Caller(skips ...int) (string, string)

func CheckDuplicateFilename

func CheckDuplicateFilename(p string) (out string, err error)

func EqualVector

func EqualVector[T constraints.Ordered](arr1, arr2 []T) (ok bool)

func ExampleRequestTmpls

func ExampleRequestTmpls() string

func FileSize2Str

func FileSize2Str(n int64) string

func First

func First[T any](v []T) *T

func GetIP

func GetIP(addrs ...string) (ip string, err error)

func GetOClock

func GetOClock(shift int) (clock time.Time)

func Hello

func Hello()

func Last

func Last[T any](v []T) *T

func LoadPprof

func LoadPprof(mux *http.ServeMux)

create new Pprof and run server

func NTSFunc

func NTSFunc(delay int64) http.HandlerFunc

func NowMs

func NowMs() string

func ParseDatetime

func ParseDatetime(value string) (at time.Time, err error)

datetime format: "2021-06-24", "09:00:01", "2021-06-24 09:10:11" or "2021-06-24T09:10:11"

func PortFromAddr

func PortFromAddr(addr string) (port int, err error)

func PprofCollect

func PprofCollect(dir string, secs, hz int) (out string, err error)

hz = 100 is recommended

```bash

go install github.com/google/pprof@latest
pprof -eog wk_data/2022-12-01T20-38-26_10s_100hz_cpu.pprof.gz
go tool pprof -eog wk_data/2022-12-01T20-38-26_10s_100hz_cpu.pprof.gz

```

func RandString

func RandString(n int) string

func RegisterLogPrinter

func RegisterLogPrinter()

func RootDir

func RootDir() (dir string, err error)

get root path of project by recursively match go.mod in parent directory

func RootFile

func RootFile(p2f ...string) (fp string, err error)

func RootModule

func RootModule() (mod string, err error)

func SegnmentsDiv

func SegnmentsDiv(length, num int) (segs [][2]int)

func SliceGet

func SliceGet[T any](slice []T, index int) (val T, exists bool)

func SortSliceOfSlice

func SortSliceOfSlice[T constraints.Ordered](slice [][]T)

demo:

[[1,2], [3]] => [[3], [1,2]]
[[2,1], [1,2]] => [[1,2], [2, 1]]

func Stack

func Stack(prefix string) (slice []string)

func TimeCeil

func TimeCeil(at time.Time, tu string) (out time.Time, err error)

ceil time, e.g. TimeCeil('2020-12-01T17:39:07.123+08:00', "M") -> '2020-12-01T17:40:00+08:00'

valid unit(key or value)
H: hour, M: minute, S: second
y: year, s: season, m: month, w: week, d: day

func TimeFloor

func TimeFloor(at time.Time, tu string) (out time.Time, err error)

floor time, e.g. TimeFloor('2020-12-01T17:39:07.123+08:00', "M") -> '2020-12-01T17:39:00+08:00'

valid unit(key or value)
H: hour, M: minute, S: second
y: year, s: season, m: month, w: week, d: day

func TimeTag

func TimeTag(at time.Time) string

func UniqVector

func UniqVector[T constraints.Ordered](arr []T) (list []T)

func ValidJSON

func ValidJSON(bts []byte) (err error)

func VectorIndex

func VectorIndex[T constraints.Ordered](list []T, v T) int

Types

type BatchProcess

type BatchProcess[T any] struct {
	// contains filtered or unexported fields
}

func NewBatchProcess

func NewBatchProcess[T any](count int, duration time.Duration, process func([]T)) (
	bp *BatchProcess[T], err error)

func (*BatchProcess[T]) Down

func (bp *BatchProcess[T]) Down()

func (*BatchProcess[T]) Recv

func (bp *BatchProcess[T]) Recv(v T) (err error)

type DefaultLogger

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

func NewDefaultLogger

func NewDefaultLogger(writer io.Writer, withTs bool) (logger *DefaultLogger)

func (*DefaultLogger) Debug

func (logger *DefaultLogger) Debug(format string, a ...any)

func (*DefaultLogger) Error

func (logger *DefaultLogger) Error(format string, a ...any)

func (*DefaultLogger) Info

func (logger *DefaultLogger) Info(format string, a ...any)

func (*DefaultLogger) Printf

func (logger *DefaultLogger) Printf(format string, a ...any) (n int, err error)

func (*DefaultLogger) Trace

func (logger *DefaultLogger) Trace(format string, a ...any)

func (*DefaultLogger) Warn

func (logger *DefaultLogger) Warn(format string, a ...any)

type LogIntf

type LogIntf interface {
	// Trace(string, ...any)
	Debug(string, ...any)
	Info(string, ...any)
	Warn(string, ...any)
	Error(string, ...any)
}

customize yourself, no concurrency safety guaranteed

type LogPrinter

type LogPrinter struct{}

func (*LogPrinter) Write

func (w *LogPrinter) Write(bts []byte) (int, error)

type LogWriter

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

a simple log writer

func NewLogWriter

func NewLogWriter(prefix string, w io.Writer) (lw *LogWriter, err error)

func (*LogWriter) Close

func (lw *LogWriter) Close() (err error)

func (*LogWriter) Register

func (lw *LogWriter) Register()

set as output of log pkg

func (*LogWriter) Write

func (lw *LogWriter) Write(bts []byte) (int, error)

type NetworkTimeResult

type NetworkTimeResult struct {
	T1    time.Time `json:"t1"`
	T2    time.Time `json:"t2"`
	T3    time.Time `json:"t3"`
	T4    time.Time `json:"t4"`
	Sigma int64     `json:"sigma"`
	Delta int64     `json:"delta"`
}

func GetNetworkTime

func GetNetworkTime(addr string, delay int64) (result *NetworkTimeResult, err error)

func (NetworkTimeResult) String

func (result NetworkTimeResult) String() string

type NetworkTimeServer

type NetworkTimeServer struct {
	*http.Server
	// contains filtered or unexported fields
}

https://en.wikipedia.org/wiki/Network_Time_Protocol https://en.wikipedia.org/wiki/File:NTP-Algorithm.svg

func NewNetworkTimeServer

func NewNetworkTimeServer(addr string, delay int64) (ser *NetworkTimeServer, err error)

func (*NetworkTimeServer) Run

func (ser *NetworkTimeServer) Run() error

type Output

type Output struct {
	Key    string `mapstructure:"key"`    // key in json
	Header string `mapstructure:"header"` // header key
	Tmpl   string `mapstructure:"tmpl"`   // header value template
}

func (*Output) Get

func (output *Output) Get(bts []byte) (value string, err error)

type Parallel

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

func NewParallel

func NewParallel(n int) (p *Parallel)

func (*Parallel) Do

func (p *Parallel) Do(fn func() error, handle func(error))

func (*Parallel) Wait

func (p *Parallel) Wait()

type RequestTmpl

type RequestTmpl struct {
	Name            string            `mapstructure:"name"`
	Method          string            `mapstructure:"method"`
	Path            string            `mapstructure:"path"`
	Params          string            `mapstructure:"params"`
	Body            string            `mapstructure:"body"`
	NoPublicHeaders bool              `mapstructure:"no_public_headers"`
	Headers         map[string]string `mapstructure:"headers"`
	Outputs         []Output          `mapstructure:"outputs"`
}

type RequestTmpls

type RequestTmpls struct {
	Url     string            `mapstructure:"url"`
	Headers map[string]string `mapstructure:"headers"`
	Prelude []RequestTmpl     `mapstructure:"prelude"`
	APIs    []RequestTmpl     `mapstructure:"apis"`
	// contains filtered or unexported fields
}

func LoadRequestTmpls

func LoadRequestTmpls(name, fp string) (item *RequestTmpls, err error)

func (*RequestTmpls) Match

func (item *RequestTmpls) Match(names ...string) (tmpls []*RequestTmpl, err error)

func (*RequestTmpls) Request

func (item *RequestTmpls) Request(tmpl *RequestTmpl) (
	statusCode int, body string, err error)

type SigningMd5

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

func NewSigningMd5

func NewSigningMd5(secrete, key string, lowcase bool) (*SigningMd5, error)

func (*SigningMd5) SignQuery

func (sign *SigningMd5) SignQuery(param map[string]string) (query string)

func (*SigningMd5) SignValues

func (sign *SigningMd5) SignValues(param map[string]string) (value string)

func (*SigningMd5) VerifyQuery

func (sign *SigningMd5) VerifyQuery(query string) (err error)

Jump to

Keyboard shortcuts

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