core

package module
v0.2.57 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2024 License: LGPL-2.1 Imports: 19 Imported by: 44

README

core

Golaxy分布式服务开发框架的内核,本包仅提供线程模型与代码组织框架,不提供例如服务发现(Service Discovery)、消息队列与事件驱动架构(MQ and Broker)、分布式锁(Distributed Sync)等分布式服务特性,这些特性将会以官方插件形式提供。

本包提供的主要特性如下:

  • 实体组件框架(Entity Component)
  • Actor线程模型(Actor Model)
  • 运行时环境(Runtime and Context)
  • 服务环境(Service and Context)
  • 实体原型系统(Entity Prototype)
  • 插件系统(Plugin Support)
  • 本地事件系统(Local Event)
  • 异步调用方案(Async/Await)

Install

go get -u git.golaxy.org/core

Documentation

Overview

Package core Golaxy分布式服务开发框架的内核,主要提供Actor线程模型(Actor Model)和EC组件框架(Entity-Component)。

  • 使用EC组件框架(Entity-Component)组织代码结构。
  • 并发模式基于Actor编程模型,实体(Entity)就是Actor,其中组件(Component)用于实现状态(state)与行为(behavior),运行时(Runtime)中的任务处理流水线就是邮箱(mailbox), 实体的Id就是邮箱地址(mailbox address),服务上下文(Service context)提供的全局实体管理功能,可以用于投递邮件(mail)给Actor。不同于传统Actor编程模型的地方是 多个Actor可以使用同个邮箱,也就是多个实体可以加入同个运行时,在同个线程中运行。
  • 一些分布式系统常用的依赖项,例如服务发现(Service Discovery)、消息队列与事件驱动架构(MQ and Broker)、分布式锁(Distributed Sync)等分布式服务特性,这些特性将会以官方插件形式提供。

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrCore     = exception.ErrCore                  // 内核错误
	ErrPanicked = exception.ErrPanicked              // panic错误
	ErrArgs     = exception.ErrArgs                  // 参数错误
	ErrRuntime  = fmt.Errorf("%w: runtime", ErrCore) // 运行时错误
	ErrService  = fmt.Errorf("%w: service", ErrCore) // 服务错误
)
View Source
var (
	ErrProcessQueueClosed = fmt.Errorf("%w: process queue is closed", ErrRuntime) // 任务处理流水线关闭
	ErrProcessQueueFull   = fmt.Errorf("%w: process queue is full", ErrRuntime)   // 任务处理流水线已满
)
View Source
var (
	ErrAllFailures = fmt.Errorf("%w: all of async result failures", ErrCore)
)
View Source
var With _Option

Functions

func Async

Async 异步执行代码,有返回值

func AsyncVoid

AsyncVoid 异步执行代码,无返回值

func Go

Go 使用新线程执行代码,有返回值

func GoVoid

GoVoid 使用新线程执行代码,无返回值

func ReadChan

func ReadChan[T any](ctx context.Context, ch <-chan T) runtime.AsyncRet

ReadChan 读取channel

func TimeAfter

func TimeAfter(ctx context.Context, dur time.Duration) runtime.AsyncRet

TimeAfter 定时器,指定时长

func TimeAt

func TimeAt(ctx context.Context, at time.Time) runtime.AsyncRet

TimeAt 定时器,指定时间点

func TimeTick

func TimeTick(ctx context.Context, dur time.Duration) runtime.AsyncRet

TimeTick 心跳器

func UnsafeRuntime deprecated

func UnsafeRuntime(runtime Runtime) _UnsafeRuntime

Deprecated: UnsafeRuntime 访问运行时内部方法

func UnsafeService deprecated

func UnsafeService(service Service) _UnsafeService

Deprecated: UnsafeService 访问服务内部方法

Types

type AwaitDirector

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

AwaitDirector 异步等待分发器

func Await

func Await(ctxProvider runtime.CurrentContextProvider, asyncRet ...runtime.AsyncRet) AwaitDirector

