utils

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 2, 2024 License: GPL-3.0 Imports: 27 Imported by: 0

Documentation

Index

Constants

View Source
const CtxKey_nolog = CtxKey("nolog") // 不打印日志,错误日志还会打印 值:不受限制 一般写1
View Source
const CtxKey_traceId = CtxKey("traceId") // context产生时,设置的唯一ID,用来链路追踪

Variables

View Source
var (
	LowerCaseLettersCharset = []byte("abcdefghijklmnopqrstuvwxyz")
	UpperCaseLettersCharset = []byte("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
	LettersCharset          = append(LowerCaseLettersCharset, UpperCaseLettersCharset...)
	NumbersCharset          = []byte("0123456789")
	AlphanumericCharset     = append(LettersCharset, NumbersCharset...)
	SpecialCharset          = []byte("!@#$%^&*()_+-=[]{}|;':\",./<>?")
	AllCharset              = append(AlphanumericCharset, SpecialCharset...)
)
View Source
var (
	TestHeatBeatReqMsg = &TestMsg{
		TestMsgHead: TestMsgHead{
			Msgid: 1,
		},
		BodyMsg: &TestHeatBeatReq{Data: string("heatreqmsg")},
	}
	TestHeatBeatRespMsg = &TestMsg{
		TestMsgHead: TestMsgHead{
			Msgid: 2,
		},
		BodyMsg: &TestHeatBeatResp{Data: string("heatrespmsg")},
	}

	TestRegMsgID = func(msgType reflect.Type) string {
		m, ok := reflect.New(msgType).Interface().(TestMsgBody)
		if ok {
			return strconv.Itoa(int(m.MsgID()))
		}
		return ""
	}
)

Functions

func Clamp

func Clamp[T Ordered](value T, min T, max T) T

func ClientIPHeader

func ClientIPHeader(header http.Header) *net.IPAddr

func ClientIPRequest

func ClientIPRequest(req *http.Request) *net.IPAddr

func ClientTCPIPHeader

func ClientTCPIPHeader(header http.Header) *net.TCPAddr

func Compact

func Compact[T comparable](collection []T) []T

func Contains

func Contains[T comparable](collection []T, value T) bool

func Count

func Count[T comparable](collection []T, value T) (count int)

func CountValues

func CountValues[T comparable](collection []T) map[T]int

func CreatePid

func CreatePid() bool

进程ID写入pid文件

func CronAddFunc

func CronAddFunc(spec string, cmd func()) (int, error)

添加一个定时任务

func Daemon

func Daemon()

func DefaultAntsPool

func DefaultAntsPool() *ants.Pool

暴露出原始对象

func Exit

func Exit(code int)

Exit 退出

func ExitWait

func ExitWait()

等待退出,一般放到main函数的最下面调用

func Exiting

func Exiting() bool

func GenTraceID

func GenTraceID() int64

func HandlePanic

func HandlePanic()

通用的panic处理函数

func HandlePanic2

func HandlePanic2(callback func())

func HandlePanicWithCaller

func HandlePanicWithCaller(caller *CallerDesc)

func IndexOf

func IndexOf[T comparable](collection []T, element T) int

func IsMatch

func IsMatch(pattern, str string) bool

通配符匹配 支持?和*

func IsSorted

func IsSorted[T Ordered](collection []T) bool

func LastIndexOf

func LastIndexOf[T comparable](collection []T, element T) int

func LocalIP

func LocalIP() (net.IP, error)

LocalIP ...

func LocalIPString

func LocalIPString() string

func LogCtx

func LogCtx(e *zerolog.Event, ctx context.Context) *zerolog.Event

方便ctx中内容输出使用

func LogHttpBody

func LogHttpBody(e *zerolog.Event, header http.Header, logkey string, data []byte, limit int) *zerolog.Event

打印http相关的内容日志,细节待完善

func LogHttpInterface

func LogHttpInterface(e *zerolog.Event, header http.Header, logkey string, body interface{}, limit int) *zerolog.Event

func Min

func Min[T Ordered](collection []T) T

func ParseIP

func ParseIP(addr net.Addr) string

func RandFloat32

func RandFloat32() float32

RandFloat32 float32[0,1.0)

func RandFloat64

func RandFloat64() float64

RandFloat64 float64[0,1.0)

func RandInt

func RandInt() int

RandInt int随机数

func RandInt31

func RandInt31() int32

RandInt31 int32随机数

func RandInt31n

func RandInt31n(n int32) int32

RandInt31n [0,n)的随机数

func RandInt31nm

func RandInt31nm(n, m int32) int32

RandInt31nm [n,m)的随机数

func RandInt31nm2

func RandInt31nm2(n, m int32) int32

RandInt31nm2 [n,m]的随机数

func RandInt63

func RandInt63() int64

RandInt63 int64随机数

func RandInt63n

func RandInt63n(n int64) int64

RandInt63n [0,n)的随机数

func RandInt63nm

func RandInt63nm(n, m int64) int64

RandInt63nm [n,m)的随机数

func RandInt63nm2

func RandInt63nm2(n, m int64) int64

RandInt63nm2 [n,m]的随机数

func RandIntn

func RandIntn(n int) int

RandIntn [0,n)的随机数

func RandIntnm

func RandIntnm(n, m int) int

RandIntnm [n,m)的随机数

func RandIntnm2

func RandIntnm2(n, m int) int

RandIntnm2 [n,m]的随机数

func RandShuffle

func RandShuffle(n int, swap func(i, j int))

func RandShuffleSlice

func RandShuffleSlice[T any](collection []T) []T

func RandString

func RandString(size int) string

生成随机字符串

func RandString2

func RandString2(size int, charset []byte) string

func RandUint32

func RandUint32() uint32

RandUint32 uint32

func RandUint64

func RandUint64() uint64

RandUint64 uint64

func RegExit

func RegExit(fn func(os.Signal))

注册退出函数

func Replace

func Replace[T comparable](collection []T, old T, new T, n int) []T

func ReplaceAll

func ReplaceAll[T comparable](collection []T, old T, new T) []T

func Reverse

func Reverse[T any](collection []T) []T

func Slice

func Slice[T any](collection []T, start int, end int) []T

func Submit

func Submit(task func()) error

提交一个任务

func SubmitProcess

func SubmitProcess(task func()) error

提交一个可以可以等待的任务

func Sum

func Sum[T Float | Integer | Complex](collection []T) T

func WaitProcess

func WaitProcess(timeout time.Duration)

等待提交的任务

Types

type CallerDesc

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

func GetCallerDesc

func GetCallerDesc(skip int) *CallerDesc

func (*CallerDesc) Name

func (c *CallerDesc) Name() string

返回 文件名:函数名 用作指标统计

func (*CallerDesc) Pos

func (c *CallerDesc) Pos() string

返回 文件名:函数名:行号 用作日志

type Complex

type Complex interface {
	~complex64 | ~complex128
}

type CtxKey

type CtxKey string

type Float

type Float interface {
	~float32 | ~float64
}

type Integer

type Integer interface {
	Signed | Unsigned
}

type Ordered

type Ordered interface {
	Integer | Float | ~string
}

type RecvMsger

type RecvMsger interface {
	MsgID() string                                           // 获取msgid
	BodyUnMarshal(msgType reflect.Type) (interface{}, error) // 根据具体消息类型,解析出消息体
}

接受消息接口,包括消息头和消息体结构

type SendMsger

type SendMsger interface {
	MsgID() string               // 获取msgid
	MsgMarshal() ([]byte, error) // 编码整个消息,返回的[]byte用来发送数据
}

发送消息接口

type Sequence

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

协成池调用任务队列 保证任务顺序执行

func (*Sequence) Submit

func (s *Sequence) Submit(task func())

type Signed

type Signed interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64
}

