roc

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Aug 5, 2020 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package roc 每个 micserver 进程会持有一份 ROC 缓存单例,维护了所有已知的ROC对象所处的位置

Package roc Remote Object Call. ROC ,是 micserver 重要的分布式目标调用思想。 如果一个对象,例如房间/商品/玩家/工会,需要提供一个可供远程执行的操作, 这在以前称之为 RPC 调用,在 micserver 中,任意一个构造了这种对象的模块, 均可以通过 BaseModule.GetROC(objtype).GetOrRegObj(obj) 来 注册一个 ROC 对象,在其他模块中,只需要通过 BaseModule.ROCCallNR 等方法,提供 目标对象的类型及ID,即可发起针对该对象的远程操作。 因此,在任意模块中,发起的任意针对其他模块的调用,都不应该使用模块ID来操作, 因为使用统一的 ROC 至少包含以下好处:

无需知道目标对象在哪个模块上;
只需要关心目标对象的ID(目标的类型你当然是知道的);
在模块更新时,可以统一将该模块的 ROC 对象迁移到新版本模块中实现热更;
可以将对象存储到数据库并且在其他模块中加载(基于第一点好处);
对象的位置/调用路由等由底层系统维护,可提供一个高可用/强一致/易维护的分布式网络。

Index

Constants

View Source
const (
	CachePoolGroupSum = 8
)

ROC缓存分组数量

View Source
const (
	PoolGroupSum = 8
)

ROC 对象分组数

Variables

View Source
var (
	ErrUnregisterROC = errors.New("unregistered roc")
	ErrUnknowObj     = errors.New("unknow roc obj")
)

ROC错误定义

Functions

This section is empty.

Types

type Cache

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

Cache ROC缓存管理器

func GetCache

func GetCache() *Cache

GetCache 获取ROC缓存

func (*Cache) Del

func (c *Cache) Del(objType ObjType, objID string, moduleID string) bool

Del 删除目标对象

func (*Cache) DelM

func (c *Cache) DelM(objType ObjType, objIDs []string, moduleID string) int

DelM 同时删除多个 返回成功删除的数量

func (*Cache) Get

func (c *Cache) Get(objType ObjType, objID string) string

Get 获取缓存的目标对象在哪个模块上

func (*Cache) RandomObjIDByType

func (c *Cache) RandomObjIDByType(objType ObjType, limitModuleIDs map[string]bool) string

RandomObjIDByType 随机获取一个目标类型的缓存对象ID

func (*Cache) RangeByType

func (c *Cache) RangeByType(objType ObjType, f func(id string, location string) bool, limitModuleIDs map[string]bool)

RangeByType 遍历指定类型的ROC对象

func (*Cache) Set

func (c *Cache) Set(objType ObjType, objID string, moduleID string) error

Set 添加目标对象

func (*Cache) SetM

func (c *Cache) SetM(objType ObjType, objIDs []string, moduleID string) error

SetM 同时添加多个

type IObj

type IObj interface {
	GetROCObjType() ObjType
	GetROCObjID() string
	OnROCCall(*Path, []byte) ([]byte, error)
}

IObj ROC对象需要实现的接口

type IROCObjEventHook

type IROCObjEventHook interface {
	OnROCObjAdd(IObj)
	OnROCObjDel(IObj)
}

IROCObjEventHook ROC事件处理需要实现的接口

type IROCServer

type IROCServer interface {
	GetROC(ObjType) *ROC
	NewROC(ObjType) *ROC
	ROCCallNR(*Path, []byte) error
	ROCCallBlock(*Path, []byte) ([]byte, error)
	GetROCCachedLocation(ObjType, string) string
	RangeROCCachedByType(ObjType, func(id string, location string) bool)
	RandomROCCachedByType(ObjType) string
}

IROCServer ROC服务器实现的接口

type Manager

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

Manager ROC 管理器,每个 Module 都包含了一个ROC管理器,管理了本 Module 已注册的所有ROC类型及ROC对象。

func (*Manager) Call

func (rocManager *Manager) Call(callstr string, arg []byte) ([]byte, error)