Await 异步等待结果返回

func (AwaitDirector) All

func (ad AwaitDirector) All(fun generic.ActionVar2[runtime.Context, []runtime.Ret, any], va ...any)

All 异步等待所有结果返回

func (AwaitDirector) Any

Any 异步等待任意一个结果返回

func (AwaitDirector) AnyOK

func (ad AwaitDirector) AnyOK(fun generic.ActionVar2[runtime.Context, runtime.Ret, any], va ...any)

AnyOK 异步等待任意一个结果成功返回

func (AwaitDirector) Pipe

Pipe 异步等待管道返回

type CustomGC

type CustomGC = generic.DelegateAction1[Runtime] // 自定义GC函数

type EntityCreator

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

EntityCreator 实体构建器

func CreateEntity

func CreateEntity(ctxProvider runtime.CurrentContextProvider) EntityCreator

CreateEntity 创建实体

func (EntityCreator) AwakeOnFirstAccess added in v0.2.38

func (c EntityCreator) AwakeOnFirstAccess(b bool) EntityCreator

AwakeOnFirstAccess 设置开启组件被首次访问时,检测并调用Awake()

func (EntityCreator) ComponentCtor added in v0.2.38

func (c EntityCreator) ComponentCtor(ctors pt.ComponentCtor) EntityCreator

ComponentCtor 设置组件构造函数

func (EntityCreator) CompositeFace added in v0.2.38

func (c EntityCreator) CompositeFace(face iface.Face[ec.Entity]) EntityCreator

CompositeFace 设置扩展者,在扩展实体自身能力时使用

func (EntityCreator) EntityCtor added in v0.2.38

func (c EntityCreator) EntityCtor(ctors pt.EntityCtor) EntityCreator

EntityCtor 设置实体构造函数

func (EntityCreator) Meta added in v0.2.38

func (c EntityCreator) Meta(m ec.Meta) EntityCreator

Meta 设置Meta信息

func (EntityCreator) ParentId added in v0.2.38

func (c EntityCreator) ParentId(id uid.Id) EntityCreator

ParentId 设置父实体Id

func (EntityCreator) PersistId added in v0.2.38

func (c EntityCreator) PersistId(id uid.Id) EntityCreator

PersistId 设置实体持久化Id

func (EntityCreator) Prototype added in v0.2.38

func (c EntityCreator) Prototype(prototype string) EntityCreator

Prototype 设置实体原型名称

func (EntityCreator) Scope added in v0.2.38

func (c EntityCreator) Scope(scope ec.Scope) EntityCreator

Scope 设置实体的可访问作用域

func (EntityCreator) Spawn

func (c EntityCreator) Spawn() (ec.Entity, error)

Spawn 创建实体

type EntityPTCreator added in v0.2.39

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

EntityPTCreator 实体原型构建器

func CreateEntityPT added in v0.2.39

func CreateEntityPT(ctx service.Context) EntityPTCreator

CreateEntityPT 创建实体原型

func (EntityPTCreator) AddComponent added in v0.2.39

func (c EntityPTCreator) AddComponent(comp any, alias ...string) EntityPTCreator

AddComponent 添加组件

func (EntityPTCreator) Declare added in v0.2.39

func (c EntityPTCreator) Declare()

Declare 声明实体原型

func (EntityPTCreator) Prototype added in v0.2.40

func (c EntityPTCreator) Prototype(prototype string) EntityPTCreator

Prototype 设置实体原型名称

type LifecycleComponentAwake

type LifecycleComponentAwake interface {
	Awake()
}

LifecycleComponentAwake 组件的生命周期进入唤醒(awake)时的回调,组件实现此接口即可使用

type LifecycleComponentDispose

type LifecycleComponentDispose interface {
	Dispose()
}

LifecycleComponentDispose 组件的生命周期进入死亡(death)时的回调,组件实现此接口即可使用

