core

package module
v0.3.16 Latest Latest
Warning

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

Go to latest
Published: Jul 9, 2024 License: LGPL-2.1 Imports: 21 Imported by: 57

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/eventcode 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

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 gctx.CurrentContextProvider, asyncRet ...async.AsyncRet) AwaitDirector

Await 异步等待结果返回

func (AwaitDirector) All

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

All 异步等待所有结果返回

func (AwaitDirector) Any

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

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

func (AwaitDirector) AnyOK

func (ad AwaitDirector) AnyOK(fun generic.ActionVar2[runtime.Context, async.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(provider gctx.CurrentContextProvider, prototype string) EntityCreator

CreateEntity 创建实体

func (EntityCreator) AwakeOnFirstAccess added in v0.2.38

func (c EntityCreator) AwakeOnFirstAccess(b bool) EntityCreator

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

func (EntityCreator) Composite added in v0.2.80

func (c EntityCreator) Composite(composite ec.Entity) EntityCreator

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

func (EntityCreator) CompositeFace added in v0.2.38

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

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

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

CreateEntityPT 创建实体原型

func (EntityPTCreator) AddComponent added in v0.2.39

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

AddComponent 添加组件

func (EntityPTCreator) AddMutableComponent added in v0.3.12

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

AddMutableComponent 添加不固定的组件

func (EntityPTCreator) AwakeOnFirstAccess added in v0.2.80

func (c EntityPTCreator) AwakeOnFirstAccess(b bool) EntityPTCreator

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

func (EntityPTCreator) Composite added in v0.2.80

func (c EntityPTCreator) Composite(composite any) EntityPTCreator

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

func (EntityPTCreator) Declare added in v0.2.39

func (c EntityPTCreator) Declare()

Declare 声明实体原型

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(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 运行
	Run() <-chan struct{}
	// Terminate 停止
	Terminate() <-chan struct{}
	// TerminatedChan 已停止chan
	TerminatedChan() <-chan struct{}
}

Running 运行接口

type Runtime

type Runtime interface {
	gctx.CurrentContextProvider
	gctx.ConcurrentContextProvider
	async.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, async.Ret], va ...any) async.AsyncRet

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

func (*RuntimeBehavior) PushCallDelegate

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

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

func (*RuntimeBehavior) PushCallVoid

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

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

func (*RuntimeBehavior) PushCallVoidDelegate

func (rt *RuntimeBehavior) PushCallVoidDelegate(fun generic.DelegateActionVar0[any], va ...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) TerminatedChan added in v0.2.78

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

TerminatedChan 已停止chan

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 运行

func (*ServiceBehavior) Terminate

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

Terminate 停止

func (*ServiceBehavior) TerminatedChan added in v0.2.78

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

TerminatedChan 已停止chan

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功能来生成代码。
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