core

package module
v0.3.44 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2024 License: LGPL-2.1 Imports: 21 Imported by: 58

README

CORE

English | 简体中文

Introduction

The Golaxy Distributed Service Development Framework aims to provide a comprehensive server-side solution for real-time communication applications. Based on the EC system and Actor thread model, the framework is designed to be simple and easy to use, making it particularly suitable for developing games and remote control systems.

This project is the core part of the framework, with the main features including:

  • Entity Component Framework (Entity Component): Provides flexible entity and component management, supporting the creation and maintenance of complex objects.
  • Entity Prototype System (Entity Prototype): Supports the definition and reuse of entity prototypes, simplifying the creation process of entities.
  • Actor Thread Model (Actor Model): Based on the Actor model's thread processing mechanism, each Actor runs in an independent computing unit, achieving parallel task processing and enhancing the system's concurrency performance and stability.
  • Runtime Environment (Runtime and Context): Implements an independent runtime thread environment for Actors, providing mechanisms for entity management and communication calls.
  • Service Environment (Service and Context): Supports the startup, shutdown, and management of services, offering global entity management and communication call mechanisms.
  • Plugin System (Plugin Support): Provides mechanisms for extending framework functions, supporting the implementation of new features in the runtime or service environments.
  • Local Event System (Local Event): Based on a code generator, it provides an efficient local event mechanism within the independent runtime thread environment of Actors.
  • Asynchronous Call Scheme (Async/Await): Supports asynchronous operations, simplifying the writing of asynchronous code, and enhancing the system's responsiveness.

Directory

Directory Description
/ Main implementation of service and runtime related functionalities.
/define Supports the definition of plugins or components using generics, simplifying code writing.
/ec Entity Component Framework.
/event Local Event System.
/event/eventc Local Event Code Generator.
/plugin Plugin System.
/pt Entity Prototype System.
/runtime Runtime Context.
/service Service Context.
/utils Various utility classes and functions.

Examples

For more details, see: Examples

Installation

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

func GoVoid(ctx context.Context, fun generic.ActionVar1[context.Context, any], args ...any) async.AsyncRet

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

func ReadChan

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

ReadChan 读取channel

func TimeAfter

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

TimeAfter 定时器,指定时长

func TimeAt

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

TimeAt 定时器,指定时间点

func TimeTick

func TimeTick(ctx context.Context, dur time.Duration) async.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(provider ictx.CurrentContextProvider, asyncRet ...async.AsyncRet) AwaitDirector

Await 异步等待结果返回

func (AwaitDirector) All

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

All 异步等待所有结果返回

func (AwaitDirector) Any

func (ad AwaitDirector) Any(fun generic.ActionVar2[runtime.Context, async.Ret, any], args ...any)

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

func (AwaitDirector) AnyOK

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

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

func (AwaitDirector) Pipe

func (ad AwaitDirector) Pipe(ctx context.Context, fun generic.ActionVar2[runtime.Context, async.Ret, any], args ...any)

Pipe 异步等待管道返回

type CustomGC

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

type EntityCreator

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

EntityCreator 实体构建器

func CreateEntity

func CreateEntity(provider ictx.CurrentContextProvider, prototype string) EntityCreator

CreateEntity 创建实体

func (EntityCreator) AwakeOnFirstAccess added in v0.2.38

func (c EntityCreator) AwakeOnFirstAccess(b bool) EntityCreator

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

func (EntityCreator) Instance added in v0.3.34

func (c EntityCreator) Instance(instance ec.Entity) EntityCreator

Instance 设置实例,用于扩展实体能力

func (EntityCreator) InstanceFace added in v0.3.34

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

InstanceFace 设置实例,用于扩展实体能力

func (EntityCreator) Meta added in v0.2.38

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) 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(svcCtx service.Context, prototype string) EntityPTCreator

CreateEntityPT 创建实体原型

func (EntityPTCreator) AddComponent added in v0.2.39

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

AddComponent 添加组件

func (EntityPTCreator) AwakeOnFirstAccess added in v0.2.80

func (c EntityPTCreator) AwakeOnFirstAccess(b bool) EntityPTCreator

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

func (EntityPTCreator) Declare added in v0.2.39

func (c EntityPTCreator) Declare()

Declare 声明实体原型

func (EntityPTCreator) Instance added in v0.3.34

func (c EntityPTCreator) Instance(instance any) EntityPTCreator

Instance 设置实例,用于扩展实体能力

func (EntityPTCreator) Redeclare added in v0.3.42

func (c EntityPTCreator) Redeclare()

Redeclare 重新声明实体原型

