Documentation
¶
Overview ¶
提供了服务注册发现、服务互联互通、线路负载均衡、业务逻辑承载等功能.
Index ¶
- Constants
- Variables
- func BackupLan() int
- func ClearInterval(id int, tid ...int64)
- func ClearTimeout(id int, tid ...int64)
- func CloseLan()
- func MonitorLan()
- func NotifyCgi(id int, creq *xproto.CgiReq, cresp *xproto.CgiResp) bool
- func NotifyEvt(id int, param interface{}) bool
- func NotifyMsg(id int, mreq *xproto.MsgReq) bool
- func NotifyRpc(id int, rreq *xproto.RpcReq, rresp *xproto.RpcResp) bool
- func PauseLan()
- func PostKV(key string, value string, version string, block ...bool) bool
- func PullKV(key string) []byte
- func RecvLan()
- func RegCgi(id int, fun func(creq *xproto.CgiReq, cresp *xproto.CgiResp)) int
- func RegCgiRoute(_map map[int]*CgiRoute)
- func RegEvt(id int, fun func(interface{})) int
- func RegMsg(id int, fun func(*xproto.MsgReq)) int
- func RegMsgRoute(_map map[int]*MsgRoute)
- func RegRpc(id int, fun func(rreq *xproto.RpcReq, rresp *xproto.RpcResp)) int
- func RegRpcRoute(_map map[int]*RpcRoute)
- func RestoreLan()
- func ResumeLan()
- func RunIn(tid int64, fun func()) chan bool
- func RunInMain(fun func()) chan bool
- func SendAsync(id int, uid int, req proto.Message, addr string, ...)
- func SendCgi(id int, uid int, req *http.Request, addr string, timeout ...int) (cresp *xproto.CgiResp, err error)
- func SendFrame(frame xproto.IFrame) bool
- func SendMsg(id int, msg proto.Message, mreq *xproto.MsgReq) bool
- func SendSync(id int, uid int, req proto.Message, resp proto.Message, addr string, ...) error
- func Start(server IServer)
- func StartLan(lanCfg *LanCfg, handleMsg func(*xproto.MsgReq), ...)
- func Stop()
- func SubKV(key string, interval int, onUpdate func(data []byte))
- func UnregCgi(id int, hid int) bool
- func UnregEvt(id int, hid int) bool
- func UnregMsg(id int, hid int) bool
- func UnregRpc(id int, hid int) bool
- func WatchSignal() <-chan string
- type CgiFunc
- type CgiRoute
- type EvtFunc
- type IServer
- type LanCfg
- type LanClt
- type LanSvr
- type MsgFunc
- type MsgRoute
- type Proc
- type Route
- type RpcFunc
- type RpcRoute
- type Server
- func (this *Server) CTOR(CHILD interface{})
- func (this *Server) Destroy()
- func (this *Server) GetConfig() *SvrCfg
- func (this *Server) GetFPS() int
- func (this *Server) GetTitle() string
- func (_this *Server) Init() bool
- func (this *Server) InitConfig() bool
- func (this *Server) Name() string
- func (_this *Server) PreQuit()
- func (this *Server) RecvCgi(rreq *xproto.CgiReq, rresp *xproto.CgiResp)
- func (this *Server) RecvMsg(mreq *xproto.MsgReq)
- func (this *Server) RecvRpc(rreq *xproto.RpcReq, rresp *xproto.RpcResp)
- func (this *Server) Start()
- func (this *Server) Update(delta float32)
- func (this *Server) UpdateTitle() string
- type SvrCfg
- type TimerEntity
- type TimerRecord
- type Wrap
Constants ¶
View Source
const ( CONSUL_RESP_OK = "ok" // Consul响应200 CONSUL_CHECK_PATH = "/check" // Consul心跳检测 )
View Source
const ( LAN_CIN_MAX_FRAME = 50000 // 最大输入网络帧数 LAN_COUT_MAX_FRAME = 50000 // 最大输出网络帧数 )
View Source
const ( EVT_SERVER_STARTED = -1 // 服务就绪(配置就绪 & 日志就绪 & DB就绪 & Redis就绪 & Lan就绪) EVT_SERVER_CHANGED = -2 // 服务变更(参数类型:[]interface{}{added map[string][]string, removed map[string][]string}) EVT_SERVER_PREQUIT = -3 // 服务即将退出 )
View Source
const (
SERVER_SLEEP time.Duration = 10 * time.Millisecond // 帧刷新间隔
)
View Source
const (
UPDATE_SLEEP time.Duration = 10 * time.Millisecond // 帧刷新间隔
)
Variables ¶
View Source
var ( CslClt *consulapi.Client // Consul连接 CslCfg *consulapi.Config // Consul配置 )
View Source
var ( GMsg = xevt.NewEvtMgr(true) // Msg消息中心 GRpc = xevt.NewEvtMgr(false) // Rpc消息中心 GCgi = xevt.NewEvtMgr(false) // Cgi消息中心 GEvt = xevt.NewEvtMgr(true) // Evt消息中心 )
View Source
var ( ERR_SEND_CHAN_FULL = errors.New("send chan is full") ERR_NO_ROUTE_FOUND = errors.New("no route found") ERR_RPC_TIMEOUT = errors.New("rpc call timeout") ERR_CGI_TIMEOUT = errors.New("cgi call timeout") ERR_RPC_INTERRUPTED = errors.New("rpc call has been interrupted, see log context for more details.") ERR_CGI_INTERRUPTED = errors.New("cgi call has been interrupted, see log context for more details.") )
View Source
var ( GWrap *Wrap // 服务封装器 GServer IServer // 全局服务 )
View Source
var CGIROUTEMAP map[int]*CgiRoute // cgi路由
View Source
var MSGROUTEMAP map[int]*MsgRoute // msg路由
View Source
var (
MainTID int64 = -1 // 主线程ID
)
View Source
var RPCROUTEMAP map[int]*RpcRoute // rpc路由
Functions ¶
func SendAsync ¶
func SendAsync(id int, uid int, req proto.Message, addr string, callback func(frame *xproto.RpcResp, err error), offsetAndTimeout ...int)
发送Rpc消息(异步)
id: 消息ID uid: 用户ID(负载均衡) req: 请求结构体 addr: 目标服务器 callback: 回调函数 offset: 目标协程ID偏移(基于protocol中定义) timeout: 超时时长
func SendCgi ¶
func SendCgi(id int, uid int, req *http.Request, addr string, timeout ...int) (cresp *xproto.CgiResp, err error)
发送Cgi消息(同步,否则ResponseWriter无法输出)
id: 消息ID uid: 用户ID(负载均衡) req: 请求结构体 addr: 目标服务器 timeout: 超时时长
func SendSync ¶
func SendSync(id int, uid int, req proto.Message, resp proto.Message, addr string, offsetAndTimeout ...int) error
发送Rpc消息(同步)
id: 消息ID uid: 用户ID(负载均衡) req: 请求结构体 resp: 返回结构体 addr: 目标服务器 offset: 目标协程偏移(基于protocol中定义) timeout: 超时时长
func StartLan ¶
func StartLan(lanCfg *LanCfg, handleMsg func(*xproto.MsgReq), handleRpc func(*xproto.RpcReq, *xproto.RpcResp), handleCgi func(*xproto.CgiReq, *xproto.CgiResp))
启动线路
lancfg: 线路配置 handleMsg: 消息处理函数
func WatchSignal ¶
func WatchSignal() <-chan string
Types ¶
type IServer ¶
type IServer interface {
Init() bool // 初始化
Start() // 服务启动
Update(delta float32) // 服务循环
Destroy() // 服务结束
PreQuit() // 服务即将退出
Name() string // 服务名称
InitConfig() bool // 读取配置
GetConfig() *SvrCfg // 获取配置
GetFPS() int // 获取帧率
UpdateTitle() string // 更新标题
GetTitle() string // 获取标题
RecvMsg(mreq *xproto.MsgReq) // 接收Msg消息
RecvRpc(rreq *xproto.RpcReq, rresp *xproto.RpcResp) // 接收Rpc消息
RecvCgi(creq *xproto.CgiReq, cresp *xproto.CgiResp) // 接收Cgi消息
}
服务接口
type LanCfg ¶
type LanCfg struct {
Name string // 名称
Addr string // tcp://$ip:$port
Raw string // $ip:$port
IP string // IP
Port int // 端口
GO int // 逻辑线程数
MaxRx int // 最大接收字节数(KB)
MsgProto string // msg消息协议类型,可选pb/json,默认pb
CgiProto string // cgi消息协议类型,可选pb/json,默认json
}
线路配置
type LanSvr ¶
type LanSvr struct {
*LanCfg
mangos.Socket
Clients sync.Map // 连接池(map[string][]*LanClt)
ClientID sync.Map // 连接映射(map[string]*LanClt)
SClosed bool // 是否关闭
}
线路服务
type Proc ¶
type Proc struct {
TID int64 // 线路的GoID
Num int // 线路线程总数
CIN chan xproto.IFrame // 输入队列
COUT chan xproto.IFrame // 输出队列
Loop bool // 循环标识
Pause bool // 暂停标识
Resp sync.Map // map[int64]chan *xproto.RpcReq/*xproto.CgiFrame
}
业务处理器
type Route ¶
type Route struct {
ID int // 路由ID
Name string // 路由名称
GoL int // 协程ID(左)
GoR int //协程ID(右)
RW bool // 可读可写(默认true)
Log int // 日志层级(参考xlog的LogLevel)
Dst []string // 目标
}
路由信息
type Server ¶
type Server struct {
xobj.OBJECT
REAL IServer
Config *SvrCfg // 配置信息
FPS int // 应用帧率
Title string // 应用标题
}
服务对象
func (*Server) UpdateTitle ¶
type SvrCfg ¶
type SvrCfg struct {
Raw xconfig.Configer
Env string // 环境标识: 测试,内测,生产
LanCfg *LanCfg // 线路配置
LinkServer string // 需要连接的内部服务器
ConsulAddr string // Consul中心地址
ConsulHttp string // Consul检测地址
ConsulTimeout string // Consul超时时间
ConsulInterval string // Consul访问间隔
ConsulDeregister string // Consul延迟注销
}
服务配置
type TimerEntity ¶
type TimerEntity struct {
ID int // 定时器ID
Func func() // 定时器回调
Time int // 定时时间
RawTime int // 初始时间
Repeat bool // 循环调用
Crash bool // 是否崩溃
RW bool // 是否读写
Tag interface{} // 日志标签
Log int // 日志层级
}
定时器对象
func SetInterval ¶
func SetInterval(fun func(), interval float32, tid ...int64) *TimerEntity
设置间歇调用(务必在逻辑线程中调用或指定线程ID)
fun: 回调函数 interval: 间歇时间(秒) tid: 线程ID
func SetTimeout ¶
func SetTimeout(fun func(), timeout float32, tid ...int64) *TimerEntity
设置超时调用(务必在逻辑线程中调用或指定线程ID)
fun: 回调函数 timeout: 超时时间(秒) tid: 线程ID
type TimerRecord ¶
定时器句柄
Click to show internal directories.
Click to hide internal directories.