Call 执行远程发来的ROC调用请求

func (*Manager) CallPathDecode

func (rocManager *Manager) CallPathDecode(kstr string) (ObjType, string)

CallPathDecode 解码ROC调用路径

func (*Manager) GetObj

func (rocManager *Manager) GetObj(objType ObjType, objID string) (IObj, bool)

GetObj kstr的格式必须为 ROC 远程对象调用那样定义的格式 (对象类型)([对象的键])

func (*Manager) GetROC

func (rocManager *Manager) GetROC(objtype ObjType) *ROC

GetROC 获取一个类型ROC

func (*Manager) HookObjEvent

func (rocManager *Manager) HookObjEvent(hook IROCObjEventHook)

HookObjEvent 监听ROC事件

func (*Manager) NewROC

func (rocManager *Manager) NewROC(objtype ObjType) *ROC

NewROC 新建一种类型的ROC

func (*Manager) OnROCObjAdd

func (rocManager *Manager) OnROCObjAdd(obj IObj)

OnROCObjAdd 当增加一个ROC对象时调用

func (*Manager) OnROCObjDel

func (rocManager *Manager) OnROCObjDel(obj IObj)

OnROCObjDel 当删除一个ROC对象时调用

func (*Manager) RegObj

func (rocManager *Manager) RegObj(obj IObj) error

RegObj 注册一个ROC对象,如果该类型的ROC未注册将返回 error

type ObjType

type ObjType string

ObjType ROC 对象的类型,本质上是字符串

type Path

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

Path ROC调用的路径

func NewPath

func NewPath(pathstr string) *Path

NewPath 根据调用路径字符串,构造一个ROC调用路径

func O

func O(objType ObjType, objID string) *Path

O 根据目标ROC的类型及ID,构造一个ROC调用路径

func (*Path) F

func (path *Path) F(funcName string) *Path

F 添加一个ROC调用路径的函数段,一个ROC调用可以携带多个函数名,中间以 . 号连接

func (*Path) Get

func (path *Path) Get(pos int) string

Get 获取当前ROC调用路径指定位置的值

func (*Path) GetObjID

func (path *Path) GetObjID() string

GetObjID 获取ROC调用路径的目标ROC对象ID

func (*Path) GetObjType

func (path *Path) GetObjType() ObjType

GetObjType 获取ROC调用路径的目标ROC对象类型

func (*Path) GetPos

func (path *Path) GetPos() int

GetPos 获取ROC调用路径当前行进到的位置

func (*Path) Move

func (path *Path) Move() string

Move 移动当前ROC调用路径到下一个位置,并返回该位置的值

func (*Path) Reset

func (path *Path) Reset()

Reset 重置当前ROC调用路径的位置

func (*Path) String

func (path *Path) String() string

String 获取当前ROC调用路径的字符串描述

type ROC

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

ROC 一个类型的ROC,维护了这个类型的所有 ROC 对象

func (*ROC) DelObj

func (roc *ROC) DelObj(obj IObj) error

DelObj 删除目标ROC对象

func (*ROC) DelObjByID

func (roc *ROC) DelObjByID(id string) error

DelObjByID 删除指定ID的ROC对象

func (*ROC) GetObj

func (roc *ROC) GetObj(id string) (IObj, bool)

GetObj 获取指定ID的ROC对象

func (*ROC) GetOrRegObj

func (roc *ROC) GetOrRegObj(id string, obj IObj) (IObj, bool)

GetOrRegObj 获取或者注册一个ROC对象

func (*ROC) HookObjEvent

func (roc *ROC) HookObjEvent(hook IROCObjEventHook)

HookObjEvent 设置消息

func (*ROC) Init

func (roc *ROC) Init()

Init 初始化该类型的ROC

func (*ROC) RangeObj

func (roc *ROC) RangeObj(f func(obj IObj) bool)

RangeObj 遍历该类型的ROC对象

func (*ROC) RegObj

func (roc *ROC) RegObj(obj IObj) error

RegObj 在使用远程对象调用前,需要先注册

Jump to

Keyboard shortcuts

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