Documentation
¶
Overview ¶
Package bigfish 提供所有框架功能API给上层业务使用
Index ¶
- Constants
- func AliasService(svc SvcID, name string)
- func Cache() *sync.Map
- func CloseService(svc SvcID)
- func InvalidService(svc SvcID) bool
- func Request(srcSvc SvcID, dstSvc SvcID, msgID int, msgData interface{}, ...)
- func Send(dstSvc SvcID, msgID int, msgData interface{})
- func SetInstance(inst IController)
- func SetRepeatTimer(svc SvcID, duration time.Duration, callback TimerCallback)
- func SetTimer(svc SvcID, duration time.Duration, callback TimerCallback)
- func SetTimerAt(svc SvcID, timing time.Time, callback TimerCallback)
- type ICmdArgs
- type IController
- type Service
- type SvcCallback
- type SvcID
- type TimerCallback
Constants ¶
View Source
const ( DevelopmentEnv = 1 // 开发环境 ProductionEnv = 2 // 线上生产环境 )
进程运行环境
Variables ¶
This section is empty.
Functions ¶
func AliasService ¶
AliasService 给服务起别名,该名字必须唯一 重复命名会更新该名字对应的服务ID
func Request ¶
func Request(srcSvc SvcID, dstSvc SvcID, msgID int, msgData interface{}, callback SvcCallback)
Request 发送请求包, srcSvc 为请求方自身,dstSvc 为目标服务。请求类消息,需回应。 callback 为异步调用,参数 reqMsg 为请求的 msgData。注意上下文数据变更,不要使用 UpValue 、闭包等。
func SetRepeatTimer ¶
func SetRepeatTimer(svc SvcID, duration time.Duration, callback TimerCallback)
SetRepeatTimer 设置定时器,每隔一段时间触发
func SetTimer ¶
func SetTimer(svc SvcID, duration time.Duration, callback TimerCallback)
SetTimer 设置定时器
func SetTimerAt ¶
func SetTimerAt(svc SvcID, timing time.Time, callback TimerCallback)
SetTimerAt 设置定时器,定点触发
Types ¶
type ICmdArgs ¶
type ICmdArgs interface {
// CfgFile 配置文件
CfgFile() string
// AppName 应用程序名称
AppName() string
// ServerID 服务器ID
ServerID() uint16
// Daemon 是否以后台进程运行
Daemon() bool
// Env 运行环境(可自定义)
Env() int
// LogDir 日志文件路径
LogDir() string
// ServiceDir 服务动态库文件路径
ServiceDir() string
// AppDir 进程动态库文件路径
AppDir() string
// ListenAddress 网络监听地址
ListenAddress() string
}
ICmdArgs 进程命令行参数
type IController ¶
type IController interface {
// CmdArgs 进程命令行参数
CmdArgs() ICmdArgs
// NewService 创建服务
// name 服务的动态库文件名
// ticker 服务Update方法执行频率,最低 1 毫秒。为 0 则无帧刷新,不执行Update方法
// initParams 初始化参数(自定义)
// 返回服务ID
NewService(name string, ticker time.Duration, initParams interface{}) SvcID
// AliasService 给服务起别名,该名字必须唯一
// 重复命名会更新该名字对应的服务ID
AliasService(svc SvcID, name string)
// ServiceID 根据别名获取服务ID
ServiceID(alias string) SvcID
// InvalidService 服务是否有效
InvalidService(svc SvcID) bool
// Send 发送消息包, dstSvc 为目标服务。通知类消息,无回应。
Send(dstSvc SvcID, msgID int, msgData interface{})
// Request 发送请求包, srcSvc 为请求方自身,dstSvc 为目标服务。请求类消息,需回应。
// callback 为异步调用,参数 reqMsg 为请求的 msgData。注意上下文数据变更,不要使用 UpValue 、闭包等。
Request(srcSvc SvcID, dstSvc SvcID, msgID int, msgData interface{}, callback SvcCallback)
// CloseService 关闭微服务
CloseService(svc SvcID)
// SetTimerAt 设置定时器,定点触发
SetTimerAt(svc SvcID, timing time.Time, callback TimerCallback)
// SetRepeatTimer 设置定时器,每隔一段时间触发
SetRepeatTimer(svc SvcID, duration time.Duration, callback TimerCallback)
// Cache 获取全局数据对象,业务层自由存储使用
Cache() *sync.Map
}
IController 框架功能集, 所有API并发安全
type Service ¶
type Service interface {
// Start 启动服务
Start(self SvcID)
// Update 帧刷新
Update()
// OnRecvMessage 收到其他服务发来的消息,若无需回应则返回nil
OnRecvMessage(msgID int, msgData interface{}) (respData interface{})
// Shutdown 服务关闭
Shutdown()
}
Service 服务接口 每个 service 都是一个独立的服务,在单独的 go routine 中运行。服务间采用消息通信。 微服务驱动模式
1,消息驱动 :当收到外界的消息时被唤醒,处理消息。 2,帧驱动 :按帧刷新执行 Update 方法,可设置帧率。
服务实例的编写规范
1,业务层不用考虑资源竞争加锁等问题,因为每个微服务在它的整个生命周期中任意时刻有且仅有一个线程操作它。 2,整个服务无阻塞操作,不允许业务层使用阻塞相关的代码。 3,shutdown时只能操作自己的资源(保存或释放等)。
注意事项
1,框架最大支持 65535 个服务 2,服务间消息通信零拷贝,业务层不得在消息通信后继续使用消息对象
type SvcCallback ¶
type SvcCallback func(self Service, reqMsg interface{}, respMsg interface{})
SvcCallback 服务请求回调, 异步调用 参数 self 为请求者对象, reqMsg 为请求的 msgData,respMsg 为回应消息 异步操作,注意上下文数据变更,不要使用 UpValue 、闭包等
type SvcID ¶
type SvcID uint16
SvcID 服务标识类型
const InvalidSvcID SvcID = 0 // 无效服务
func NewService ¶
NewService 创建服务
name 服务的动态库文件名 ticker 服务Update方法执行频率,最低 1 毫秒。为 0 则无帧刷新,不执行Update方法 initParams 初始化参数(自定义)
返回服务ID
type TimerCallback ¶
type TimerCallback func(self Service)
TimerCallback 定时器回调 异步操作,注意上下文数据变更,不要使用 UpValue 、闭包等
Click to show internal directories.
Click to hide internal directories.