kiwi

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2024 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ModeDebug = "debug"
	ModeDev   = "dev"
	ModeProd  = "prod"
)
View Source
const (
	N_Service_Start = "service_start"
	N_Service_Stop  = "service_stop"
)
View Source
const (
	Evt_Start           = "start"
	Evt_Stop            = "stop"
	Evt_Svc_Connected   = "svc_connected"
	Evt_Svc_Disonnected = "svc_disconnected"
)
View Source
const (
	SDebug = "debug"
	SInfo  = "info"
	SWarn  = "warn"
	SError = "error"
	SFatal = "fatal"
)
View Source
const (
	DefTimeFormatter = "2006-01-02 15:04:05.999"
)

Variables

View Source
var (
	TestLevels = []TLevel{TDebug, TInfo, TWarn, TError, TFatal}
	DevLevels  = []TLevel{TInfo, TWarn, TError, TFatal}
	ProdLevels = []TLevel{TWarn, TError, TFatal}
)
View Source
var Mod = TSvcCode(1000)

Functions

func AddLogger

func AddLogger(logger ILogger)

func AddVar

func AddVar[T Var](name string, def T, usage string)

func BeforeExitCh

func BeforeExitCh(name string) chan<- struct{}

func BeforeExitFn

func BeforeExitFn(name string, fn util.Fn)

func BindEvent

func BindEvent(name string, handler EventHandler[util.M])

func CodecSpawn

func CodecSpawn[T any](svc TSvc, mtd TCode) (T, *util.Err)

func CodecSpawnRes

func CodecSpawnRes[T any](svc TSvc, mtd TCode) (T, *util.Err)

func Debug

func Debug(str string, params util.M)

func DispatchEvent

func DispatchEvent(name string, data any)

func Error

func Error(err *util.Err)

func Error2

func Error2(code util.TErrCode, m util.M)

func Error3

func Error3(code util.TErrCode, e error)

func Fatal

func Fatal(err *util.Err)

func Fatal2

func Fatal2(code util.TErrCode, m util.M)

func Fatal3

func Fatal3(code util.TErrCode, e error)

func GetCaller

func GetCaller(skip int) string

func GetVar

func GetVar[T Var](name string) (T, bool)

func GoWaitGroup

func GoWaitGroup(secs int, wg *sync.WaitGroup, over util.FnBool, ticker util.FnInt)

func Info

func Info(str string, params util.M)

func LevelToStr

func LevelToStr(l TLevel) string

func ParseVar

func ParseVar()

func SetCallerSkip

func SetCallerSkip(skip int)

func SetCodec

func SetCodec(codec ICodec)

func SetGate

func SetGate(gate IGate)

func SetLogDefParams

func SetLogDefParams(params util.M)

func SetNode

func SetNode(node INode)

func SetPacker

func SetPacker(packer IPacker)

func SetRouter

func SetRouter(router IRouter)

func SplitSvcCode

func SplitSvcCode(sc TSvcCode) (TSvc, TCode)

func TC

func TC(pid int64, params util.M, exclude bool) int64

TC 链路标记

func TD

func TD(tid int64, msg string, params util.M)

TD 链路Debug

func TE

func TE(tid int64, err *util.Err)

TE 链路Error

func TE2

func TE2(tid int64, code util.TErrCode, m util.M)

func TE3

func TE3(tid int64, code util.TErrCode, e error)

func TF

func TF(tid int64, err *util.Err)

TF 链路Fatal

func TF2

func TF2(tid int64, code util.TErrCode, m util.M)

func TI

func TI(tid int64, msg string, params util.M)

TI 链路Info

func TW

func TW(tid int64, err *util.Err)

TW 链路Warn

func TW2

func TW2(tid int64, code util.TErrCode, m util.M)

func TW3

func TW3(tid int64, code util.TErrCode, e error)

func UnbindEvent

func UnbindEvent(name string, handler EventHandler[util.M])

func WaitExit

func WaitExit()

func WaitGroup

func WaitGroup(secs int, wg *sync.WaitGroup, ticker util.FnInt) bool

func Warn

func Warn(err *util.Err)

func Warn2

func Warn2(code util.TErrCode, m util.M)

func Warn3

func Warn3(code util.TErrCode, e error)

Types

type AgentBytesToBytes

type AgentBytesToBytes func(IAgent, []byte) []byte

type AgentBytesToBytesErr

type AgentBytesToBytesErr func(IAgent, []byte) ([]byte, *util.Err)

type AgentOpt

type AgentOpt struct {
	PacketMaxCap uint32     //最大包长
	PacketMinCap uint32     //包长最小容量
	OnErr        util.FnErr //处理错误
	DeadlineSecs int
	AgentMode    AgentRWMode
	HeadLen      uint32
}