type LifecycleComponentLateUpdate

type LifecycleComponentLateUpdate = eventLateUpdate

LifecycleComponentLateUpdate 如果开启运行时的帧更新特性,那么组件状态为活跃(living)时,将会收到这个帧迟滞更新(late update)回调,组件实现此接口即可使用

type LifecycleComponentShut

type LifecycleComponentShut interface {
	Shut()
}

LifecycleComponentShut 组件的生命周期进入结束(shut)时的回调,组件实现此接口即可使用

type LifecycleComponentStart

type LifecycleComponentStart interface {
	Start()
}

LifecycleComponentStart 组件的生命周期进入开始(start)时的回调,组件实现此接口即可使用

type LifecycleComponentUpdate

type LifecycleComponentUpdate = eventUpdate

LifecycleComponentUpdate 如果开启运行时的帧更新特性,那么组件状态为活跃(living)时,将会收到这个帧更新(update)回调,组件实现此接口即可使用

type LifecycleEntityAwake

type LifecycleEntityAwake interface {
	Awake()
}

LifecycleEntityAwake 实体的生命周期进入唤醒(awake)时的回调,实体实现此接口即可使用

type LifecycleEntityDispose

type LifecycleEntityDispose interface {
	Dispose()
}

LifecycleEntityDispose 实体的生命周期进入死亡(death)时的回调,实体实现此接口即可使用

type LifecycleEntityLateUpdate

type LifecycleEntityLateUpdate = eventLateUpdate

LifecycleEntityLateUpdate 如果开启运行时的帧更新特性,那么实体状态为活跃(living)时,将会收到这个帧迟滞更新(late update)回调,实体实现此接口即可使用

type LifecycleEntityShut

type LifecycleEntityShut interface {
	Shut()
}

LifecycleEntityShut 实体的生命周期进入结束(shut)时的回调,实体实现此接口即可使用

type LifecycleEntityStart

type LifecycleEntityStart interface {
	Start()
}

LifecycleEntityStart 实体的生命周期进入开始(start)时的回调,实体实现此接口即可使用

type LifecycleEntityUpdate

type LifecycleEntityUpdate = eventUpdate

LifecycleEntityUpdate 如果开启运行时的帧更新特性,那么实体状态为活跃(living)时,将会收到这个帧更新(update)回调,实体实现此接口即可使用

type LifecycleRuntimePluginInit

type LifecycleRuntimePluginInit interface {
	InitRP(ctx runtime.Context)
}

LifecycleRuntimePluginInit 运行时上的插件初始化回调,插件实现此接口即可使用

type LifecycleRuntimePluginShut

type LifecycleRuntimePluginShut interface {
	ShutRP(ctx runtime.Context)
}

LifecycleRuntimePluginShut 运行时上的插件结束回调,插件实现此接口即可使用

type LifecycleServicePluginInit

type LifecycleServicePluginInit interface {
	InitSP(ctx service.Context)
}

LifecycleServicePluginInit 服务上的插件初始化回调,插件实现此接口即可使用

type LifecycleServicePluginShut

type LifecycleServicePluginShut interface {
	ShutSP(ctx service.Context)
}

LifecycleServicePluginShut 服务上的插件结束回调,插件实现此接口即可使用

type Running

type Running interface {
	// Run 运行,返回的channel用于线程同步,可以阻塞等待至运行结束
	Run() <-chan struct{}
	// Terminate 停止
	Terminate()
}

Running 运行接口

type Runtime

type Runtime interface {
	concurrent.CurrentContextProvider
	concurrent.ConcurrentContextProvider
	concurrent.Callee
	reinterpret.CompositeProvider
	Running
	// contains filtered or unexported methods
}

Runtime 运行时接口

func NewRuntime

func NewRuntime(ctx runtime.Context, settings ...option.Setting[RuntimeOptions]) Runtime

NewRuntime 创建运行时

func UnsafeNewRuntime deprecated