下面几种约束 参考https://cs.opensource.google/go/x/exp

type TestHeatBeatReq

type TestHeatBeatReq struct {
	Data string `json:"data,omitempty"` // 消息体就是一个字符串
}

func (*TestHeatBeatReq) Marshal

func (m *TestHeatBeatReq) Marshal() ([]byte, error)

func (*TestHeatBeatReq) MsgID

func (m *TestHeatBeatReq) MsgID() uint32

func (*TestHeatBeatReq) UnMarshal

func (m *TestHeatBeatReq) UnMarshal(buf []byte) error

type TestHeatBeatResp

type TestHeatBeatResp struct {
	Data string `json:"data,omitempty"` // 消息体就是一个字符串
}

func (*TestHeatBeatResp) Marshal

func (m *TestHeatBeatResp) Marshal() ([]byte, error)

func (*TestHeatBeatResp) MsgID

func (m *TestHeatBeatResp) MsgID() uint32

func (*TestHeatBeatResp) UnMarshal

func (m *TestHeatBeatResp) UnMarshal(buf []byte) error

type TestMsg

type TestMsg struct {
	TestMsgHead
	// 根据方向不一样,填充的字段不一样
	Data    []byte      // 接受数据时填充
	BodyMsg TestMsgBody // 发送数据填充
}

通用的消息测试使用

func TestDecodeMsg

func TestDecodeMsg(data []byte) (*TestMsg, int, error)

根据二进制解码出TestMsg结构

func (*TestMsg) Body

func (tm *TestMsg) Body() interface{}

func (*TestMsg) BodyUnMarshal

func (tm *TestMsg) BodyUnMarshal(msgType reflect.Type) (interface{}, error)

func (*TestMsg) Head

func (tm *TestMsg) Head() interface{}

func (*TestMsg) MarshalZerologObject

func (tm *TestMsg) MarshalZerologObject(e *zerolog.Event)

func (*TestMsg) MsgID

func (tm *TestMsg) MsgID() string

func (*TestMsg) MsgMarshal

func (tm *TestMsg) MsgMarshal() ([]byte, error)

type TestMsgBody

type TestMsgBody interface {
	MsgID() uint32
	Marshal() ([]byte, error)
	UnMarshal([]byte) error
}

type TestMsgHead

type TestMsgHead struct {
	Msgid uint32 `json:"msgid,omitempty"`
	Len   uint32 `json:"len,omitempty"`
}

type Unsigned

type Unsigned interface {
	~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
}

Jump to

Keyboard shortcuts

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