AgentOpt Agent代理选项

type AgentOption

type AgentOption func(o *AgentOpt)

AgentOpt Agent代理选项

func AgentDeadline

func AgentDeadline(secs int) AgentOption

func AgentErr

func AgentErr(onErr util.FnErr) AgentOption

AgentErr 连接关闭的回调,Error为空正常关闭

func AgentHeadLen

func AgentHeadLen(length uint32) AgentOption

func AgentMode

func AgentMode(mode AgentRWMode) AgentOption

func AgentPacketMaxCap

func AgentPacketMaxCap(packetMaxCap uint32) AgentOption

AgentPacketMaxCap 最大包长

func AgentPacketMinCap

func AgentPacketMinCap(packetMinCap uint32) AgentOption

AgentPacketMinCap 包长最小容量

type AgentRWMode

type AgentRWMode uint8
const (
	AgentRW AgentRWMode = iota
	AgentR
	AgentW
)

type Dispatcher

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

func Event

func Event() *Dispatcher[util.M]

func NewDispatcher

func NewDispatcher[T any](data T) *Dispatcher[T]

func (*Dispatcher[T]) Bind

func (d *Dispatcher[T]) Bind(name string, handler EventHandler[T])

func (*Dispatcher[T]) Dispatch

func (d *Dispatcher[T]) Dispatch(name string, data any)

func (*Dispatcher[T]) Unbind

func (d *Dispatcher[T]) Unbind(name string, handler EventHandler[T])

type EWorker

type EWorker uint8
const (
	EWorkerGo     EWorker = iota
	EWorkerActive         //需要key
	EWorkerShare          //需要key
	EWorkerGlobal
	EWorkerSelf
)

type EventHandler

type EventHandler[T any] func(T, any)

type EvtRouterConnected

type EvtRouterConnected struct {
	Svc  TSvc
	Id   int64
	Head util.M
}

type EvtRouterDisconnected

type EvtRouterDisconnected struct {
	Svc TSvc
	Id  int64
}

type EvtStart

type EvtStart struct {
	Wg *sync.WaitGroup
}

type EvtStop

type EvtStop struct {
	Wg *sync.WaitGroup
}

type EvtSvcConnected

type EvtSvcConnected struct {
	Svc  TSvc
	Id   int64
	Head util.M
}

type EvtSvcDisconnected

type EvtSvcDisconnected struct {
	Svc TSvc
	Id  int64
}

type FnAgent

type FnAgent func(IAgent)

type FnAgentBool

type FnAgentBool func(IAgent, bool)

type FnAgentBytes

type FnAgentBytes func(IAgent, []byte)

type FnAgentErr

type FnAgentErr func(IAgent, *util.Err)

type FnRcvPkt

type FnRcvPkt func(IRcvPkt)

type FnRcvPus

type FnRcvPus func(IRcvPush)

type FnRcvReq

type FnRcvReq func(IRcvRequest)

type GateReceiver

type GateReceiver func(agent IAgent, svc, method string, head util.M, body []byte, fnErr util.FnErr)

type IAgent

type IAgent interface {
	SetHead(key string, val any)
	SetHeads(m util.M)
	GetHead(key string) (any, bool)
	DelHead(keys ...string)
	CopyHead(m util.M)
	SetCache(key string, val any)
	SetCaches(m util.M)
	GetCache(key string) (any, bool)
	DelCache(keys ...string)
	CopyCache(m util.M)
	Id() string
	SetId(id string)
	Addr() string
	Host() string
	// Enable 状态
	Enable() *util.Enable
	// Send 发送数据
	Send(bytes []byte) *util.Err
	// Dispose 释放
	Dispose()
	BindConnected(fn FnAgent)
	BindDisconnected(fn FnAgentErr)
}

IAgent 连接代理接口

type ICodec

type ICodec interface {
	PbMarshal(obj util.IMsg) ([]byte, *util.Err)
	PbUnmarshal(data []byte, msg util.IMsg) *util.Err
	PbUnmarshal2(svc TSvc, mtd TCode, data []byte) (util.IMsg, *util.Err)
	JsonMarshal(obj util.IMsg) ([]byte, *util.Err)
	JsonUnmarshal(data []byte, msg util.IMsg) *util.Err
	JsonUnmarshal2(svc TSvc, mtd TCode, data []byte) (util.IMsg, *util.Err)
	Spawn(svc TSvc, mtd TCode) (util.IMsg, *util.Err)
	SpawnRes(svc TSvc, mtd TCode) (util.IMsg, *util.Err)
	BindFac(svc TSvc, mtd TCode, fac util.ToMsg)
	BindReqToRes(svc TSvc, req, res TCode)
	ReqToResCode(svc TSvc, req TCode) (TCode, *util.Err)
	MsgToSvcCode(msg util.IMsg) (svc TSvc, code TCode)
}