func UnsafeNewRuntime(ctx runtime.Context, options RuntimeOptions) Runtime

Deprecated: UnsafeNewRuntime 内部创建运行时

type RuntimeBehavior

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

RuntimeBehavior 运行时行为,在需要扩展运行时能力时,匿名嵌入至运行时结构体中

func (*RuntimeBehavior) GetCompositeFaceCache added in v0.2.34

func (rt *RuntimeBehavior) GetCompositeFaceCache() iface.Cache

GetCompositeFaceCache 支持重新解释类型

func (*RuntimeBehavior) GetConcurrentContext

func (rt *RuntimeBehavior) GetConcurrentContext() iface.Cache

GetConcurrentContext 获取多线程安全的上下文

func (*RuntimeBehavior) GetCurrentContext

func (rt *RuntimeBehavior) GetCurrentContext() iface.Cache

GetCurrentContext 获取当前上下文

func (*RuntimeBehavior) OnComponentDestroySelf

func (rt *RuntimeBehavior) OnComponentDestroySelf(comp ec.Component)

OnComponentDestroySelf 事件处理器:组件销毁自身

func (*RuntimeBehavior) OnEntityDestroySelf

func (rt *RuntimeBehavior) OnEntityDestroySelf(entity ec.Entity)

OnEntityDestroySelf 事件处理器:实体销毁自身

func (*RuntimeBehavior) OnEntityMgrAddEntity

func (rt *RuntimeBehavior) OnEntityMgrAddEntity(entityMgr runtime.EntityMgr, entity ec.Entity)

OnEntityMgrAddEntity 事件处理器:实体管理器添加实体

func (*RuntimeBehavior) OnEntityMgrEntityAddComponents

func (rt *RuntimeBehavior) OnEntityMgrEntityAddComponents(entityMgr runtime.EntityMgr, entity ec.Entity, components []ec.Component)

OnEntityMgrEntityAddComponents 事件处理器:实体管理器中的实体添加组件

func (*RuntimeBehavior) OnEntityMgrEntityFirstAccessComponent

func (rt *RuntimeBehavior) OnEntityMgrEntityFirstAccessComponent(entityMgr runtime.EntityMgr, entity ec.Entity, component ec.Component)

OnEntityMgrEntityFirstAccessComponent 事件处理器:实体管理器中的实体首次访问组件

func (*RuntimeBehavior) OnEntityMgrEntityRemoveComponent

func (rt *RuntimeBehavior) OnEntityMgrEntityRemoveComponent(entityMgr runtime.EntityMgr, entity ec.Entity, component ec.Component)

OnEntityMgrEntityRemoveComponent 事件处理器:实体管理器中的实体删除组件

func (*RuntimeBehavior) OnEntityMgrRemoveEntity

func (rt *RuntimeBehavior) OnEntityMgrRemoveEntity(entityMgr runtime.EntityMgr, entity ec.Entity)

OnEntityMgrRemoveEntity 事件处理器:实体管理器删除实体

func (*RuntimeBehavior) PushCall

func (rt *RuntimeBehavior) PushCall(fun generic.FuncVar0[any, runtime.Ret], va ...any) runtime.AsyncRet

PushCall 将调用函数压入接受者的任务处理流水线,返回AsyncRet。

func (*RuntimeBehavior) PushCallDelegate

func (rt *RuntimeBehavior) PushCallDelegate(fun generic.DelegateFuncVar0[any, runtime.Ret], va ...any) runtime.AsyncRet

PushCallDelegate 将调用委托压入接受者的任务处理流水线,返回AsyncRet。

func (*RuntimeBehavior) PushCallVoid

func (rt *RuntimeBehavior) PushCallVoid(fun generic.ActionVar0[any], va ...any) runtime.AsyncRet

PushCallVoid 将调用函数压入接受者的任务处理流水线,返回AsyncRet。

func (*RuntimeBehavior) PushCallVoidDelegate

