Documentation
¶
Overview ¶
Package service 定义服务级上下文。
Package service 表示应用的全局作用域,也是多个 runtime 的父上下文。
服务上下文负责:
- 持有父 context、等待组与终止状态;
- 提供随服务关闭并汇合后台任务的 AsyncScope;
- 管理实体原型库与组件原型库;
- 提供全局实体索引,以及按实体 ID Submit 或 Post 到所属 Runtime;
- 管理随服务同步启停的 service add-in,并派发服务运行事件。
通常先用 NewContext 创建上下文,再交给 core.NewService 绑定和运行。 service add-in 只能在启动前安装或卸载;管理器会在 RunningEvent_Starting 时冻结, 随后按注册顺序初始化插件,并在停服时按相反顺序关闭。原型声明、插件安装等准备 逻辑可在创建上下文后完成,也可放在 service.RunningEvent_Birth 中。
Index ¶
- Variables
- func UnsafeAddInManager(mgr AddInManager) _UnsafeAddInManagerdeprecated
- func UnsafeAddInStatus(status AddInStatus) _UnsafeAddInStatusdeprecated
- func UnsafeContext(ctx Context) _UnsafeContextdeprecated
- type AddInManager
- type AddInStatus
- type Caller
- type Context
- type ContextBehavior
- func (ctx *ContextBehavior) AddInManager() extension.AddInManager
- func (ctx *ContextBehavior) EntityLib() pt.EntityLib
- func (ctx *ContextBehavior) EntityManager() EntityManager
- func (ctx *ContextBehavior) Id() uid.Id
- func (ctx *ContextBehavior) InstanceFaceCache() iface.Cache
- func (ctx *ContextBehavior) Name() string
- func (ctx *ContextBehavior) Post(entityID uid.Id, fun generic.ActionVar1[ec.Entity, any], args ...any) error
- func (ctx *ContextBehavior) PostDelegate(entityID uid.Id, fun generic.DelegateVoidVar1[ec.Entity, any], args ...any) error
- func (ctx *ContextBehavior) Reflected() reflect.Value
- func (ctx *ContextBehavior) String() string
- func (ctx *ContextBehavior) Submit(entityID uid.Id, fun generic.FuncVar1[ec.Entity, any, async.Result], ...) async.Future
- func (ctx *ContextBehavior) SubmitDelegate(entityID uid.Id, fun generic.DelegateVar1[ec.Entity, any, async.Result], ...) async.Future
- func (ctx *ContextBehavior) SubmitDelegateVoid(entityID uid.Id, fun generic.DelegateVoidVar1[ec.Entity, any], args ...any) async.Future
- func (ctx *ContextBehavior) SubmitVoid(entityID uid.Id, fun generic.ActionVar1[ec.Entity, any], args ...any) async.Future
- type ContextOptions
- type EntityManager
- type RunningEvent
- type RunningEventCB
Constants ¶
This section is empty.
Variables ¶
var ( ErrContext = fmt.Errorf("%w: service-context", exception.ErrCore) // 服务上下文错误。 ErrEntityManager = fmt.Errorf("%w: entity-manager", ErrContext) // 全局实体管理器错误。 )
var With _ContextOption
With 提供 Service 上下文选项构造器。
Functions ¶
func UnsafeAddInManager
deprecated
added in
v0.4.27
func UnsafeAddInManager(mgr AddInManager) _UnsafeAddInManager
Deprecated: UnsafeAddInManager 暴露服务插件管理器的生命周期操作,仅供 core 使用。
func UnsafeAddInStatus
deprecated
added in
v0.4.27
func UnsafeAddInStatus(status AddInStatus) _UnsafeAddInStatus
Deprecated: UnsafeAddInStatus 暴露服务插件状态的生命周期操作,仅供 core 使用。
func UnsafeContext
deprecated
func UnsafeContext(ctx Context) _UnsafeContext
Deprecated: UnsafeContext 暴露服务上下文内部能力,仅供框架集成代码使用。
Types ¶
type AddInManager ¶ added in v0.4.27
type AddInManager interface {
extension.AddInManager
// contains filtered or unexported methods
}
AddInManager 管理与服务生命周期绑定的插件。
插件只能在服务启动前安装或卸载。服务启动时管理器会被冻结,此后 Install 和 Uninstall 均会 panic。插件按安装顺序启动,并在服务停止时按相反顺序关闭。
管理器通过不可变快照支持多个 goroutine 并发安装、卸载和查询插件。
func NewAddInManager ¶ added in v0.4.27
func NewAddInManager() AddInManager
NewAddInManager 创建一个未冻结的空服务插件管理器。
type AddInStatus ¶ added in v0.4.27
type AddInStatus interface {
extension.AddInStatus
// contains filtered or unexported methods
}
AddInStatus 描述与服务生命周期绑定的插件状态。
type Caller ¶
type Caller interface {
Submit(entityID uid.Id, fun generic.FuncVar1[ec.Entity, any, async.Result], args ...any) async.Future
SubmitDelegate(entityID uid.Id, fun generic.DelegateVar1[ec.Entity, any, async.Result], args ...any) async.Future
SubmitVoid(entityID uid.Id, fun generic.ActionVar1[ec.Entity, any], args ...any) async.Future
SubmitDelegateVoid(entityID uid.Id, fun generic.DelegateVoidVar1[ec.Entity, any], args ...any) async.Future
Post(entityID uid.Id, fun generic.ActionVar1[ec.Entity, any], args ...any) error
PostDelegate(entityID uid.Id, fun generic.DelegateVoidVar1[ec.Entity, any], args ...any) error
}
Caller 通过全局 Entity ID 把任务投递到实体所属 Runtime。
type Context ¶
type Context interface {
corectx.Context
reinterpret.InstanceProvider
extension.AddInProvider
pt.EntityPTProvider
Caller
fmt.Stringer
// Name 返回服务名称。
Name() string
// Id 返回服务的持久化 ID。
Id() uid.Id
// Reflected 返回实际服务上下文实例的反射值。
Reflected() reflect.Value
// EntityManager 返回并发安全的全局实体索引。
EntityManager() EntityManager
// contains filtered or unexported methods
}
Context 表示应用级共享作用域,并为多个 runtime 提供父上下文和全局资源。
func Current ¶
func Current(provider corectx.ConcurrentContextProvider) Context
Current 返回 provider 所属运行时的服务上下文。 provider 为 nil 时会 panic;provider 未关联运行时上下文时返回 nil。
func NewContext ¶
func NewContext(settings ...option.Setting[ContextOptions]) Context
NewContext 创建服务上下文。 未提供父上下文、持久化 ID、原型库或插件管理器时会自动创建默认值。
func UnsafeNewContext
deprecated
func UnsafeNewContext(options ContextOptions) Context
Deprecated: UnsafeNewContext 仅供框架内部使用,请改用 NewContext。
type ContextBehavior ¶
type ContextBehavior struct {
corectx.ContextBehavior
// contains filtered or unexported fields
}
ContextBehavior 提供 Context 的默认实现。 扩展服务上下文时应匿名嵌入该类型,并通过 InstanceFace 传入扩展实例。
func (*ContextBehavior) AddInManager ¶ added in v0.4.25
func (ctx *ContextBehavior) AddInManager() extension.AddInManager
AddInManager 返回服务插件管理器的公共接口。
func (*ContextBehavior) EntityLib ¶ added in v0.4.25
func (ctx *ContextBehavior) EntityLib() pt.EntityLib
EntityLib 返回服务使用的实体原型库。
func (*ContextBehavior) EntityManager ¶ added in v0.4.25
func (ctx *ContextBehavior) EntityManager() EntityManager
EntityManager 返回并发安全的全局实体索引。
func (*ContextBehavior) Id ¶ added in v0.4.25
func (ctx *ContextBehavior) Id() uid.Id
Id 返回服务的持久化 ID。
func (*ContextBehavior) InstanceFaceCache ¶ added in v0.4.25
func (ctx *ContextBehavior) InstanceFaceCache() iface.Cache
InstanceFaceCache 返回上下文实例的接口缓存,用于 reinterpret.Cast。
func (*ContextBehavior) Name ¶ added in v0.4.25
func (ctx *ContextBehavior) Name() string
Name 返回服务名称。
func (*ContextBehavior) Post ¶ added in v0.4.27
func (ctx *ContextBehavior) Post(entityID uid.Id, fun generic.ActionVar1[ec.Entity, any], args ...any) error
Post 只报告实体查询和入队错误;任务执行前实体已经失活时会静默丢弃。
func (*ContextBehavior) PostDelegate ¶ added in v0.4.27
func (ctx *ContextBehavior) PostDelegate(entityID uid.Id, fun generic.DelegateVoidVar1[ec.Entity, any], args ...any) error
PostDelegate 是 Post 的 DelegateVoid 版本。
func (*ContextBehavior) Reflected ¶ added in v0.4.25
func (ctx *ContextBehavior) Reflected() reflect.Value
Reflected 返回实际服务上下文实例的反射值。
func (*ContextBehavior) String ¶
func (ctx *ContextBehavior) String() string
String 实现 fmt.Stringer,返回包含服务 ID 和名称的 JSON 文本。
func (*ContextBehavior) SubmitDelegate ¶ added in v0.4.27
func (*ContextBehavior) SubmitDelegateVoid ¶ added in v0.4.27
func (*ContextBehavior) SubmitVoid ¶ added in v0.4.27
type ContextOptions ¶
type ContextOptions struct {
InstanceFace iface.Face[Context] // 自定义上下文实例及其接口缓存。
Context context.Context // 父上下文;nil 时使用 context.Background。
AutoRecover bool // 回调发生 panic 时是否自动恢复。
ReportError chan error // 自动恢复后接收 panic 错误的通道。
Name string // 服务名称。
PersistId uid.Id // 服务持久化 ID;为 Nil 时自动生成。
EntityLib pt.EntityLib // 实体原型库;nil 时创建独立实体库。
AddInManager AddInManager // 服务插件管理器;nil 时创建默认管理器。
RunningEventCB RunningEventCB // 服务运行事件回调。
}
ContextOptions 定义创建服务上下文时使用的选项。
type EntityManager ¶ added in v0.3.66
type EntityManager interface {
// GetEntity 按 ID 查询全局实体。
GetEntity(id uid.Id) (ec.ConcurrentEntity, bool)
// GetOrAddEntity 原子地查询或注册全局实体,并报告该 ID 是否已经存在。
GetOrAddEntity(entity ec.ConcurrentEntity) (ec.ConcurrentEntity, bool, error)
// RemoveEntity 按 ID 注销全局实体;实体不存在时不执行任何操作。
RemoveEntity(id uid.Id)
}
EntityManager 提供跨运行时可并发访问的全局实体索引。 只有 Scope_Global 实体会由运行时注册到该索引。 注册与注销事件在调用方 goroutine 中同步派发,因此不同 Runtime 可能并发触发回调。
type RunningEvent ¶ added in v0.4.25
type RunningEvent int32
RunningEvent 标识服务生命周期及服务级资源变化事件。
const ( RunningEvent_Birth RunningEvent = iota // 服务对象创建完成。 RunningEvent_Starting // 服务开始启动,插件管理器在回调前冻结并激活插件。 RunningEvent_Started // 服务启动完成。 RunningEvent_Heartbeat // 服务运行期间的每秒心跳。 RunningEvent_Terminating // 服务开始停止。 RunningEvent_Terminated // 服务等待组清空、插件停用完成,主体生命周期结束。 RunningEvent_EntityPTDeclared // 实体原型已声明。 RunningEvent_ComponentPTDeclared // 组件原型已声明。 RunningEvent_EntityRegistered // 全局实体已注册。 RunningEvent_EntityDeregistered // 全局实体已注销。 )
func (RunningEvent) String ¶ added in v0.4.25
func (i RunningEvent) String() string
type RunningEventCB ¶ added in v0.4.25
type RunningEventCB = generic.ActionVar2[Context, RunningEvent, any] // 服务运行事件回调。