func Codec

func Codec() ICodec

type IDialer

type IDialer interface {
	// Name 拨号器名称
	Name() string
	// Connect 连接远程服务器
	Connect(ctx context.Context) *util.Err
	// Agent 连接代理
	Agent() IAgent
}

IDialer 拨号器接口

type IGate

type IGate interface {
	Dispose() *util.Err
	Send(tid int64, id string, bytes []byte, handler util.FnBool)
	AddrSend(tid int64, addr string, bytes []byte, handler util.FnBool)
	MultiSend(tid int64, idToPayload map[string][]byte, handler util.FnMapBool)
	MultiAddrSend(tid int64, addrToPayload map[string][]byte, handler util.FnMapBool)
	AllSend(tid int64, bytes []byte)
	CloseWithId(tid int64, id string, removeHeadKeys, removeCacheKeys []string)
	CloseWithAddr(tid int64, addr string, removeHeadKeys, removeCacheKeys []string)
	UpdateHeadCache(tid int64, id string, head, cache util.M, handler util.FnBool)
	UpdateAddrHeadCache(tid int64, addr string, head, cache util.M, handler util.FnBool)
	RemoveHeadCache(tid int64, addr string, head, cache []string, handler util.FnBool)
	RemoveAddrHeadCache(tid int64, addr string, head, cache []string, handler util.FnBool)
	GetHeadCache(tid int64, id string, fn util.FnM2Bool)
	GetAddrHeadCache(tid int64, id string, fn util.FnM2Bool)
	SetRoles(m map[TSvcCode][]int64)
	Authenticate(mask int64, svc TSvc, code TCode) bool
}

func Gate

func Gate() IGate

type IListener

type IListener interface {
	// Addr 监听地址
	Addr() string
	Port() int
	// Start 开始监听
	Start() *util.Err
	// Close 关闭监听
	Close()
}

IListener 监听器

type ILogger

type ILogger interface {
	// Log 记录日志
	Log(level TLevel, msg, caller string, stack []byte, params util.M)
	// Trace 标记链路
	Trace(pid, tid int64, caller string, params util.M)
	// Span 链路日志
	Span(level TLevel, tid int64, msg, caller string, stack []byte, params util.M)
}

ILogger 日志

type INode

type INode interface {
	Init() *util.Err
	Connect(ip string, port int, svc TSvc, nodeId int64, ver string, head util.M)
	Disconnect(svc TSvc, id int64)
	Push(pus ISndPush)
	PushNode(nodeId int64, pus ISndPush)
	Request(req ISndRequest)
	RequestNode(nodeId int64, req ISndRequest)
	Notify(ntf ISndNotice)
	ReceiveWatchNotice(nodeId int64, methods []TCode)
	SendToNode(nodeId int64, bytes []byte, fnErr util.FnErr)
}

func Node

func Node() INode

type INodeDialer

type INodeDialer interface {
	Svc() TSvc
	NodeId() int64
	Dialer() IDialer
	Head() util.M
	Send(bytes []byte, fnErr util.FnErr)
}

type INodeHandler

type INodeHandler interface {
	Receive(agent IAgent, bytes []byte)
}

type IPacker

type IPacker interface {
	PackWatchNotify(id int64, methods []TCode) []byte
	UnpackWatchNotify(bytes []byte) (id int64, methods []TCode, err *util.Err)
	PackPush(tid int64, pus ISndPush) ([]byte, *util.Err)
	UnpackPush(bytes []byte, pkg IRcvPush) (err *util.Err)
	UnpackPushBytes(bytes []byte, head util.M) (tid int64, json bool, payload []byte, err *util.Err)
	PackRequest(tid int64, req ISndRequest) ([]byte, *util.Err)
	UnpackRequest(bytes []byte, pkg IRcvRequest) (err *util.Err)
	PackResponseOk(tid int64, head util.M, pkt []byte) ([]byte, *util.Err)
	UnpackResponseOk(bytes []byte, head util.M) (tid int64, payload []byte, err *util.Err)
	PackResponseFail(tid int64, head util.M, code uint16) ([]byte, *util.Err)
	UnpackResponseFail(bytes []byte, head util.M) (tid int64, code uint16, readErr *util.Err)
	PackNotify(tid int64, ntf ISndNotice) ([]byte, *util.Err)
	UnpackNotify(bytes []byte, pkg IRcvNotice) (err *util.Err)
	PackM(m util.M) ([]byte, *util.Err)
	UnpackM(bytes []byte, m util.M) (err *util.Err)
}