func (rt *RuntimeBehavior) PushCallVoidDelegate(fun generic.DelegateActionVar0[any], va ...any) runtime.AsyncRet

PushCallVoidDelegate 将调用委托压入接受者的任务处理流水线,返回AsyncRet。

func (*RuntimeBehavior) Run

func (rt *RuntimeBehavior) Run() <-chan struct{}

Run 运行,返回的channel用于线程同步,可以阻塞等待至运行结束

func (*RuntimeBehavior) Terminate

func (rt *RuntimeBehavior) Terminate()

Terminate 停止

type RuntimeOptions

type RuntimeOptions struct {
	CompositeFace        iface.Face[Runtime] // 扩展者,在扩展运行时自身能力时使用
	AutoRun              bool                // 是否开启自动运行
	ProcessQueueCapacity int                 // 任务处理流水线大小
	Frame                runtime.Frame       // 帧,设置为nil表示不使用帧更新特性
	GCInterval           time.Duration       // GC间隔时长
	CustomGC             CustomGC            // 自定义GC
}

RuntimeOptions 创建运行时的所有选项

type Service

type Service interface {
	reinterpret.CompositeProvider
	Running

	// GetContext 获取服务上下文
	GetContext() service.Context
	// contains filtered or unexported methods
}

Service 服务

func NewService

func NewService(ctx service.Context, settings ...option.Setting[ServiceOptions]) Service

NewService 创建服务

func UnsafeNewService deprecated

func UnsafeNewService(ctx service.Context, options ServiceOptions) Service

Deprecated: UnsafeNewService 内部创建服务

type ServiceBehavior

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

func (*ServiceBehavior) GetCompositeFaceCache added in v0.2.34

func (serv *ServiceBehavior) GetCompositeFaceCache() iface.Cache

GetCompositeFaceCache 支持重新解释类型

func (*ServiceBehavior) GetContext

func (serv *ServiceBehavior) GetContext() service.Context

GetContext 获取服务上下文

func (*ServiceBehavior) Run

func (serv *ServiceBehavior) Run() <-chan struct{}

Run 运行,返回的channel用于线程同步,可以阻塞等待至运行结束

func (*ServiceBehavior) Terminate

func (serv *ServiceBehavior) Terminate()

Terminate 停止

type ServiceOptions

type ServiceOptions struct {
	CompositeFace iface.Face[Service] // 扩展者,在扩展服务自身功能时使用
}

ServiceOptions 创建服务的所有选项

Directories

Path Synopsis
Package define 利用泛型特性,简化代码编写。
Package define 利用泛型特性,简化代码编写。
Package ec 提供了一个EC(Entity-Component)组件框架,用于帮助开发者组织代码架构。
Package ec 提供了一个EC(Entity-Component)组件框架,用于帮助开发者组织代码架构。
Package event 高效的本地事件系统,适用于单线程环境,需要使用go:generate功能来生成代码。
Package event 高效的本地事件系统,适用于单线程环境,需要使用go:generate功能来生成代码。
eventcode
Package eventcode 使用go:generate功能,在编译前自动化生成代码
Package eventcode 使用go:generate功能,在编译前自动化生成代码
internal
Package plugin 插件,用于开发一些需要使用单例模式设计的功能,例如服务发现、消息队列与日志系统等。
Package plugin 插件,用于开发一些需要使用单例模式设计的功能,例如服务发现、消息队列与日志系统等。
Package pt 实体与组件原型,用于创建实例。
Package pt 实体与组件原型,用于创建实例。
Package runtime 运行时环境提供的一些接口与函数。
Package runtime 运行时环境提供的一些接口与函数。
Package service 运行时环境提供的一些接口与函数。
Package service 运行时环境提供的一些接口与函数。
Package util 一些工具类与函数。
Package util 一些工具类与函数。
container
Package container 提供常用的容器数据结构。
Package container 提供常用的容器数据结构。
uid

Jump to

Keyboard shortcuts

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