func (EntityPTCreator) Scope added in v0.2.80

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

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

type LifecycleComponentAwake

type LifecycleComponentAwake interface {
	Awake()
}

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

type LifecycleComponentDispose

type LifecycleComponentDispose interface {
	Dispose()
}

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

type LifecycleComponentLateUpdate

type LifecycleComponentLateUpdate = eventLateUpdate

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

type LifecycleComponentShut

type LifecycleComponentShut interface {
	Shut()
}

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

type LifecycleComponentStart

type LifecycleComponentStart interface {
	Start()
}

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

type LifecycleComponentUpdate

type LifecycleComponentUpdate = eventUpdate

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

type LifecycleEntityAwake

type LifecycleEntityAwake interface {
	Awake()
}

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

type LifecycleEntityDispose

type LifecycleEntityDispose interface {
	Dispose()
}

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

type LifecycleEntityLateUpdate

type LifecycleEntityLateUpdate = eventLateUpdate

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

type LifecycleEntityShut

type LifecycleEntityShut interface {
	Shut()
}

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

type LifecycleEntityStart

type LifecycleEntityStart interface {
	Start()
}

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

type LifecycleEntityUpdate

type LifecycleEntityUpdate = eventUpdate

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

type LifecycleRuntimePluginInit

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

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

type LifecycleRuntimePluginShut

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

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

type LifecycleServicePluginInit

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

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

type LifecycleServicePluginShut

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

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

type Runtime

type Runtime interface {
	ictx.CurrentContextProvider
	ictx.ConcurrentContextProvider
	reinterpret.InstanceProvider
	async.Callee
	// contains filtered or unexported methods
}

Runtime 运行时接口

func NewRuntime

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

NewRuntime 创建运行时

func UnsafeNewRuntime deprecated

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

Deprecated: UnsafeNewRuntime 内部创建运行时

type RuntimeBehavior

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

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

func (*RuntimeBehavior) GetConcurrentContext

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

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

func (*RuntimeBehavior) GetCurrentContext

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

GetCurrentContext 获取当前上下文

func (*RuntimeBehavior) GetInstanceFaceCache added in v0.3.34

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

GetInstanceFaceCache 支持重新解释类型

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, async.Ret], args ...any) async.AsyncRet

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

func (*RuntimeBehavior) PushCallDelegate

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

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

func (*RuntimeBehavior) PushCallVoid

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

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

func (*RuntimeBehavior) PushCallVoidDelegate

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

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

func (*RuntimeBehavior) Run

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

Run 运行

func (*RuntimeBehavior) Terminate

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

Terminate 停止

func (*RuntimeBehavior) Terminated added in v0.3.33

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

Terminated 已停止

type RuntimeOptions

type RuntimeOptions struct {
	InstanceFace         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.InstanceProvider

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

Service 服务

func NewService

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

NewService 创建服务

func UnsafeNewService deprecated

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

Deprecated: UnsafeNewService 内部创建服务

type ServiceBehavior

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

func (*ServiceBehavior) GetContext

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

GetContext 获取服务上下文

func (*ServiceBehavior) GetInstanceFaceCache added in v0.3.34

func (svc *ServiceBehavior) GetInstanceFaceCache() iface.Cache

GetInstanceFaceCache 支持重新解释类型

func (*ServiceBehavior) Run

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

Run 运行

func (*ServiceBehavior) Terminate

func (svc *ServiceBehavior) Terminate() <-chan struct{}

Terminate 停止

func (*ServiceBehavior) Terminated added in v0.3.33

func (svc *ServiceBehavior) Terminated() <-chan struct{}

Terminated 已停止

type ServiceOptions

type ServiceOptions struct {
	InstanceFace 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功能来生成代码。
eventc
Package eventc 使用go:generate功能,在编译前自动化生成代码
Package eventc 使用go:generate功能,在编译前自动化生成代码
internal
Package plugin 插件,用于开发一些需要使用单例模式设计的功能,例如服务发现、消息队列与日志系统等。
Package plugin 插件,用于开发一些需要使用单例模式设计的功能,例如服务发现、消息队列与日志系统等。
Package pt 实体与组件原型,用于创建实例。
Package pt 实体与组件原型,用于创建实例。
Package runtime 运行时环境提供的一些接口与函数。
Package runtime 运行时环境提供的一些接口与函数。
Package service 运行时环境提供的一些接口与函数。
Package service 运行时环境提供的一些接口与函数。
Package utils 一些工具类与函数。
Package utils 一些工具类与函数。
uid

Jump to

Keyboard shortcuts

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