func Packer

func Packer() IPacker

type IRcvNotice

type IRcvNotice interface {
	IRcvPkt
}

type IRcvPkt

type IRcvPkt interface {
	SenderId() int64
	Tid() int64
	Svc() TSvc
	Code() TCode
	Head() util.M
	HeadId() string
	Json() bool
	Msg() util.IMsg
	SetWorker(typ EWorker, key string)
	Worker() EWorker
	WorkerKey() string
	InitWithBytes(msgType uint8, tid int64, head util.M, json bool, bytes []byte) *util.Err
	InitWithMsg(msgType uint8, tid int64, head util.M, json bool, msg util.IMsg)
	Complete()
	Err(err *util.Err)
	Err2(code util.TErrCode, m util.M)
	Err3(code util.TErrCode, e error)
}

type IRcvPush

type IRcvPush interface {
	IRcvPkt
}

type IRcvRequest

type IRcvRequest interface {
	IRcvPkt
	Ok(msg util.IMsg)
	Fail(code uint16)
}

type IRouter

type IRouter interface {
	AddRequest(req ISndRequest)
	DelRequest(tid int64)
	BindPus(svc TSvc, code TCode, fn FnRcvPus)
	BindReq(svc TSvc, code TCode, fn FnRcvReq)
	OnPush(pkt IRcvPush)
	OnRequest(pkt IRcvRequest)
	OnResponseOk(tid int64, head util.M, msg util.IMsg)
	OnResponseOkBytes(tid int64, head util.M, bytes []byte)
	OnResponseFail(tid int64, head util.M, code uint16)
	WatchNotice(msg util.IMsg, handler NotifyHandler)
	GetWatchCodes(svc TSvc) ([]TCode, bool)
	OnNotice(pkt IRcvNotice)
}

func Router

func Router() IRouter

type IService

type IService interface {
	Svc() TSvc
	Start()
	Shutdown()
	Dispose()
}

type ISndNotice

type ISndNotice interface {
	ISndPacket
}

type ISndPacket

type ISndPacket interface {
	InitHead()
	Pid() int64
	Tid() int64
	Json() bool
	Svc() TSvc
	Code() TCode
	Head() util.M
	GetSvcNodeId() (int64, bool)
	Payload() []byte
	Msg() util.IMsg
	Dispose()
}

type ISndPush

type ISndPush interface {
	ISndPacket
}

type ISndRequest

type ISndRequest interface {
	ISndPacket
	SetBytesHandler(fail util.FnInt64MUint16, ok util.FnInt64MBytes)
	SetHandler(fail util.FnInt64MUint16, ok util.FnInt64MMsg)
	OkBytes(head util.M, bytes []byte)
	Ok(head util.M, msg util.IMsg)
	Fail(head util.M, code uint16)
}

type NodeDialerToBool

type NodeDialerToBool func(INodeDialer) bool

type NodeMeta

type NodeMeta struct {
	Ip        string
	Port      int
	NodeId    int64
	StartTime int64
	Data      util.M
	Mode      string
	Services  map[TSvc]string
}

func GetNodeMeta

func GetNodeMeta() *NodeMeta

func (*NodeMeta) AddService

func (n *NodeMeta) AddService(svc TSvc, ver string)

func (*NodeMeta) HasService

func (n *NodeMeta) HasService(svc TSvc) bool

func (*NodeMeta) Init

func (n *NodeMeta) Init(id int64)

type NotifyHandler

type NotifyHandler func(pkt IRcvNotice)

type PacketToStr

type PacketToStr func(IRcvPkt) string

type PktToKey

type PktToKey func(pkt IRcvPkt) string

type TCode

type TCode = uint8

type TLevel

type TLevel = int64
const (
	TDebug TLevel = 1 << iota
	TInfo
	TWarn
	TError
	TFatal
)

func LvlToMask

func LvlToMask(levels ...int64) TLevel

func StrLvlToMask

func StrLvlToMask(levels ...string) TLevel

func StrToLevel

func StrToLevel(l string) TLevel

type TSvc

type TSvc = uint16

type TSvcCode

type TSvcCode = uint16

func MergeSvcCode

func MergeSvcCode(svc TSvc, code TCode) TSvcCode

type Var

type Var interface {
	int | int64 | float64 | bool | string
}

type WorkerFn

type WorkerFn func(id string, fn util.FnAnySlc, params ...any)

Directories

Path Synopsis
etd
mgo
rds

Jump to

Keyboard shortcuts

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