mirror of
https://github.com/duanhf2012/origin.git
synced 2026-03-06 22:27:36 +08:00
Compare commits
44 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
000853b479 | ||
|
|
387e83d65c | ||
|
|
07a102c6ea | ||
|
|
e9bbf5b592 | ||
|
|
3f4189fd40 | ||
|
|
c72f2e4582 | ||
|
|
f600c2a573 | ||
|
|
6a29ba2c88 | ||
|
|
64c14eb326 | ||
|
|
75790302ec | ||
|
|
b943ea9a83 | ||
|
|
39116c4402 | ||
|
|
1cf071a444 | ||
|
|
e6c09064bf | ||
|
|
84ab0cb84a | ||
|
|
22fe00173b | ||
|
|
8e0ed62fca | ||
|
|
7116b509e9 | ||
|
|
73d384361d | ||
|
|
ce56b19fe8 | ||
|
|
1367d776e6 | ||
|
|
987d35ff15 | ||
|
|
d225bb4bd2 | ||
|
|
ea37fb5081 | ||
|
|
0a92f48d0b | ||
|
|
f5e86fee02 | ||
|
|
166facc959 | ||
|
|
5bb747201b | ||
|
|
1014bc54e4 | ||
|
|
9c26c742fe | ||
|
|
d1935b1bbc | ||
|
|
90d54bf3e2 | ||
|
|
78cc33c84e | ||
|
|
9cf21bf418 | ||
|
|
c6d0bd9a19 | ||
|
|
61bf95e457 | ||
|
|
8b2a551ee5 | ||
|
|
927c2ffa37 | ||
|
|
b23b30aac5 | ||
|
|
03f8ba0316 | ||
|
|
277480a7f0 | ||
|
|
647a654a36 | ||
|
|
de483a88f1 | ||
|
|
bbbb511b5f |
159
README.md
159
README.md
@@ -14,17 +14,7 @@ origin 解决的问题:
|
||||
Hello world!
|
||||
------------
|
||||
|
||||
下面我们来一步步的建立origin服务器,先下载[origin引擎](https://github.com/duanhf2012/origin "origin引擎"),或者使用如下命令:
|
||||
|
||||
```go
|
||||
go get -v -u github.com/duanhf2012/origin/v2
|
||||
```
|
||||
建议使用origin v2版本(import引用中都加入v2)
|
||||
|
||||
|
||||
|
||||
[README.md](README.md)
|
||||
于是下载到GOPATH环境目录中,在src中加入main.go,内容如下:
|
||||
下面我们来一步步的建立origin服务器,建议使用origin v2版本,内容如下:
|
||||
|
||||
```go
|
||||
package main
|
||||
@@ -43,11 +33,19 @@ func main() {
|
||||
一个origin进程需要创建一个node对象,Start开始运行。您也可以直接下载origin引擎示例:
|
||||
|
||||
```
|
||||
go get -v -u github.com/duanhf2012/originserver_v2
|
||||
git clone https://github.com/duanhf2012/originserver_v2.git
|
||||
```
|
||||
|
||||
本文所有的说明都是基于该示例为主。
|
||||
|
||||
我们也提供了示例项目,包含网络模块(支持tcp,kcp,websocket)、角色加载登录流程,存档,配置读取等功能。请参照:
|
||||
|
||||
```
|
||||
git clone https://github.com/duanhf2012/origingame.git
|
||||
```
|
||||
|
||||
|
||||
|
||||
origin引擎三大对象关系
|
||||
----------------------
|
||||
|
||||
@@ -69,16 +67,10 @@ origin的配置文件以json格式,主要包含Discovery、RpcMode、NodeList
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
### Discovery部分
|
||||
|
||||
origin目前支持etcd与origin自带的服务发现类型。
|
||||
|
||||
|
||||
|
||||
Etcd方式示例:
|
||||
|
||||
```json
|
||||
@@ -106,8 +98,6 @@ NetworkName:所在的网络名称,可以配置多个。node会往对应的
|
||||
|
||||
Endpoints:Etcd服务器地址
|
||||
|
||||
|
||||
|
||||
Origin方式示例:
|
||||
|
||||
```json
|
||||
@@ -130,8 +120,6 @@ TTLSecond:表示健康检查TTL失效时间10秒
|
||||
|
||||
MasterNodeList:指定哪些Node为服务发现Master结点,需要配置NodeId与ListenAddr,注意它们要与实际的Node配置一致。
|
||||
|
||||
|
||||
|
||||
### RpcMode部分
|
||||
|
||||
默认模式
|
||||
@@ -146,8 +134,6 @@ MasterNodeList:指定哪些Node为服务发现Master结点,需要配置NodeI
|
||||
|
||||
默认模式下,origin的node之前通过tcp连接组网。
|
||||
|
||||
|
||||
|
||||
Nats模式
|
||||
|
||||
```json
|
||||
@@ -167,8 +153,6 @@ NatsUrl:Nats连接url串
|
||||
|
||||
NoRandomize:在多连接集群模式下,连接nats节点是否顺序策略。false表示随机连接,true表示顺序连接。
|
||||
|
||||
|
||||
|
||||
### NodeList部分
|
||||
|
||||
```
|
||||
@@ -212,8 +196,6 @@ NoRandomize:在多连接集群模式下,连接nats节点是否顺序策略。f
|
||||
在启动程序命令originserver -start nodeid="node_1"中nodeid就是根据该配置装载服务。
|
||||
更多参数使用,请使用originserver -help查看。
|
||||
|
||||
|
||||
|
||||
### Service 部分
|
||||
|
||||
service.json如下:
|
||||
@@ -231,8 +213,7 @@ service.json如下:
|
||||
{
|
||||
"Certfile":"",
|
||||
"Keyfile":""
|
||||
}
|
||||
]
|
||||
}]
|
||||
|
||||
},
|
||||
"TcpService":{
|
||||
@@ -319,8 +300,6 @@ service.json如下:
|
||||
* PendingWriteNum:发送网络队列最大数量
|
||||
* MaxMsgLen:包最大长度
|
||||
|
||||
|
||||
|
||||
### Global部分
|
||||
|
||||
```json
|
||||
@@ -343,8 +322,6 @@ if ok == false {
|
||||
areaId, ok := mapGlobal["AreaId"]
|
||||
```
|
||||
|
||||
|
||||
|
||||
---
|
||||
|
||||
第一章:origin基础:
|
||||
@@ -511,7 +488,6 @@ func (slf *TestService1) OnInit() error {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
性能监控功能:
|
||||
-------------
|
||||
|
||||
@@ -662,51 +638,7 @@ Module1 Release.
|
||||
第四章:事件使用
|
||||
----------------
|
||||
|
||||
事件是origin中一个重要的组成部分,可以在同一个node中的service与service或者与module之间进行事件通知。系统内置的几个服务,如:TcpService/HttpService等都是通过事件功能实现。他也是一个典型的观察者设计模型。在event中有两个类型的interface,一个是event.IEventProcessor它提供注册与卸载功能,另一个是event.IEventHandler提供消息广播等功能。
|
||||
|
||||
在目录simple_event/TestService4.go中
|
||||
|
||||
```
|
||||
package simple_event
|
||||
|
||||
import (
|
||||
"github.com/duanhf2012/origin/v2/event"
|
||||
"github.com/duanhf2012/origin/v2/node"
|
||||
"github.com/duanhf2012/origin/v2/service"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
//自定义事件类型,必需从event.Sys_Event_User_Define开始
|
||||
//event.Sys_Event_User_Define以内给系统预留
|
||||
EVENT1 event.EventType =event.Sys_Event_User_Define+1
|
||||
)
|
||||
|
||||
func init(){
|
||||
node.Setup(&TestService4{})
|
||||
}
|
||||
|
||||
type TestService4 struct {
|
||||
service.Service
|
||||
}
|
||||
|
||||
func (slf *TestService4) OnInit() error {
|
||||
//10秒后触发广播事件
|
||||
slf.AfterFunc(time.Second*10,slf.TriggerEvent)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (slf *TestService4) TriggerEvent(){
|
||||
//广播事件,传入event.Event对象,类型为EVENT1,Data可以自定义任何数据
|
||||
//这样,所有监听者都可以收到该事件
|
||||
slf.GetEventHandler().NotifyEvent(&event.Event{
|
||||
Type: EVENT1,
|
||||
Data: "event data.",
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
```
|
||||
事件是origin中一个重要的组成部分,可以在服务与各module之间进行事件通知。它也是一个典型的观察者设计模型。在event中有两个类型的interface,一个是event.IEventProcessor它提供注册与卸载功能,另一个是event.IEventHandler提供消息广播等功能。
|
||||
|
||||
在目录simple_event/TestService5.go中
|
||||
|
||||
@@ -718,12 +650,20 @@ import (
|
||||
"github.com/duanhf2012/origin/v2/event"
|
||||
"github.com/duanhf2012/origin/v2/node"
|
||||
"github.com/duanhf2012/origin/v2/service"
|
||||
"github.com/duanhf2012/origin/v2/util/timer"
|
||||
"time"
|
||||
)
|
||||
|
||||
func init() {
|
||||
node.Setup(&TestService5{})
|
||||
}
|
||||
|
||||
const (
|
||||
//自定义事件类型,必需从event.Sys_Event_User_Define开始
|
||||
//event.Sys_Event_User_Define以内给系统预留
|
||||
EVENT1 event.EventType = event.Sys_Event_User_Define + 1
|
||||
)
|
||||
|
||||
type TestService5 struct {
|
||||
service.Service
|
||||
}
|
||||
@@ -733,44 +673,51 @@ type TestModule struct {
|
||||
}
|
||||
|
||||
func (slf *TestModule) OnInit() error {
|
||||
//在当前node中查找TestService4
|
||||
pService := node.GetService("TestService4")
|
||||
//在TestModule中注册监听EVENT1事件
|
||||
slf.GetEventProcessor().RegEventReceiverFunc(EVENT1, slf.GetEventHandler(), slf.OnModuleEvent)
|
||||
|
||||
//在TestModule中,往TestService4中注册EVENT1类型事件监听
|
||||
pService.(*TestService4).GetEventProcessor().RegEventReciverFunc(EVENT1,slf.GetEventHandler(),slf.OnModuleEvent)
|
||||
return nil
|
||||
}
|
||||
|
||||
// OnModuleEvent 模块监听事件回调
|
||||
func (slf *TestModule) OnModuleEvent(ev event.IEvent) {
|
||||
event := ev.(*event.Event)
|
||||
fmt.Printf("OnModuleEvent type :%d data:%+v\n", event.GetEventType(), event.Data)
|
||||
}
|
||||
|
||||
|
||||
//服务初始化函数,在安装服务时,服务将自动调用OnInit函数
|
||||
// OnInit 服务初始化函数,在安装服务时,服务将自动调用OnInit函数
|
||||
func (slf *TestService5) OnInit() error {
|
||||
//通过服务名获取服务对象
|
||||
pService := node.GetService("TestService4")
|
||||
|
||||
////在TestModule中,往TestService4中注册EVENT1类型事件监听
|
||||
pService.(*TestService4).GetEventProcessor().RegEventReciverFunc(EVENT1,slf.GetEventHandler(),slf.OnServiceEvent)
|
||||
//在服务中注册监听EVENT1类型事件
|
||||
slf.RegEventReceiverFunc(EVENT1, slf.GetEventHandler(), slf.OnServiceEvent)
|
||||
slf.AddModule(&TestModule{})
|
||||
|
||||
slf.AfterFunc(time.Second*10, slf.TriggerEvent)
|
||||
return nil
|
||||
}
|
||||
|
||||
// OnServiceEvent 服务监听事件回调
|
||||
func (slf *TestService5) OnServiceEvent(ev event.IEvent) {
|
||||
event := ev.(*event.Event)
|
||||
fmt.Printf("OnServiceEvent type :%d data:%+v\n", event.Type, event.Data)
|
||||
}
|
||||
|
||||
func (slf *TestService5) TriggerEvent(t *timer.Timer) {
|
||||
//广播事件,传入event.Event对象,类型为EVENT1,Data可以自定义任何数据
|
||||
//这样,所有监听者都可以收到该事件
|
||||
slf.GetEventHandler().NotifyEvent(&event.Event{
|
||||
Type: EVENT1,
|
||||
Data: "event data.",
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
```
|
||||
|
||||
程序运行10秒后,调用slf.TriggerEvent函数广播事件,于是在TestService5中会收到
|
||||
|
||||
```
|
||||
OnServiceEvent type :1001 data:event data.
|
||||
OnModuleEvent type :1001 data:event data.
|
||||
OnServiceEvent type :2 data:event data.
|
||||
OnModuleEvent type :2 data:event data.
|
||||
```
|
||||
|
||||
在上面的TestModule中监听的事情,当这个Module被Release时监听会自动卸载。
|
||||
@@ -917,10 +864,11 @@ func (slf *TestService7) GoTest(){
|
||||
|
||||
您可以把TestService6配置到其他的Node中,比如NodeId为2中。只要在一个子网,origin引擎可以无差别调用。开发者只需要关注Service关系。同样它也是您服务器架构设计的核心需要思考的部分。
|
||||
|
||||
|
||||
第六章:并发函数调用
|
||||
---------------
|
||||
--------------------
|
||||
|
||||
在开发中经常会有将某些任务放到其他协程中并发执行,执行完成后,将服务的工作线程去回调。使用方式很简单,先打开该功能如下代码:
|
||||
|
||||
```
|
||||
//以下通过cpu数量来定开启协程并发数量,建议:(1)cpu密集型计算使用1.0 (2)i/o密集型使用2.0或者更高
|
||||
slf.OpenConcurrentByNumCPU(1.0)
|
||||
@@ -931,6 +879,7 @@ func (slf *TestService7) GoTest(){
|
||||
```
|
||||
|
||||
使用示例如下:
|
||||
|
||||
```
|
||||
|
||||
func (slf *TestService13) testAsyncDo() {
|
||||
@@ -984,9 +933,8 @@ func (slf *TestService13) testAsyncDo() {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
第七章:服务发现
|
||||
--------------------
|
||||
----------------
|
||||
|
||||
origin引擎默认使用读取所有结点配置的进行确认结点有哪些Service。引擎也支持动态服务发现的方式,支持etcd与origin类型,具体请参照【配置说明】部分,Node结点可以配置只发现某些服务,如下示例:
|
||||
|
||||
@@ -1009,12 +957,11 @@ origin引擎默认使用读取所有结点配置的进行确认结点有哪些Se
|
||||
}]
|
||||
}
|
||||
```
|
||||
|
||||
DiscoveryService:在当前nodeid为nodeid_test的结点中,只发现 MasterNodeId为nodeid_1或NetworkName为networkname1网络中的TestService8服务。
|
||||
|
||||
**注意**:MasterNodeId与NetworkName只配置一个,分别在模式为origin或者etcd服务发现类型时。
|
||||
|
||||
|
||||
|
||||
第八章:HttpService使用
|
||||
-----------------------
|
||||
|
||||
@@ -1171,13 +1118,19 @@ func (slf *TestTcpService) OnRequest (clientid string,msg proto.Message){
|
||||
第十章:其他系统模块介绍
|
||||
------------------------
|
||||
|
||||
* sysservice/wsservice.go:支持了WebSocket协议,使用方法与TcpService类似
|
||||
* sysmodule/DBModule.go:对mysql数据库操作
|
||||
* sysmodule/RedisModule.go:对Redis数据进行操作
|
||||
* sysmodule/HttpClientPoolModule.go:Http客户端请求封装
|
||||
* sysservice/wsservice/:支持了WebSocket协议,使用方法与TcpService类似
|
||||
* sysservice/messagequeueservice/:自定义的消息队列
|
||||
* sysservice/rankservice/:排行榜服务,采用跳表数据结构实现
|
||||
* sysmodule/mysqlmodule/:对mysql数据库操作
|
||||
* sysmodule/redismodule/:对Redis数据进行操作
|
||||
* sysmodule/httpclientmodule/:Http客户端请求封装
|
||||
* sysmodule/ginmodule/:对gin模块的封装,支持服务协程处理
|
||||
* sysmodule/kafkamodule/:对kafka的封装
|
||||
* log/log.go:日志的封装,可以使用它构建对象记录业务文件日志
|
||||
* util:在该目录下,有常用的uuid,hash,md5,协程封装等工具库
|
||||
* https://github.com/duanhf2012/originservice: 其他扩展支持的服务可以在该工程上看到,目前支持firebase推送的封装。
|
||||
* https://github.com/duanhf2012/origingame: 基础游戏服务器的框架
|
||||
* etcd与nats开发环境搭建可以从https://github.com/duanhf2012/originserver_v2下的docker-compose获取
|
||||
|
||||
备注:
|
||||
-----
|
||||
|
||||
@@ -1,13 +1,15 @@
|
||||
package cluster
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/duanhf2012/origin/v2/event"
|
||||
"github.com/duanhf2012/origin/v2/log"
|
||||
"github.com/duanhf2012/origin/v2/rpc"
|
||||
"github.com/duanhf2012/origin/v2/service"
|
||||
"reflect"
|
||||
"strings"
|
||||
"sync"
|
||||
"github.com/duanhf2012/origin/v2/event"
|
||||
)
|
||||
|
||||
var configDir = "./config/"
|
||||
@@ -54,7 +56,6 @@ type Cluster struct {
|
||||
|
||||
discoveryInfo DiscoveryInfo //服务发现配置
|
||||
rpcMode RpcMode
|
||||
//masterDiscoveryNodeList []NodeInfo //配置发现Master结点
|
||||
globalCfg interface{} //全局配置
|
||||
|
||||
localServiceCfg map[string]interface{} //map[serviceName]配置数据*
|
||||
@@ -63,6 +64,7 @@ type Cluster struct {
|
||||
locker sync.RWMutex //结点与服务关系保护锁
|
||||
mapRpc map[string]*NodeRpcInfo //nodeId
|
||||
mapServiceNode map[string]map[string]struct{} //map[serviceName]map[NodeId]
|
||||
mapTemplateServiceNode map[string]map[string]struct{} //map[templateServiceName]map[serviceName]nodeId
|
||||
|
||||
callSet rpc.CallSet
|
||||
rpcNats rpc.RpcNats
|
||||
@@ -70,7 +72,6 @@ type Cluster struct {
|
||||
|
||||
rpcEventLocker sync.RWMutex //Rpc事件监听保护锁
|
||||
mapServiceListenRpcEvent map[string]struct{} //ServiceName
|
||||
mapServiceListenDiscoveryEvent map[string]struct{} //ServiceName
|
||||
}
|
||||
|
||||
func GetCluster() *Cluster {
|
||||
@@ -112,22 +113,22 @@ func (cls *Cluster) DelNode(nodeId string) {
|
||||
cls.locker.Lock()
|
||||
defer cls.locker.Unlock()
|
||||
|
||||
rpc, ok := cls.mapRpc[nodeId]
|
||||
nodeRpc, ok := cls.mapRpc[nodeId]
|
||||
if ok == false {
|
||||
return
|
||||
}
|
||||
|
||||
cls.TriggerDiscoveryEvent(false,nodeId,rpc.nodeInfo.ServiceList)
|
||||
for _, serviceName := range rpc.nodeInfo.ServiceList {
|
||||
cls.TriggerDiscoveryEvent(false, nodeId, nodeRpc.nodeInfo.ServiceList)
|
||||
for _, serviceName := range nodeRpc.nodeInfo.ServiceList {
|
||||
cls.delServiceNode(serviceName, nodeId)
|
||||
}
|
||||
|
||||
delete(cls.mapRpc, nodeId)
|
||||
if ok == true {
|
||||
rpc.client.Close(false)
|
||||
nodeRpc.client.Close(false)
|
||||
}
|
||||
|
||||
log.Info("remove node ",log.String("NodeId", rpc.nodeInfo.NodeId),log.String("ListenAddr", rpc.nodeInfo.ListenAddr))
|
||||
log.Info("remove node ", log.String("NodeId", nodeRpc.nodeInfo.NodeId), log.String("ListenAddr", nodeRpc.nodeInfo.ListenAddr))
|
||||
}
|
||||
|
||||
func (cls *Cluster) serviceDiscoveryDelNode(nodeId string) {
|
||||
@@ -139,6 +140,20 @@ func (cls *Cluster) delServiceNode(serviceName string, nodeId string) {
|
||||
return
|
||||
}
|
||||
|
||||
//处理模板服务
|
||||
splitServiceName := strings.Split(serviceName, ":")
|
||||
if len(splitServiceName) == 2 {
|
||||
serviceName = splitServiceName[0]
|
||||
templateServiceName := splitServiceName[1]
|
||||
|
||||
mapService := cls.mapTemplateServiceNode[templateServiceName]
|
||||
delete(mapService, serviceName)
|
||||
|
||||
if len(cls.mapTemplateServiceNode[templateServiceName]) == 0 {
|
||||
delete(cls.mapTemplateServiceNode, templateServiceName)
|
||||
}
|
||||
}
|
||||
|
||||
mapNode := cls.mapServiceNode[serviceName]
|
||||
delete(mapNode, nodeId)
|
||||
if len(mapNode) == 0 {
|
||||
@@ -173,7 +188,20 @@ func (cls *Cluster) serviceDiscoverySetNodeInfo(nodeInfo *NodeInfo) {
|
||||
continue
|
||||
}
|
||||
mapDuplicate[serviceName] = nil
|
||||
if _, ok := cls.mapServiceNode[serviceName]; ok == false {
|
||||
|
||||
//如果是模板服务,则记录模板关系
|
||||
splitServiceName := strings.Split(serviceName, ":")
|
||||
if len(splitServiceName) == 2 {
|
||||
serviceName = splitServiceName[0]
|
||||
templateServiceName := splitServiceName[1]
|
||||
//记录模板
|
||||
if _, ok = cls.mapTemplateServiceNode[templateServiceName]; ok == false {
|
||||
cls.mapTemplateServiceNode[templateServiceName] = map[string]struct{}{}
|
||||
}
|
||||
cls.mapTemplateServiceNode[templateServiceName][serviceName] = struct{}{}
|
||||
}
|
||||
|
||||
if _, ok = cls.mapServiceNode[serviceName]; ok == false {
|
||||
cls.mapServiceNode[serviceName] = make(map[string]struct{}, 1)
|
||||
}
|
||||
cls.mapServiceNode[serviceName][nodeInfo.NodeId] = struct{}{}
|
||||
@@ -202,7 +230,6 @@ func (cls *Cluster) serviceDiscoverySetNodeInfo(nodeInfo *NodeInfo) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func (cls *Cluster) Init(localNodeId string, setupServiceFun SetupServiceFun) error {
|
||||
//1.初始化配置
|
||||
err := cls.InitCfg(localNodeId)
|
||||
@@ -228,8 +255,6 @@ func (cls *Cluster) Init(localNodeId string, setupServiceFun SetupServiceFun) er
|
||||
}
|
||||
service.RegRpcEventFun = cls.RegRpcEvent
|
||||
service.UnRegRpcEventFun = cls.UnRegRpcEvent
|
||||
service.RegDiscoveryServiceEventFun = cls.RegDiscoveryEvent
|
||||
service.UnRegDiscoveryServiceEventFun = cls.UnReDiscoveryEvent
|
||||
|
||||
err = cls.serviceDiscovery.InitDiscovery(localNodeId, cls.serviceDiscoveryDelNode, cls.serviceDiscoverySetNodeInfo)
|
||||
if err != nil {
|
||||
@@ -263,29 +288,32 @@ func (cls *Cluster) GetRpcClient(nodeId string) (*rpc.Client,bool) {
|
||||
return cls.getRpcClient(nodeId)
|
||||
}
|
||||
|
||||
func GetRpcClient(nodeId string, serviceMethod string,filterRetire bool, clientList []*rpc.Client) (error, int) {
|
||||
func GetNodeIdByTemplateService(templateServiceName string, rpcClientList []*rpc.Client, filterRetire bool) (error, []*rpc.Client) {
|
||||
return GetCluster().GetNodeIdByTemplateService(templateServiceName, rpcClientList, filterRetire)
|
||||
}
|
||||
|
||||
func GetRpcClient(nodeId string, serviceMethod string, filterRetire bool, clientList []*rpc.Client) (error, []*rpc.Client) {
|
||||
if nodeId != rpc.NodeIdNull {
|
||||
pClient, retire := GetCluster().GetRpcClient(nodeId)
|
||||
if pClient == nil {
|
||||
return fmt.Errorf("cannot find nodeid %d!", nodeId), 0
|
||||
return fmt.Errorf("cannot find nodeid %s", nodeId), nil
|
||||
}
|
||||
|
||||
//如果需要筛选掉退休结点
|
||||
if filterRetire == true && retire == true {
|
||||
return fmt.Errorf("cannot find nodeid %d!", nodeId), 0
|
||||
return fmt.Errorf("cannot find nodeid %s", nodeId), nil
|
||||
}
|
||||
|
||||
clientList[0] = pClient
|
||||
return nil, 1
|
||||
clientList = append(clientList, pClient)
|
||||
return nil, clientList
|
||||
}
|
||||
|
||||
findIndex := strings.Index(serviceMethod, ".")
|
||||
if findIndex == -1 {
|
||||
return fmt.Errorf("servicemethod param %s is error!", serviceMethod), 0
|
||||
return fmt.Errorf("servicemethod param %s is error!", serviceMethod), nil
|
||||
}
|
||||
serviceName := serviceMethod[:findIndex]
|
||||
|
||||
//1.找到对应的rpcNodeid
|
||||
return GetCluster().GetNodeIdByService(serviceName, clientList, filterRetire)
|
||||
}
|
||||
|
||||
@@ -310,7 +338,7 @@ func (cls *Cluster) NotifyAllService(event event.IEvent){
|
||||
cls.rpcEventLocker.Lock()
|
||||
defer cls.rpcEventLocker.Unlock()
|
||||
|
||||
for serviceName, _ := range cls.mapServiceListenRpcEvent {
|
||||
for serviceName := range cls.mapServiceListenRpcEvent {
|
||||
ser := service.GetService(serviceName)
|
||||
if ser == nil {
|
||||
log.Error("cannot find service name " + serviceName)
|
||||
@@ -322,23 +350,12 @@ func (cls *Cluster) NotifyAllService(event event.IEvent){
|
||||
}
|
||||
|
||||
func (cls *Cluster) TriggerDiscoveryEvent(bDiscovery bool, nodeId string, serviceName []string) {
|
||||
cls.rpcEventLocker.Lock()
|
||||
defer cls.rpcEventLocker.Unlock()
|
||||
|
||||
for sName, _ := range cls.mapServiceListenDiscoveryEvent {
|
||||
ser := service.GetService(sName)
|
||||
if ser == nil {
|
||||
log.Error("cannot find service",log.Any("services",serviceName))
|
||||
continue
|
||||
}
|
||||
|
||||
var eventData service.DiscoveryServiceEvent
|
||||
eventData.IsDiscovery = bDiscovery
|
||||
eventData.NodeId = nodeId
|
||||
eventData.ServiceName = serviceName
|
||||
ser.(service.IModule).NotifyEvent(&eventData)
|
||||
}
|
||||
|
||||
cls.NotifyAllService(&eventData)
|
||||
}
|
||||
|
||||
func (cls *Cluster) GetLocalNodeInfo() *NodeInfo {
|
||||
@@ -361,25 +378,6 @@ func (cls *Cluster) UnRegRpcEvent(serviceName string) {
|
||||
cls.rpcEventLocker.Unlock()
|
||||
}
|
||||
|
||||
|
||||
func (cls *Cluster) RegDiscoveryEvent(serviceName string) {
|
||||
cls.rpcEventLocker.Lock()
|
||||
if cls.mapServiceListenDiscoveryEvent == nil {
|
||||
cls.mapServiceListenDiscoveryEvent = map[string]struct{}{}
|
||||
}
|
||||
|
||||
cls.mapServiceListenDiscoveryEvent[serviceName] = struct{}{}
|
||||
cls.rpcEventLocker.Unlock()
|
||||
}
|
||||
|
||||
func (cls *Cluster) UnReDiscoveryEvent(serviceName string) {
|
||||
cls.rpcEventLocker.Lock()
|
||||
delete(cls.mapServiceListenDiscoveryEvent, serviceName)
|
||||
cls.rpcEventLocker.Unlock()
|
||||
}
|
||||
|
||||
|
||||
|
||||
func HasService(nodeId string, serviceName string) bool {
|
||||
cluster.locker.RLock()
|
||||
defer cluster.locker.RUnlock()
|
||||
@@ -403,17 +401,56 @@ func GetNodeByServiceName(serviceName string) map[string]struct{} {
|
||||
}
|
||||
|
||||
mapNodeId := map[string]struct{}{}
|
||||
for nodeId,_ := range mapNode {
|
||||
for nodeId := range mapNode {
|
||||
mapNodeId[nodeId] = struct{}{}
|
||||
}
|
||||
|
||||
return mapNodeId
|
||||
}
|
||||
|
||||
// GetNodeByTemplateServiceName 通过模板服务名获取服务名,返回 map[serviceName真实服务名]NodeId
|
||||
func GetNodeByTemplateServiceName(templateServiceName string) map[string]string {
|
||||
cluster.locker.RLock()
|
||||
defer cluster.locker.RUnlock()
|
||||
|
||||
mapServiceName := cluster.mapTemplateServiceNode[templateServiceName]
|
||||
mapNodeId := make(map[string]string, 9)
|
||||
for serviceName := range mapServiceName {
|
||||
mapNode, ok := cluster.mapServiceNode[serviceName]
|
||||
if ok == false {
|
||||
return nil
|
||||
}
|
||||
|
||||
for nodeId := range mapNode {
|
||||
mapNodeId[serviceName] = nodeId
|
||||
}
|
||||
}
|
||||
|
||||
return mapNodeId
|
||||
}
|
||||
|
||||
func (cls *Cluster) GetGlobalCfg() interface{} {
|
||||
return cls.globalCfg
|
||||
}
|
||||
|
||||
func (cls *Cluster) ParseGlobalCfg(cfg interface{}) error {
|
||||
if cls.globalCfg == nil {
|
||||
return errors.New("no service configuration found")
|
||||
}
|
||||
|
||||
rv := reflect.ValueOf(cls.globalCfg)
|
||||
if rv.Kind() == reflect.Ptr && rv.IsNil() {
|
||||
return errors.New("no service configuration found")
|
||||
}
|
||||
|
||||
bytes, err := json.Marshal(cls.globalCfg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return json.Unmarshal(bytes, cfg)
|
||||
}
|
||||
|
||||
func (cls *Cluster) GetNodeInfo(nodeId string) (NodeInfo, bool) {
|
||||
cls.locker.RLock()
|
||||
defer cls.locker.RUnlock()
|
||||
@@ -426,19 +463,24 @@ func (cls *Cluster) GetNodeInfo(nodeId string) (NodeInfo,bool) {
|
||||
return nodeInfo.nodeInfo, true
|
||||
}
|
||||
|
||||
func (dc *Cluster) CanDiscoveryService(fromMasterNodeId string,serviceName string) bool{
|
||||
func (cls *Cluster) CanDiscoveryService(fromMasterNodeId string, serviceName string) bool {
|
||||
canDiscovery := true
|
||||
|
||||
for i:=0;i<len(dc.GetLocalNodeInfo().DiscoveryService);i++{
|
||||
masterNodeId := dc.GetLocalNodeInfo().DiscoveryService[i].MasterNodeId
|
||||
splitServiceName := strings.Split(serviceName, ":")
|
||||
if len(splitServiceName) == 2 {
|
||||
serviceName = splitServiceName[0]
|
||||
}
|
||||
|
||||
for i := 0; i < len(cls.GetLocalNodeInfo().DiscoveryService); i++ {
|
||||
masterNodeId := cls.GetLocalNodeInfo().DiscoveryService[i].MasterNodeId
|
||||
//无效的配置,则跳过
|
||||
if masterNodeId == rpc.NodeIdNull && len(dc.GetLocalNodeInfo().DiscoveryService[i].ServiceList)==0 {
|
||||
if masterNodeId == rpc.NodeIdNull && len(cls.GetLocalNodeInfo().DiscoveryService[i].ServiceList) == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
canDiscovery = false
|
||||
if masterNodeId == fromMasterNodeId || masterNodeId == rpc.NodeIdNull {
|
||||
for _,discoveryService := range dc.GetLocalNodeInfo().DiscoveryService[i].ServiceList {
|
||||
for _, discoveryService := range cls.GetLocalNodeInfo().DiscoveryService[i].ServiceList {
|
||||
if discoveryService == serviceName {
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package cluster
|
||||
|
||||
|
||||
import (
|
||||
"github.com/duanhf2012/origin/v2/event"
|
||||
"github.com/duanhf2012/origin/v2/log"
|
||||
@@ -23,7 +22,6 @@ import (
|
||||
)
|
||||
|
||||
const originDir = "/origin"
|
||||
const testKey = originDir+"/_inner/_test_7501f3ed-b716-44c2-0090-fc1ed0166d7a"
|
||||
|
||||
type etcdClientInfo struct {
|
||||
watchKeys []string
|
||||
@@ -44,7 +42,7 @@ type EtcdDiscoveryService struct {
|
||||
mapDiscoveryNodeId map[string]map[string]struct{} //map[networkName]map[nodeId]
|
||||
}
|
||||
|
||||
func getEtcdDiscovery() (IServiceDiscovery) {
|
||||
func getEtcdDiscovery() IServiceDiscovery {
|
||||
etcdDiscovery := &EtcdDiscoveryService{}
|
||||
return etcdDiscovery
|
||||
}
|
||||
@@ -168,7 +166,6 @@ func (ed *EtcdDiscoveryService) registerServiceByClient(client *clientv3.Client,
|
||||
}()
|
||||
}
|
||||
|
||||
|
||||
func (ed *EtcdDiscoveryService) tryRegisterService(client *clientv3.Client, etcdClient *etcdClientInfo) {
|
||||
if ed.isStop() {
|
||||
return
|
||||
@@ -230,10 +227,10 @@ func (ed *EtcdDiscoveryService) isStop() bool{
|
||||
return atomic.LoadInt32(&ed.isClose) == 1
|
||||
}
|
||||
|
||||
func (nd *EtcdDiscoveryService) OnStart() {
|
||||
for c, ec := range nd.mapClient {
|
||||
nd.tryRegisterService(c,ec)
|
||||
nd.tryWatch(c,ec)
|
||||
func (ed *EtcdDiscoveryService) OnStart() {
|
||||
for c, ec := range ed.mapClient {
|
||||
ed.tryRegisterService(c, ec)
|
||||
ed.tryWatch(c, ec)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package cluster
|
||||
|
||||
import (
|
||||
"time"
|
||||
"container/list"
|
||||
"time"
|
||||
)
|
||||
|
||||
type nodeTTL struct {
|
||||
@@ -46,7 +46,7 @@ func (ns *nodeSetTTL) addAndRefreshNode(nodeId string){
|
||||
}
|
||||
|
||||
func (ns *nodeSetTTL) checkTTL(cb func(nodeIdList []string)) {
|
||||
nodeIdList := []string{}
|
||||
var nodeIdList []string
|
||||
for {
|
||||
f := ns.l.Front()
|
||||
if f == nil {
|
||||
|
||||
@@ -14,7 +14,6 @@ const OriginDiscoveryMasterName = "DiscoveryMaster"
|
||||
const OriginDiscoveryClientName = "DiscoveryClient"
|
||||
const RegServiceDiscover = OriginDiscoveryMasterName + ".RPC_RegServiceDiscover"
|
||||
const SubServiceDiscover = OriginDiscoveryClientName + ".RPC_SubServiceDiscover"
|
||||
const AddSubServiceDiscover = OriginDiscoveryMasterName + ".RPC_AddSubServiceDiscover"
|
||||
const NodeRetireRpcMethod = OriginDiscoveryMasterName + ".RPC_NodeRetire"
|
||||
const RpcPingMethod = OriginDiscoveryMasterName + ".RPC_Ping"
|
||||
const UnRegServiceDiscover = OriginDiscoveryMasterName + ".RPC_UnRegServiceDiscover"
|
||||
@@ -147,19 +146,25 @@ func (ds *OriginDiscoveryMaster) OnStart() {
|
||||
ds.checkTTL()
|
||||
}
|
||||
|
||||
func (dc *OriginDiscoveryMaster) OnNatsConnected(){
|
||||
func (ds *OriginDiscoveryMaster) OnNatsConnected() {
|
||||
//向所有的节点同步服务发现信息
|
||||
var notifyDiscover rpc.SubscribeDiscoverNotify
|
||||
notifyDiscover.IsFull = true
|
||||
notifyDiscover.NodeInfo = dc.nodeInfo
|
||||
notifyDiscover.NodeInfo = ds.nodeInfo
|
||||
notifyDiscover.MasterNodeId = cluster.GetLocalNodeInfo().NodeId
|
||||
dc.RpcCastGo(SubServiceDiscover, ¬ifyDiscover)
|
||||
ds.RpcCastGo(SubServiceDiscover, ¬ifyDiscover)
|
||||
}
|
||||
|
||||
func (dc *OriginDiscoveryMaster) OnNatsDisconnect(){
|
||||
func (ds *OriginDiscoveryMaster) OnNatsDisconnect() {
|
||||
}
|
||||
|
||||
func (ds *OriginDiscoveryMaster) OnNodeConnected(nodeId string) {
|
||||
var notifyDiscover rpc.SubscribeDiscoverNotify
|
||||
notifyDiscover.IsFull = true
|
||||
notifyDiscover.NodeInfo = ds.nodeInfo
|
||||
notifyDiscover.MasterNodeId = cluster.GetLocalNodeInfo().NodeId
|
||||
|
||||
ds.GoNode(nodeId, SubServiceDiscover, ¬ifyDiscover)
|
||||
}
|
||||
|
||||
func (ds *OriginDiscoveryMaster) OnNodeDisconnect(nodeId string) {
|
||||
@@ -182,7 +187,11 @@ func (ds *OriginDiscoveryMaster) OnNodeDisconnect(nodeId string) {
|
||||
}
|
||||
|
||||
func (ds *OriginDiscoveryMaster) RpcCastGo(serviceMethod string, args interface{}) {
|
||||
for nodeId, _ := range ds.mapNodeInfo {
|
||||
for nodeId := range ds.mapNodeInfo {
|
||||
if nodeId == cluster.GetLocalNodeInfo().NodeId {
|
||||
continue
|
||||
}
|
||||
|
||||
ds.GoNode(nodeId, serviceMethod, args)
|
||||
}
|
||||
}
|
||||
@@ -198,7 +207,7 @@ func (ds *OriginDiscoveryMaster) RPC_Ping(req *rpc.Ping, res *rpc.Pong) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ds *OriginDiscoveryMaster) RPC_NodeRetire(req *rpc.NodeRetireReq, res *rpc.Empty) error {
|
||||
func (ds *OriginDiscoveryMaster) RPC_NodeRetire(req *rpc.NodeRetireReq, _ *rpc.Empty) error {
|
||||
log.Info("node is retire", log.String("nodeId", req.NodeInfo.NodeId), log.Bool("retire", req.NodeInfo.Retire))
|
||||
|
||||
ds.updateNodeInfo(req.NodeInfo)
|
||||
@@ -249,14 +258,13 @@ func (ds *OriginDiscoveryMaster) RPC_RegServiceDiscover(req *rpc.RegServiceDisco
|
||||
//加入到本地Cluster模块中,将连接该结点
|
||||
cluster.serviceDiscoverySetNodeInfo(&nodeInfo)
|
||||
|
||||
|
||||
res.IsFull = true
|
||||
res.NodeInfo = ds.nodeInfo
|
||||
res.MasterNodeId = cluster.GetLocalNodeInfo().NodeId
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ds *OriginDiscoveryMaster) RPC_UnRegServiceDiscover(req *rpc.UnRegServiceDiscoverReq, res *rpc.Empty) error {
|
||||
func (ds *OriginDiscoveryMaster) RPC_UnRegServiceDiscover(req *rpc.UnRegServiceDiscoverReq, _ *rpc.Empty) error {
|
||||
log.Debug("RPC_UnRegServiceDiscover", log.String("nodeId", req.NodeId))
|
||||
ds.OnNodeDisconnect(req.NodeId)
|
||||
return nil
|
||||
@@ -340,7 +348,6 @@ func (dc *OriginDiscoveryClient) ping(){
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
func (dc *OriginDiscoveryClient) OnStart() {
|
||||
//2.添加并连接发现主结点
|
||||
dc.addDiscoveryMaster()
|
||||
@@ -371,8 +378,8 @@ func (dc *OriginDiscoveryClient) fullCompareDiffNode(masterNodeId string, mapNod
|
||||
}
|
||||
|
||||
//本地任何Master都不存在的,放到diffNodeIdSlice
|
||||
for nodeId, _ := range mapNodeId {
|
||||
_, ok := mapNodeInfo[nodeId]
|
||||
for nodeId := range mapNodeId {
|
||||
_, ok = mapNodeInfo[nodeId]
|
||||
if ok == false {
|
||||
diffNodeIdSlice = append(diffNodeIdSlice, nodeId)
|
||||
}
|
||||
@@ -381,7 +388,7 @@ func (dc *OriginDiscoveryClient) fullCompareDiffNode(masterNodeId string, mapNod
|
||||
return diffNodeIdSlice
|
||||
}
|
||||
|
||||
//订阅发现的服务通知
|
||||
// RPC_SubServiceDiscover 订阅发现的服务通知
|
||||
func (dc *OriginDiscoveryClient) RPC_SubServiceDiscover(req *rpc.SubscribeDiscoverNotify) error {
|
||||
mapNodeInfo := map[string]*rpc.NodeInfo{}
|
||||
for _, nodeInfo := range req.NodeInfo {
|
||||
@@ -606,7 +613,6 @@ func (cls *Cluster) AddDiscoveryService(serviceName string, bPublicService bool)
|
||||
cls.mapServiceNode[serviceName][cls.localNodeInfo.NodeId] = struct{}{}
|
||||
}
|
||||
|
||||
|
||||
func (cls *Cluster) IsOriginMasterDiscoveryNode(nodeId string) bool {
|
||||
return cls.getOriginMasterDiscoveryNodeInfo(nodeId) != nil
|
||||
}
|
||||
|
||||
@@ -1,15 +1,16 @@
|
||||
package cluster
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/duanhf2012/origin/v2/log"
|
||||
"github.com/duanhf2012/origin/v2/rpc"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
"gopkg.in/yaml.v3"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
"errors"
|
||||
)
|
||||
|
||||
var json = jsoniter.ConfigCompatibleWithStandardLibrary
|
||||
@@ -44,7 +45,7 @@ const MinTTL = 3
|
||||
type DiscoveryInfo struct {
|
||||
discoveryType DiscoveryType
|
||||
Etcd *EtcdDiscovery //etcd
|
||||
Origin *OriginDiscovery //orign
|
||||
Origin *OriginDiscovery //origin
|
||||
}
|
||||
|
||||
type NatsConfig struct {
|
||||
@@ -55,7 +56,6 @@ type NatsConfig struct {
|
||||
type RpcMode struct {
|
||||
Typ string `json:"Type"`
|
||||
Nats NatsConfig
|
||||
|
||||
}
|
||||
|
||||
type NodeInfoList struct {
|
||||
@@ -64,6 +64,41 @@ type NodeInfoList struct {
|
||||
NodeList []NodeInfo
|
||||
}
|
||||
|
||||
func validConfigFile(f os.DirEntry) bool {
|
||||
if f.IsDir() == true || (filepath.Ext(f.Name()) != ".json" && filepath.Ext(f.Name()) != ".yml" && filepath.Ext(f.Name()) != ".yaml") {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func yamlToJson(data []byte, v interface{}) ([]byte, error) {
|
||||
mapKeyData := map[string]interface{}{}
|
||||
err := yaml.Unmarshal(data, &mapKeyData)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
data, err = json.Marshal(mapKeyData)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return data, nil
|
||||
}
|
||||
|
||||
func unmarshalConfig(data []byte, v interface{}) error {
|
||||
if !json.Valid(data) {
|
||||
var err error
|
||||
data, err = yamlToJson(data, v)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return json.Unmarshal(data, v)
|
||||
}
|
||||
|
||||
func (d *DiscoveryInfo) getDiscoveryType() DiscoveryType {
|
||||
return d.discoveryType
|
||||
}
|
||||
@@ -89,7 +124,7 @@ func (d *DiscoveryInfo) setEtcd(etcd *EtcdDiscovery) error{
|
||||
}
|
||||
|
||||
if d.discoveryType != InvalidType {
|
||||
return fmt.Errorf("Repeat configuration of Discovery")
|
||||
return fmt.Errorf("repeat configuration of Discovery")
|
||||
}
|
||||
|
||||
//Endpoints不允许重复
|
||||
@@ -130,7 +165,7 @@ func (d *DiscoveryInfo) setOrigin(originDiscovery *OriginDiscovery) error{
|
||||
}
|
||||
|
||||
if d.discoveryType != InvalidType {
|
||||
return fmt.Errorf("Repeat configuration of Discovery")
|
||||
return fmt.Errorf("repeat configuration of Discovery")
|
||||
}
|
||||
|
||||
mapListenAddr := make(map[string]struct{})
|
||||
@@ -161,7 +196,7 @@ func (cls *Cluster) ReadClusterConfig(filepath string) (*NodeInfoList, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err = json.Unmarshal(d, c)
|
||||
err = unmarshalConfig(d, c)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -176,7 +211,7 @@ func (cls *Cluster) readServiceConfig(filepath string) (interface{}, map[string]
|
||||
if err != nil {
|
||||
return nil, nil, nil, err
|
||||
}
|
||||
err = json.Unmarshal(d, &c)
|
||||
err = unmarshalConfig(d, &c)
|
||||
if err != nil {
|
||||
return nil, nil, nil, err
|
||||
}
|
||||
@@ -238,17 +273,19 @@ func (cls *Cluster) readLocalClusterConfig(nodeId string) (DiscoveryInfo, []Node
|
||||
clusterCfgPath := strings.TrimRight(configDir, "/") + "/cluster"
|
||||
fileInfoList, err := os.ReadDir(clusterCfgPath)
|
||||
if err != nil {
|
||||
return discoveryInfo, nil,rpcMode, fmt.Errorf("Read dir %s is fail :%+v", clusterCfgPath, err)
|
||||
return discoveryInfo, nil, rpcMode, fmt.Errorf("read dir %s is fail :%+v", clusterCfgPath, err)
|
||||
}
|
||||
|
||||
//读取任何文件,只读符合格式的配置,目录下的文件可以自定义分文件
|
||||
for _, f := range fileInfoList {
|
||||
if f.IsDir() == false {
|
||||
filePath := strings.TrimRight(strings.TrimRight(clusterCfgPath, "/"), "\\") + "/" + f.Name()
|
||||
fileNodeInfoList, rerr := cls.ReadClusterConfig(filePath)
|
||||
if !validConfigFile(f) {
|
||||
continue
|
||||
}
|
||||
|
||||
if rerr != nil {
|
||||
return discoveryInfo, nil,rpcMode, fmt.Errorf("read file path %s is error:%+v", filePath, rerr)
|
||||
filePath := strings.TrimRight(strings.TrimRight(clusterCfgPath, "/"), "\\") + "/" + f.Name()
|
||||
fileNodeInfoList, rErr := cls.ReadClusterConfig(filePath)
|
||||
if rErr != nil {
|
||||
return discoveryInfo, nil, rpcMode, fmt.Errorf("read file path %s is error:%+v", filePath, rErr)
|
||||
}
|
||||
|
||||
err = cls.SetRpcMode(&fileNodeInfoList.RpcMode, &rpcMode)
|
||||
@@ -267,13 +304,12 @@ func (cls *Cluster) readLocalClusterConfig(nodeId string) (DiscoveryInfo, []Node
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if nodeId != rpc.NodeIdNull && (len(nodeInfoList) != 1) {
|
||||
return discoveryInfo, nil,rpcMode, fmt.Errorf("%d configurations were found for the configuration with node ID %d!", len(nodeInfoList), nodeId)
|
||||
return discoveryInfo, nil, rpcMode, fmt.Errorf("nodeid %s configuration error in NodeList", nodeId)
|
||||
}
|
||||
|
||||
for i, _ := range nodeInfoList {
|
||||
for i := range nodeInfoList {
|
||||
for j, s := range nodeInfoList[i].ServiceList {
|
||||
//私有结点不加入到Public服务列表中
|
||||
if strings.HasPrefix(s, "_") == false && nodeInfoList[i].Private == false {
|
||||
@@ -291,7 +327,7 @@ func (cls *Cluster) readLocalService(localNodeId string) error {
|
||||
clusterCfgPath := strings.TrimRight(configDir, "/") + "/cluster"
|
||||
fileInfoList, err := os.ReadDir(clusterCfgPath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Read dir %s is fail :%+v", clusterCfgPath, err)
|
||||
return fmt.Errorf("read dir %s is fail :%+v", clusterCfgPath, err)
|
||||
}
|
||||
|
||||
var globalCfg interface{}
|
||||
@@ -300,11 +336,7 @@ func (cls *Cluster) readLocalService(localNodeId string) error {
|
||||
|
||||
//读取任何文件,只读符合格式的配置,目录下的文件可以自定义分文件
|
||||
for _, f := range fileInfoList {
|
||||
if f.IsDir() == true {
|
||||
continue
|
||||
}
|
||||
|
||||
if filepath.Ext(f.Name())!= ".json" {
|
||||
if !validConfigFile(f) {
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -317,7 +349,7 @@ func (cls *Cluster) readLocalService(localNodeId string) error {
|
||||
if currGlobalCfg != nil {
|
||||
//不允许重复的配置global配置
|
||||
if globalCfg != nil {
|
||||
return fmt.Errorf("[Global] does not allow repeated configuration in %s.",f.Name())
|
||||
return fmt.Errorf("[Global] does not allow repeated configuration in %s", f.Name())
|
||||
}
|
||||
globalCfg = currGlobalCfg
|
||||
}
|
||||
@@ -325,11 +357,15 @@ func (cls *Cluster) readLocalService(localNodeId string) error {
|
||||
//保存公共配置
|
||||
for _, s := range cls.localNodeInfo.ServiceList {
|
||||
for {
|
||||
splitServiceName := strings.Split(s, ":")
|
||||
if len(splitServiceName) == 2 {
|
||||
s = splitServiceName[0]
|
||||
}
|
||||
//取公共服务配置
|
||||
pubCfg, ok := serviceConfig[s]
|
||||
if ok == true {
|
||||
if _, publicOk := publicService[s]; publicOk == true {
|
||||
return fmt.Errorf("public service [%s] does not allow repeated configuration in %s.",s,f.Name())
|
||||
return fmt.Errorf("public service [%s] does not allow repeated configuration in %s", s, f.Name())
|
||||
}
|
||||
publicService[s] = pubCfg
|
||||
}
|
||||
@@ -345,7 +381,7 @@ func (cls *Cluster) readLocalService(localNodeId string) error {
|
||||
}
|
||||
|
||||
if _, nodeOK := nodeService[s]; nodeOK == true {
|
||||
return fmt.Errorf("NodeService NodeId[%d] Service[%s] does not allow repeated configuration in %s.",cls.localNodeInfo.NodeId,s,f.Name())
|
||||
return fmt.Errorf("NodeService NodeId[%s] Service[%s] does not allow repeated configuration in %s", cls.localNodeInfo.NodeId, s, f.Name())
|
||||
}
|
||||
nodeService[s] = nodeCfg
|
||||
break
|
||||
@@ -355,6 +391,11 @@ func (cls *Cluster) readLocalService(localNodeId string) error {
|
||||
|
||||
//组合所有的配置
|
||||
for _, s := range cls.localNodeInfo.ServiceList {
|
||||
splitServiceName := strings.Split(s, ":")
|
||||
if len(splitServiceName) == 2 {
|
||||
s = splitServiceName[0]
|
||||
}
|
||||
|
||||
//先从NodeService中找
|
||||
var serviceCfg interface{}
|
||||
var ok bool
|
||||
@@ -382,12 +423,23 @@ func (cls *Cluster) parseLocalCfg() {
|
||||
|
||||
cls.mapRpc[cls.localNodeInfo.NodeId] = &rpcInfo
|
||||
|
||||
for _, sName := range cls.localNodeInfo.ServiceList {
|
||||
if _, ok := cls.mapServiceNode[sName]; ok == false {
|
||||
cls.mapServiceNode[sName] = make(map[string]struct{})
|
||||
for _, serviceName := range cls.localNodeInfo.ServiceList {
|
||||
splitServiceName := strings.Split(serviceName, ":")
|
||||
if len(splitServiceName) == 2 {
|
||||
serviceName = splitServiceName[0]
|
||||
templateServiceName := splitServiceName[1]
|
||||
//记录模板
|
||||
if _, ok := cls.mapTemplateServiceNode[templateServiceName]; ok == false {
|
||||
cls.mapTemplateServiceNode[templateServiceName] = map[string]struct{}{}
|
||||
}
|
||||
cls.mapTemplateServiceNode[templateServiceName][serviceName] = struct{}{}
|
||||
}
|
||||
|
||||
cls.mapServiceNode[sName][cls.localNodeInfo.NodeId] = struct{}{}
|
||||
if _, ok := cls.mapServiceNode[serviceName]; ok == false {
|
||||
cls.mapServiceNode[serviceName] = make(map[string]struct{})
|
||||
}
|
||||
|
||||
cls.mapServiceNode[serviceName][cls.localNodeInfo.NodeId] = struct{}{}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -403,6 +455,7 @@ func (cls *Cluster) InitCfg(localNodeId string) error {
|
||||
cls.localServiceCfg = map[string]interface{}{}
|
||||
cls.mapRpc = map[string]*NodeRpcInfo{}
|
||||
cls.mapServiceNode = map[string]map[string]struct{}{}
|
||||
cls.mapTemplateServiceNode = map[string]map[string]struct{}{}
|
||||
|
||||
//加载本地结点的NodeList配置
|
||||
discoveryInfo, nodeInfoList, rpcMode, err := cls.readLocalClusterConfig(localNodeId)
|
||||
@@ -436,14 +489,15 @@ func (cls *Cluster) IsConfigService(serviceName string) bool {
|
||||
return ok
|
||||
}
|
||||
|
||||
|
||||
func (cls *Cluster) GetNodeIdByService(serviceName string, rpcClientList []*rpc.Client, filterRetire bool) (error, int) {
|
||||
func (cls *Cluster) GetNodeIdByTemplateService(templateServiceName string, rpcClientList []*rpc.Client, filterRetire bool) (error, []*rpc.Client) {
|
||||
cls.locker.RLock()
|
||||
defer cls.locker.RUnlock()
|
||||
|
||||
mapServiceName := cls.mapTemplateServiceNode[templateServiceName]
|
||||
for serviceName := range mapServiceName {
|
||||
mapNodeId, ok := cls.mapServiceNode[serviceName]
|
||||
count := 0
|
||||
if ok == true {
|
||||
for nodeId, _ := range mapNodeId {
|
||||
for nodeId := range mapNodeId {
|
||||
pClient, retire := GetCluster().getRpcClient(nodeId)
|
||||
if pClient == nil || pClient.IsConnected() == false {
|
||||
continue
|
||||
@@ -454,15 +508,35 @@ func (cls *Cluster) GetNodeIdByService(serviceName string, rpcClientList []*rpc.
|
||||
continue
|
||||
}
|
||||
|
||||
rpcClientList[count] = pClient
|
||||
count++
|
||||
if count >= cap(rpcClientList) {
|
||||
break
|
||||
rpcClientList = append(rpcClientList, pClient)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil, count
|
||||
return nil, rpcClientList
|
||||
}
|
||||
|
||||
func (cls *Cluster) GetNodeIdByService(serviceName string, rpcClientList []*rpc.Client, filterRetire bool) (error, []*rpc.Client) {
|
||||
cls.locker.RLock()
|
||||
defer cls.locker.RUnlock()
|
||||
mapNodeId, ok := cls.mapServiceNode[serviceName]
|
||||
if ok == true {
|
||||
for nodeId := range mapNodeId {
|
||||
pClient, retire := GetCluster().getRpcClient(nodeId)
|
||||
if pClient == nil || pClient.IsConnected() == false {
|
||||
continue
|
||||
}
|
||||
|
||||
//如果需要筛选掉退休的,对retire状态的结点略过
|
||||
if filterRetire == true && retire == true {
|
||||
continue
|
||||
}
|
||||
|
||||
rpcClientList = append(rpcClientList, pClient)
|
||||
}
|
||||
}
|
||||
|
||||
return nil, rpcClientList
|
||||
}
|
||||
|
||||
func (cls *Cluster) GetServiceCfg(serviceName string) interface{} {
|
||||
|
||||
@@ -26,6 +26,8 @@ type Concurrent struct {
|
||||
}
|
||||
|
||||
/*
|
||||
OpenConcurrentByNumCPU 函数使用说明
|
||||
|
||||
cpuMul 表示cpu的倍数
|
||||
建议:(1)cpu密集型 使用1 (2)i/o密集型使用2或者更高
|
||||
*/
|
||||
|
||||
@@ -8,11 +8,13 @@ import (
|
||||
"fmt"
|
||||
"runtime"
|
||||
|
||||
"context"
|
||||
"github.com/duanhf2012/origin/v2/log"
|
||||
"github.com/duanhf2012/origin/v2/util/queue"
|
||||
)
|
||||
|
||||
var idleTimeout = int64(2 * time.Second)
|
||||
|
||||
const maxTaskQueueSessionId = 10000
|
||||
|
||||
type dispatch struct {
|
||||
@@ -30,6 +32,9 @@ type dispatch struct {
|
||||
|
||||
waitWorker sync.WaitGroup
|
||||
waitDispatch sync.WaitGroup
|
||||
|
||||
cancelContext context.Context
|
||||
cancel context.CancelFunc
|
||||
}
|
||||
|
||||
func (d *dispatch) open(minGoroutineNum int32, maxGoroutineNum int32, tasks chan task, cbChannel chan func(error)) {
|
||||
@@ -40,7 +45,7 @@ func (d *dispatch) open(minGoroutineNum int32, maxGoroutineNum int32, tasks chan
|
||||
d.workerQueue = make(chan task)
|
||||
d.cbChannel = cbChannel
|
||||
d.queueIdChannel = make(chan int64, cap(tasks))
|
||||
|
||||
d.cancelContext, d.cancel = context.WithCancel(context.Background())
|
||||
d.waitDispatch.Add(1)
|
||||
go d.run()
|
||||
}
|
||||
@@ -52,7 +57,7 @@ func (d *dispatch) run() {
|
||||
for {
|
||||
select {
|
||||
case queueId := <-d.queueIdChannel:
|
||||
d.processqueueEvent(queueId)
|
||||
d.processQueueEvent(queueId)
|
||||
default:
|
||||
select {
|
||||
case t, ok := <-d.tasks:
|
||||
@@ -61,13 +66,15 @@ func (d *dispatch) run() {
|
||||
}
|
||||
d.processTask(&t)
|
||||
case queueId := <-d.queueIdChannel:
|
||||
d.processqueueEvent(queueId)
|
||||
d.processQueueEvent(queueId)
|
||||
case <-timeout.C:
|
||||
d.processTimer()
|
||||
if atomic.LoadInt32(&d.minConcurrentNum) == -1 && len(d.tasks) == 0 {
|
||||
atomic.StoreInt64(&idleTimeout,int64(time.Millisecond * 10))
|
||||
}
|
||||
case <-d.cancelContext.Done():
|
||||
atomic.StoreInt64(&idleTimeout, int64(time.Millisecond*5))
|
||||
timeout.Reset(time.Duration(atomic.LoadInt64(&idleTimeout)))
|
||||
for i := int32(0); i < d.workerNum; i++ {
|
||||
d.processIdle()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -87,7 +94,7 @@ func (d *dispatch) processTimer() {
|
||||
d.idle = true
|
||||
}
|
||||
|
||||
func (d *dispatch) processqueueEvent(queueId int64) {
|
||||
func (d *dispatch) processQueueEvent(queueId int64) {
|
||||
d.idle = false
|
||||
|
||||
queueSession := d.mapTaskQueueSession[queueId]
|
||||
@@ -155,17 +162,18 @@ func (d *dispatch) pushQueueTaskFinishEvent(queueId int64) {
|
||||
d.queueIdChannel <- queueId
|
||||
}
|
||||
|
||||
func (c *dispatch) pushAsyncDoCallbackEvent(cb func(err error)) {
|
||||
func (d *dispatch) pushAsyncDoCallbackEvent(cb func(err error)) {
|
||||
if cb == nil {
|
||||
//不需要回调的情况
|
||||
return
|
||||
}
|
||||
|
||||
c.cbChannel <- cb
|
||||
d.cbChannel <- cb
|
||||
}
|
||||
|
||||
func (d *dispatch) close() {
|
||||
atomic.StoreInt32(&d.minConcurrentNum, -1)
|
||||
d.cancel()
|
||||
|
||||
breakFor:
|
||||
for {
|
||||
|
||||
@@ -67,8 +67,8 @@ func (w *worker) exec(t *task) {
|
||||
w.endCallFun(t.fn(), t)
|
||||
}
|
||||
|
||||
func (w *worker) endCallFun(isDocallBack bool,t *task) {
|
||||
if isDocallBack {
|
||||
func (w *worker) endCallFun(isDoCallBack bool, t *task) {
|
||||
if isDoCallBack {
|
||||
w.pushAsyncDoCallbackEvent(t.cb)
|
||||
}
|
||||
|
||||
|
||||
@@ -8,8 +8,10 @@ import (
|
||||
|
||||
type valueType int
|
||||
type CommandFunctionCB func(args interface{}) error
|
||||
|
||||
var commandList []*command
|
||||
var programName string
|
||||
|
||||
const (
|
||||
boolType valueType = 0
|
||||
stringType valueType = 1
|
||||
@@ -33,11 +35,9 @@ func (cmd *command) execute() error{
|
||||
return cmd.fn(cmd.strValue)
|
||||
} else if cmd.valType == intType {
|
||||
return cmd.fn(cmd.intValue)
|
||||
}else{
|
||||
return fmt.Errorf("Unknow command type.")
|
||||
}
|
||||
|
||||
return nil
|
||||
return fmt.Errorf("unknow command type")
|
||||
}
|
||||
|
||||
func Run(args []string) error {
|
||||
|
||||
@@ -7,7 +7,7 @@ import (
|
||||
"sync"
|
||||
)
|
||||
|
||||
//事件接受器
|
||||
// EventCallBack 事件接受器
|
||||
type EventCallBack func(event IEvent)
|
||||
|
||||
type IEvent interface {
|
||||
@@ -17,10 +17,14 @@ type IEvent interface {
|
||||
type Event struct {
|
||||
Type EventType
|
||||
Data interface{}
|
||||
IntExt [2]int64
|
||||
StringExt [2]string
|
||||
AnyExt [2]any
|
||||
ref bool
|
||||
}
|
||||
|
||||
var emptyEvent Event
|
||||
|
||||
func (e *Event) Reset() {
|
||||
*e = emptyEvent
|
||||
}
|
||||
@@ -134,7 +138,6 @@ func (handler *EventHandler) Init(processor IEventProcessor){
|
||||
handler.mapRegEvent = map[EventType]map[IEventProcessor]interface{}{}
|
||||
}
|
||||
|
||||
|
||||
func (processor *EventProcessor) Init(eventChannel IEventChannel) {
|
||||
processor.IEventChannel = eventChannel
|
||||
}
|
||||
@@ -236,7 +239,6 @@ func (processor *EventProcessor) castEvent(event IEvent){
|
||||
|
||||
eventProcessor, ok := processor.mapListenerEvent[event.GetEventType()]
|
||||
if ok == false || processor == nil {
|
||||
log.Debug("event is not listen",log.Int("event type",int(event.GetEventType())))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -244,4 +246,3 @@ func (processor *EventProcessor) castEvent(event IEvent){
|
||||
proc.PushEvent(event)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -10,14 +10,14 @@ const (
|
||||
Sys_Event_Tcp EventType = -3
|
||||
Sys_Event_Http_Event EventType = -4
|
||||
Sys_Event_WebSocket EventType = -5
|
||||
Sys_Event_Node_Conn_Event EventType = -6
|
||||
Sys_Event_Nats_Conn_Event EventType = -7
|
||||
Sys_Event_DiscoverService EventType = -8
|
||||
Sys_Event_DiscardGoroutine EventType = -9
|
||||
Sys_Event_QueueTaskFinish EventType = -10
|
||||
Sys_Event_Retire EventType = -11
|
||||
Sys_Event_EtcdDiscovery EventType = -12
|
||||
Sys_Event_Kcp EventType = -6
|
||||
Sys_Event_Node_Conn_Event EventType = -7
|
||||
Sys_Event_Nats_Conn_Event EventType = -8
|
||||
Sys_Event_DiscoverService EventType = -9
|
||||
Sys_Event_Retire EventType = -10
|
||||
Sys_Event_EtcdDiscovery EventType = -11
|
||||
Sys_Event_Gin_Event EventType = -12
|
||||
Sys_Event_FrameTick EventType = -13
|
||||
|
||||
Sys_Event_User_Define EventType = 1
|
||||
)
|
||||
|
||||
|
||||
64
go.mod
64
go.mod
@@ -1,40 +1,79 @@
|
||||
module github.com/duanhf2012/origin/v2
|
||||
|
||||
go 1.21
|
||||
go 1.22
|
||||
|
||||
toolchain go1.22.7
|
||||
|
||||
require (
|
||||
github.com/IBM/sarama v1.43.3
|
||||
github.com/gin-gonic/gin v1.10.0
|
||||
github.com/go-sql-driver/mysql v1.6.0
|
||||
github.com/gomodule/redigo v1.8.8
|
||||
github.com/google/uuid v1.6.0
|
||||
github.com/gorilla/websocket v1.5.0
|
||||
github.com/json-iterator/go v1.1.12
|
||||
github.com/nats-io/nats.go v1.34.1
|
||||
github.com/pierrec/lz4/v4 v4.1.18
|
||||
github.com/pierrec/lz4/v4 v4.1.21
|
||||
github.com/shirou/gopsutil v3.21.11+incompatible
|
||||
github.com/xtaci/kcp-go/v5 v5.6.18
|
||||
go.etcd.io/etcd/api/v3 v3.5.13
|
||||
go.etcd.io/etcd/client/v3 v3.5.13
|
||||
go.mongodb.org/mongo-driver v1.9.1
|
||||
go.uber.org/zap v1.27.0
|
||||
google.golang.org/protobuf v1.33.0
|
||||
gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22
|
||||
google.golang.org/protobuf v1.34.1
|
||||
gopkg.in/yaml.v3 v3.0.1
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/bytedance/sonic v1.11.6 // indirect
|
||||
github.com/bytedance/sonic/loader v0.1.1 // indirect
|
||||
github.com/cloudwego/base64x v0.1.4 // indirect
|
||||
github.com/cloudwego/iasm v0.2.0 // indirect
|
||||
github.com/coreos/go-semver v0.3.0 // indirect
|
||||
github.com/coreos/go-systemd/v22 v22.3.2 // indirect
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
github.com/eapache/go-resiliency v1.7.0 // indirect
|
||||
github.com/eapache/go-xerial-snappy v0.0.0-20230731223053-c322873962e3 // indirect
|
||||
github.com/eapache/queue v1.1.0 // indirect
|
||||
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
|
||||
github.com/gin-contrib/sse v0.1.0 // indirect
|
||||
github.com/go-ole/go-ole v1.2.6 // indirect
|
||||
github.com/go-playground/locales v0.14.1 // indirect
|
||||
github.com/go-playground/universal-translator v0.18.1 // indirect
|
||||
github.com/go-playground/validator/v10 v10.20.0 // indirect
|
||||
github.com/go-stack/stack v1.8.0 // indirect
|
||||
github.com/goccy/go-json v0.10.2 // indirect
|
||||
github.com/gogo/protobuf v1.3.2 // indirect
|
||||
github.com/golang/protobuf v1.5.4 // indirect
|
||||
github.com/golang/snappy v0.0.1 // indirect
|
||||
github.com/klauspost/compress v1.17.2 // indirect
|
||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
|
||||
github.com/golang/snappy v0.0.4 // indirect
|
||||
github.com/hashicorp/errwrap v1.0.0 // indirect
|
||||
github.com/hashicorp/go-multierror v1.1.1 // indirect
|
||||
github.com/hashicorp/go-uuid v1.0.3 // indirect
|
||||
github.com/jcmturner/aescts/v2 v2.0.0 // indirect
|
||||
github.com/jcmturner/dnsutils/v2 v2.0.0 // indirect
|
||||
github.com/jcmturner/gofork v1.7.6 // indirect
|
||||
github.com/jcmturner/gokrb5/v8 v8.4.4 // indirect
|
||||
github.com/jcmturner/rpc/v2 v2.0.3 // indirect
|
||||
github.com/klauspost/compress v1.17.9 // indirect
|
||||
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
|
||||
github.com/klauspost/reedsolomon v1.12.0 // indirect
|
||||
github.com/leodido/go-urn v1.4.0 // indirect
|
||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
||||
github.com/modern-go/reflect2 v1.0.2 // indirect
|
||||
github.com/nats-io/nkeys v0.4.7 // indirect
|
||||
github.com/nats-io/nuid v1.0.1 // indirect
|
||||
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
|
||||
github.com/pkg/errors v0.9.1 // indirect
|
||||
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 // indirect
|
||||
github.com/rogpeppe/go-internal v1.13.1 // indirect
|
||||
github.com/templexxx/cpu v0.1.1 // indirect
|
||||
github.com/templexxx/xorsimd v0.4.3 // indirect
|
||||
github.com/tjfoc/gmsm v1.4.1 // indirect
|
||||
github.com/tklauser/go-sysconf v0.3.13 // indirect
|
||||
github.com/tklauser/numcpus v0.7.0 // indirect
|
||||
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
|
||||
github.com/ugorji/go/codec v1.2.12 // indirect
|
||||
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
|
||||
github.com/xdg-go/scram v1.0.2 // indirect
|
||||
github.com/xdg-go/stringprep v1.0.2 // indirect
|
||||
@@ -42,11 +81,12 @@ require (
|
||||
github.com/yusufpapurcu/wmi v1.2.4 // indirect
|
||||
go.etcd.io/etcd/client/pkg/v3 v3.5.13 // indirect
|
||||
go.uber.org/multierr v1.10.0 // indirect
|
||||
golang.org/x/crypto v0.18.0 // indirect
|
||||
golang.org/x/net v0.17.0 // indirect
|
||||
golang.org/x/sync v0.3.0 // indirect
|
||||
golang.org/x/sys v0.16.0 // indirect
|
||||
golang.org/x/text v0.14.0 // indirect
|
||||
golang.org/x/arch v0.8.0 // indirect
|
||||
golang.org/x/crypto v0.26.0 // indirect
|
||||
golang.org/x/net v0.28.0 // indirect
|
||||
golang.org/x/sync v0.8.0 // indirect
|
||||
golang.org/x/sys v0.23.0 // indirect
|
||||
golang.org/x/text v0.17.0 // indirect
|
||||
google.golang.org/genproto v0.0.0-20230822172742-b8732ec3820d // indirect
|
||||
google.golang.org/genproto/googleapis/api v0.0.0-20230822172742-b8732ec3820d // indirect
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d // indirect
|
||||
|
||||
226
go.sum
226
go.sum
@@ -1,3 +1,18 @@
|
||||
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
|
||||
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
|
||||
github.com/IBM/sarama v1.43.3 h1:Yj6L2IaNvb2mRBop39N7mmJAHBVY3dTPncr3qGVkxPA=
|
||||
github.com/IBM/sarama v1.43.3/go.mod h1:FVIRaLrhK3Cla/9FfRF5X9Zua2KpS3SYIXxhac1H+FQ=
|
||||
github.com/bytedance/sonic v1.11.6 h1:oUp34TzMlL+OY1OUWxHqsdkgC/Zfc85zGqw9siXjrc0=
|
||||
github.com/bytedance/sonic v1.11.6/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4=
|
||||
github.com/bytedance/sonic/loader v0.1.1 h1:c+e5Pt1k/cy5wMveRDyk2X4B9hF4g7an8N3zCYjJFNM=
|
||||
github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU=
|
||||
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
|
||||
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
|
||||
github.com/cloudwego/base64x v0.1.4 h1:jwCgWpFanWmN8xoIUHa2rtzmkd5J2plF/dnLS6Xd/0Y=
|
||||
github.com/cloudwego/base64x v0.1.4/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w=
|
||||
github.com/cloudwego/iasm v0.2.0 h1:1KNIy1I1H9hNNFEEH3DVnI4UujN+1zjpuk6gwHLTssg=
|
||||
github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQPiEFhY=
|
||||
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
|
||||
github.com/coreos/go-semver v0.3.0 h1:wkHLiw0WNATZnSG7epLsujiMCgPAc9xhjJ4tgnAxmfM=
|
||||
github.com/coreos/go-semver v0.3.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
|
||||
github.com/coreos/go-systemd/v22 v22.3.2 h1:D9/bQk5vlXQFZ6Kwuu6zaiXJ9oTPe68++AzAJc1DzSI=
|
||||
@@ -5,44 +20,120 @@ github.com/coreos/go-systemd/v22 v22.3.2/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSV
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/eapache/go-resiliency v1.7.0 h1:n3NRTnBn5N0Cbi/IeOHuQn9s2UwVUH7Ga0ZWcP+9JTA=
|
||||
github.com/eapache/go-resiliency v1.7.0/go.mod h1:5yPzW0MIvSe0JDsv0v+DvcjEv2FyD6iZYSs1ZI+iQho=
|
||||
github.com/eapache/go-xerial-snappy v0.0.0-20230731223053-c322873962e3 h1:Oy0F4ALJ04o5Qqpdz8XLIpNA3WM/iSIXqxtqo7UGVws=
|
||||
github.com/eapache/go-xerial-snappy v0.0.0-20230731223053-c322873962e3/go.mod h1:YvSRo5mw33fLEx1+DlK6L2VV43tJt5Eyel9n9XBcR+0=
|
||||
github.com/eapache/queue v1.1.0 h1:YOEu7KNc61ntiQlcEeUIoDTJ2o8mQznoNvUhiigpIqc=
|
||||
github.com/eapache/queue v1.1.0/go.mod h1:6eCeP0CKFpHLu8blIFXhExK/dRa7WDZfr6jVFPTqq+I=
|
||||
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
|
||||
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
|
||||
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
|
||||
github.com/fortytw2/leaktest v1.3.0 h1:u8491cBMTQ8ft8aeV+adlcytMZylmA5nnwwkRZjI8vw=
|
||||
github.com/fortytw2/leaktest v1.3.0/go.mod h1:jDsjWgpAGjm2CA7WthBh/CdZYEPF31XHquHwclZch5g=
|
||||
github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0=
|
||||
github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk=
|
||||
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
|
||||
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
|
||||
github.com/gin-gonic/gin v1.10.0 h1:nTuyha1TYqgedzytsKYqna+DfLos46nTv2ygFy86HFU=
|
||||
github.com/gin-gonic/gin v1.10.0/go.mod h1:4PMNQiOhvDRa013RKVbsiNwoyezlm2rm0uX/T7kzp5Y=
|
||||
github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY=
|
||||
github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0=
|
||||
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
|
||||
github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
|
||||
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
|
||||
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
|
||||
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
|
||||
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
|
||||
github.com/go-playground/validator/v10 v10.20.0 h1:K9ISHbSaI0lyB2eWMPJo+kOS/FBExVwjEviJTixqxL8=
|
||||
github.com/go-playground/validator/v10 v10.20.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM=
|
||||
github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE=
|
||||
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
|
||||
github.com/go-stack/stack v1.8.0 h1:5SgMzNM5HxrEjV0ww2lTmX6E2Izsfxas4+YHWRs3Lsk=
|
||||
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
|
||||
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
|
||||
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
|
||||
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
|
||||
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
|
||||
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
|
||||
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
|
||||
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
|
||||
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
||||
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
||||
github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw=
|
||||
github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8=
|
||||
github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA=
|
||||
github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs=
|
||||
github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w=
|
||||
github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0=
|
||||
github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI=
|
||||
github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek=
|
||||
github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
|
||||
github.com/golang/snappy v0.0.1 h1:Qgr9rKW7uDUkrbSmQeiDsGa8SjGyCOGtuasMWwvp2P4=
|
||||
github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
|
||||
github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM=
|
||||
github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
|
||||
github.com/gomodule/redigo v1.8.8 h1:f6cXq6RRfiyrOJEV7p3JhLDlmawGBVBBP1MggY8Mo4E=
|
||||
github.com/gomodule/redigo v1.8.8/go.mod h1:7ArFNvsTjH8GMMzB4uy1snslv2BwmginuMs06a1uzZE=
|
||||
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
|
||||
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
|
||||
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
|
||||
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
|
||||
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/gorilla/securecookie v1.1.1/go.mod h1:ra0sb63/xPlUeL+yeDciTfxMRAA+MP+HVt/4epWDjd4=
|
||||
github.com/gorilla/sessions v1.2.1/go.mod h1:dk2InVEVJ0sfLlnXv9EAgkf6ecYs/i80K/zI+bUmuGM=
|
||||
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
|
||||
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||
github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA=
|
||||
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
|
||||
github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo=
|
||||
github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM=
|
||||
github.com/hashicorp/go-uuid v1.0.2/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
|
||||
github.com/hashicorp/go-uuid v1.0.3 h1:2gKiV6YVmrJ1i2CKKa9obLvRieoRGviZFL26PcT/Co8=
|
||||
github.com/hashicorp/go-uuid v1.0.3/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
|
||||
github.com/jcmturner/aescts/v2 v2.0.0 h1:9YKLH6ey7H4eDBXW8khjYslgyqG2xZikXP0EQFKrle8=
|
||||
github.com/jcmturner/aescts/v2 v2.0.0/go.mod h1:AiaICIRyfYg35RUkr8yESTqvSy7csK90qZ5xfvvsoNs=
|
||||
github.com/jcmturner/dnsutils/v2 v2.0.0 h1:lltnkeZGL0wILNvrNiVCR6Ro5PGU/SeBvVO/8c/iPbo=
|
||||
github.com/jcmturner/dnsutils/v2 v2.0.0/go.mod h1:b0TnjGOvI/n42bZa+hmXL+kFJZsFT7G4t3HTlQ184QM=
|
||||
github.com/jcmturner/gofork v1.7.6 h1:QH0l3hzAU1tfT3rZCnW5zXl+orbkNMMRGJfdJjHVETg=
|
||||
github.com/jcmturner/gofork v1.7.6/go.mod h1:1622LH6i/EZqLloHfE7IeZ0uEJwMSUyQ/nDd82IeqRo=
|
||||
github.com/jcmturner/goidentity/v6 v6.0.1 h1:VKnZd2oEIMorCTsFBnJWbExfNN7yZr3EhJAxwOkZg6o=
|
||||
github.com/jcmturner/goidentity/v6 v6.0.1/go.mod h1:X1YW3bgtvwAXju7V3LCIMpY0Gbxyjn/mY9zx4tFonSg=
|
||||
github.com/jcmturner/gokrb5/v8 v8.4.4 h1:x1Sv4HaTpepFkXbt2IkL29DXRf8sOfZXo8eRKh687T8=
|
||||
github.com/jcmturner/gokrb5/v8 v8.4.4/go.mod h1:1btQEpgT6k+unzCwX1KdWMEwPPkkgBtP+F6aCACiMrs=
|
||||
github.com/jcmturner/rpc/v2 v2.0.3 h1:7FXXj8Ti1IaVFpSAziCZWNzbNuZmnvw/i6CqLNdWfZY=
|
||||
github.com/jcmturner/rpc/v2 v2.0.3/go.mod h1:VUJYCIDm3PVOEHw8sgt091/20OJjskO/YJki3ELg/Hc=
|
||||
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
|
||||
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
|
||||
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
|
||||
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
|
||||
github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
|
||||
github.com/klauspost/compress v1.17.2 h1:RlWWUY/Dr4fL8qk9YG7DTZ7PDgME2V4csBXA8L/ixi4=
|
||||
github.com/klauspost/compress v1.17.2/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE=
|
||||
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
|
||||
github.com/klauspost/compress v1.17.9 h1:6KIumPrER1LHsvBVuDa0r5xaG0Es51mhhB9BQB2qeMA=
|
||||
github.com/klauspost/compress v1.17.9/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
|
||||
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
|
||||
github.com/klauspost/cpuid/v2 v2.2.7 h1:ZWSB3igEs+d0qvnxR/ZBzXVmxkgt8DdzP6m9pfuVLDM=
|
||||
github.com/klauspost/cpuid/v2 v2.2.7/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws=
|
||||
github.com/klauspost/reedsolomon v1.12.0 h1:I5FEp3xSwVCcEh3F5A7dofEfhXdF/bWhQWPH+XwBFno=
|
||||
github.com/klauspost/reedsolomon v1.12.0/go.mod h1:EPLZJeh4l27pUGC3aXOjheaoh1I9yut7xTURiW3LQ9Y=
|
||||
github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M=
|
||||
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
|
||||
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
|
||||
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
|
||||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
||||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
||||
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
|
||||
github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
|
||||
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
|
||||
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
||||
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
|
||||
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
|
||||
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe/go.mod h1:wL8QJuTMNUDYhXwkmfOly8iTdp5TEcJFWZD2D7SIkUc=
|
||||
@@ -52,36 +143,66 @@ github.com/nats-io/nkeys v0.4.7 h1:RwNJbbIdYCoClSDNY7QVKZlyb/wfT6ugvFCiKy6vDvI=
|
||||
github.com/nats-io/nkeys v0.4.7/go.mod h1:kqXRgRDPlGy7nGaEDMuYzmiJCIAAWDK0IMBtDmGD0nc=
|
||||
github.com/nats-io/nuid v1.0.1 h1:5iA8DT8V7q8WK2EScv2padNa/rTESc1KdnPw4TC2paw=
|
||||
github.com/nats-io/nuid v1.0.1/go.mod h1:19wcPz3Ph3q0Jbyiqsd0kePYG7A95tJPxeL+1OSON2c=
|
||||
github.com/pierrec/lz4/v4 v4.1.18 h1:xaKrnTkyoqfh1YItXl56+6KJNVYWlEEPuAQW9xsplYQ=
|
||||
github.com/pierrec/lz4/v4 v4.1.18/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4=
|
||||
github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM=
|
||||
github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs=
|
||||
github.com/pierrec/lz4/v4 v4.1.21 h1:yOVMLb6qSIDP67pl/5F7RepeKYu/VmTyEXvuMI5d9mQ=
|
||||
github.com/pierrec/lz4/v4 v4.1.21/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4=
|
||||
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
||||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
|
||||
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 h1:N/ElC8H3+5XpJzTSTfLsJV/mx9Q9g7kxmchpfZyxgzM=
|
||||
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4=
|
||||
github.com/rogpeppe/go-internal v1.13.1 h1:KvO1DLK/DRN07sQ1LQKScxyZJuNnedQ5/wKSR38lUII=
|
||||
github.com/rogpeppe/go-internal v1.13.1/go.mod h1:uMEvuHeurkdAXX61udpOXGD/AzZDWNMNyH2VO9fmH0o=
|
||||
github.com/shirou/gopsutil v3.21.11+incompatible h1:+1+c1VGhc88SSonWP6foOcLhvnKlUeu/erjjvaPEYiI=
|
||||
github.com/shirou/gopsutil v3.21.11+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
||||
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
|
||||
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
|
||||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
|
||||
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
||||
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
|
||||
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||
github.com/templexxx/cpu v0.1.1 h1:isxHaxBXpYFWnk2DReuKkigaZyrjs2+9ypIdGP4h+HI=
|
||||
github.com/templexxx/cpu v0.1.1/go.mod h1:w7Tb+7qgcAlIyX4NhLuDKt78AHA5SzPmq0Wj6HiEnnk=
|
||||
github.com/templexxx/xorsimd v0.4.3 h1:9AQTFHd7Bhk3dIT7Al2XeBX5DWOvsUPZCuhyAtNbHjU=
|
||||
github.com/templexxx/xorsimd v0.4.3/go.mod h1:oZQcD6RFDisW2Am58dSAGwwL6rHjbzrlu25VDqfWkQg=
|
||||
github.com/tidwall/pretty v1.0.0 h1:HsD+QiTn7sK6flMKIvNmpqz1qrpP3Ps6jOKIKMooyg4=
|
||||
github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
|
||||
github.com/tjfoc/gmsm v1.4.1 h1:aMe1GlZb+0bLjn+cKTPEvvn9oUEBlJitaZiiBwsbgho=
|
||||
github.com/tjfoc/gmsm v1.4.1/go.mod h1:j4INPkHWMrhJb38G+J6W4Tw0AbuN8Thu3PbdVYhVcTE=
|
||||
github.com/tklauser/go-sysconf v0.3.13 h1:GBUpcahXSpR2xN01jhkNAbTLRk2Yzgggk8IM08lq3r4=
|
||||
github.com/tklauser/go-sysconf v0.3.13/go.mod h1:zwleP4Q4OehZHGn4CYZDipCgg9usW5IJePewFCGVEa0=
|
||||
github.com/tklauser/numcpus v0.7.0 h1:yjuerZP127QG9m5Zh/mSO4wqurYil27tHrqwRoRjpr4=
|
||||
github.com/tklauser/numcpus v0.7.0/go.mod h1:bb6dMVcj8A42tSE7i32fsIUCbQNllK5iDguyOZRUzAY=
|
||||
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
|
||||
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
|
||||
github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE=
|
||||
github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
|
||||
github.com/xdg-go/pbkdf2 v1.0.0 h1:Su7DPu48wXMwC3bs7MCNG+z4FhcyEuz5dlvchbq0B0c=
|
||||
github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI=
|
||||
github.com/xdg-go/scram v1.0.2 h1:akYIkZ28e6A96dkWNJQu3nmCzH3YfwMPQExUYDaRv7w=
|
||||
github.com/xdg-go/scram v1.0.2/go.mod h1:1WAq6h33pAW+iRreB34OORO2Nf7qel3VV3fjBj+hCSs=
|
||||
github.com/xdg-go/stringprep v1.0.2 h1:6iq84/ryjjeRmMJwxutI51F2GIPlP5BfTvXHeYjyhBc=
|
||||
github.com/xdg-go/stringprep v1.0.2/go.mod h1:8F9zXuvzgwmyT5DUm4GUfZGDdT3W+LCvS6+da4O5kxM=
|
||||
github.com/xtaci/kcp-go/v5 v5.6.18 h1:7oV4mc272pcnn39/13BB11Bx7hJM4ogMIEokJYVWn4g=
|
||||
github.com/xtaci/kcp-go/v5 v5.6.18/go.mod h1:75S1AKYYzNUSXIv30h+jPKJYZUwqpfvLshu63nCNSOM=
|
||||
github.com/xtaci/lossyconn v0.0.0-20190602105132-8df528c0c9ae h1:J0GxkO96kL4WF+AIT3M4mfUVinOCPgf2uUWYFUzN0sM=
|
||||
github.com/xtaci/lossyconn v0.0.0-20190602105132-8df528c0c9ae/go.mod h1:gXtu8J62kEgmN++bm9BVICuT/e8yiLI2KFobd/TRFsE=
|
||||
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d h1:splanxYIlg+5LfHAM6xpdFEAYOk8iySO56hMFq6uLyA=
|
||||
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d/go.mod h1:rHwXgn7JulP+udvsHwJoVG1YGAP6VLg4y9I5dyZdqmA=
|
||||
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
|
||||
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
|
||||
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
||||
github.com/yusufpapurcu/wmi v1.2.4 h1:zFUKzehAFReQwLys1b/iSMl+JQGSCSjtVqQn9bBrPo0=
|
||||
github.com/yusufpapurcu/wmi v1.2.4/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0=
|
||||
go.etcd.io/etcd/api/v3 v3.5.13 h1:8WXU2/NBge6AUF1K1gOexB6e07NgsN1hXK0rSTtgSp4=
|
||||
@@ -98,65 +219,124 @@ go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ=
|
||||
go.uber.org/multierr v1.10.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
|
||||
go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8=
|
||||
go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E=
|
||||
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
|
||||
golang.org/x/arch v0.8.0 h1:3wRIsP3pM4yUptoR96otTUOXI367OS0+c9eeRi9doIc=
|
||||
golang.org/x/arch v0.8.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
||||
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
||||
golang.org/x/crypto v0.0.0-20201012173705-84dcc777aaee/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
||||
golang.org/x/crypto v0.0.0-20201216223049-8b5274cf687f/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I=
|
||||
golang.org/x/crypto v0.18.0 h1:PGVlW0xEltQnzFZ55hkuX5+KLyrMYhHld1YHO4AKcdc=
|
||||
golang.org/x/crypto v0.18.0/go.mod h1:R0j02AL6hcrfOiy9T4ZYp/rcWeMxM3L6QYxlOuEG1mg=
|
||||
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
||||
golang.org/x/crypto v0.6.0/go.mod h1:OFC/31mSvZgRz0V1QTNCzfAI1aIRzbiufJtkMIlEp58=
|
||||
golang.org/x/crypto v0.26.0 h1:RrRspgV4mU+YwB4FYnuBoKsUapNIL5cohGAmSH3azsw=
|
||||
golang.org/x/crypto v0.26.0/go.mod h1:GY7jblb9wI+FOo5y8/S2oY4zWP07AkOJ4+jxCqdqn54=
|
||||
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
|
||||
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
|
||||
golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU=
|
||||
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
|
||||
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
|
||||
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
|
||||
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
|
||||
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20200114155413-6afb5195e5aa/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20201010224723-4f7140c49acb/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
|
||||
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
|
||||
golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM=
|
||||
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
|
||||
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
||||
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
|
||||
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
|
||||
golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
|
||||
golang.org/x/net v0.28.0 h1:a9JDOJc5GMUJ0+UDqmLT86WiEy7iWyIhz8gz8E4e5hE=
|
||||
golang.org/x/net v0.28.0/go.mod h1:yqtgsTWOOnlGLG9GFRrK3++bGOUEkNBoHZc8MEDWPNg=
|
||||
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
|
||||
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E=
|
||||
golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
|
||||
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
|
||||
golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
||||
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
|
||||
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.23.0 h1:YfKFowiIMvtgl1UERQoTPPToxltDeZfbj4H7dVUCwmM=
|
||||
golang.org/x/sys v0.23.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
||||
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
|
||||
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
|
||||
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
||||
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
|
||||
golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc=
|
||||
golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY=
|
||||
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
|
||||
golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
|
||||
golang.org/x/tools v0.0.0-20190531172133-b3315ee88b7d/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
|
||||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||
golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
|
||||
golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
|
||||
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
|
||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
|
||||
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
|
||||
google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
|
||||
google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc=
|
||||
google.golang.org/genproto v0.0.0-20230822172742-b8732ec3820d h1:VBu5YqKPv6XiJ199exd8Br+Aetz+o08F+PLMnwJQHAY=
|
||||
google.golang.org/genproto v0.0.0-20230822172742-b8732ec3820d/go.mod h1:yZTlhN0tQnXo3h00fuXNCxJdLdIdnVFVBaRJ5LWBbw4=
|
||||
google.golang.org/genproto/googleapis/api v0.0.0-20230822172742-b8732ec3820d h1:DoPTO70H+bcDXcd39vOqb2viZxgqeBeSGtZ55yZU4/Q=
|
||||
google.golang.org/genproto/googleapis/api v0.0.0-20230822172742-b8732ec3820d/go.mod h1:KjSP20unUpOx5kyQUFa7k4OJg0qeJ7DEZflGDu2p6Bk=
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d h1:uvYuEyMHKNt+lT4K3bN6fGswmK8qSvcreM3BwjDh+y4=
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d/go.mod h1:+Bk1OCOj40wS2hwAMA+aCW9ypzm63QTBBHp6lQ3p+9M=
|
||||
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
|
||||
google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg=
|
||||
google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQciAY=
|
||||
google.golang.org/grpc v1.31.0/go.mod h1:N36X2cJ7JwdamYAgDz+s+rVMFjt3numwzf/HckM8pak=
|
||||
google.golang.org/grpc v1.59.0 h1:Z5Iec2pjwb+LEOqzpB2MR12/eKFhDPhuqW91O+4bwUk=
|
||||
google.golang.org/grpc v1.59.0/go.mod h1:aUPDwccQo6OTjy7Hct4AfBPD1GptF4fyUjIkQ9YtF98=
|
||||
google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI=
|
||||
google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
|
||||
google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8=
|
||||
google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0=
|
||||
google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM=
|
||||
google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE=
|
||||
google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo=
|
||||
google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
|
||||
google.golang.org/protobuf v1.34.1 h1:9ddQBjfCyZPOHPUiPxpYESBLc+T8P3E+Vo4IbKZgFWg=
|
||||
google.golang.org/protobuf v1.34.1/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
|
||||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22 h1:VpOs+IwYnYBaFnrNAeB8UUWtL3vEUnzSCL1nVjPhqrw=
|
||||
gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22/go.mod h1:yeKp02qBN3iKW1OzL3MGk2IdtZzaj7SFntXj72NppTA=
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
|
||||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
|
||||
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||
nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50=
|
||||
rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package log // import "go.uber.org/zap/buffer"
|
||||
package log
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
@@ -15,82 +15,79 @@ func (buff *Buffer) Init(){
|
||||
buff.bs = make([]byte, _size)
|
||||
}
|
||||
|
||||
|
||||
|
||||
// AppendByte writes a single byte to the Buffer.
|
||||
func (b *Buffer) AppendByte(v byte) {
|
||||
b.bs = append(b.bs, v)
|
||||
func (buff *Buffer) AppendByte(v byte) {
|
||||
buff.bs = append(buff.bs, v)
|
||||
}
|
||||
|
||||
func (b *Buffer) AppendBytes(v []byte) {
|
||||
b.bs = append(b.bs, v...)
|
||||
func (buff *Buffer) AppendBytes(v []byte) {
|
||||
buff.bs = append(buff.bs, v...)
|
||||
}
|
||||
|
||||
// AppendString writes a string to the Buffer.
|
||||
func (b *Buffer) AppendString(s string) {
|
||||
b.bs = append(b.bs, s...)
|
||||
func (buff *Buffer) AppendString(s string) {
|
||||
buff.bs = append(buff.bs, s...)
|
||||
}
|
||||
|
||||
// AppendInt appends an integer to the underlying buffer (assuming base 10).
|
||||
func (b *Buffer) AppendInt(i int64) {
|
||||
b.bs = strconv.AppendInt(b.bs, i, 10)
|
||||
func (buff *Buffer) AppendInt(i int64) {
|
||||
buff.bs = strconv.AppendInt(buff.bs, i, 10)
|
||||
}
|
||||
|
||||
// AppendUint appends an unsigned integer to the underlying buffer (assuming
|
||||
// base 10).
|
||||
func (b *Buffer) AppendUint(i uint64) {
|
||||
b.bs = strconv.AppendUint(b.bs, i, 10)
|
||||
func (buff *Buffer) AppendUint(i uint64) {
|
||||
buff.bs = strconv.AppendUint(buff.bs, i, 10)
|
||||
}
|
||||
|
||||
// AppendBool appends a bool to the underlying buffer.
|
||||
func (b *Buffer) AppendBool(v bool) {
|
||||
b.bs = strconv.AppendBool(b.bs, v)
|
||||
func (buff *Buffer) AppendBool(v bool) {
|
||||
buff.bs = strconv.AppendBool(buff.bs, v)
|
||||
}
|
||||
|
||||
// AppendFloat appends a float to the underlying buffer. It doesn't quote NaN
|
||||
// or +/- Inf.
|
||||
func (b *Buffer) AppendFloat(f float64, bitSize int) {
|
||||
b.bs = strconv.AppendFloat(b.bs, f, 'f', -1, bitSize)
|
||||
func (buff *Buffer) AppendFloat(f float64, bitSize int) {
|
||||
buff.bs = strconv.AppendFloat(buff.bs, f, 'f', -1, bitSize)
|
||||
}
|
||||
|
||||
// Len returns the length of the underlying byte slice.
|
||||
func (b *Buffer) Len() int {
|
||||
return len(b.bs)
|
||||
func (buff *Buffer) Len() int {
|
||||
return len(buff.bs)
|
||||
}
|
||||
|
||||
// Cap returns the capacity of the underlying byte slice.
|
||||
func (b *Buffer) Cap() int {
|
||||
return cap(b.bs)
|
||||
func (buff *Buffer) Cap() int {
|
||||
return cap(buff.bs)
|
||||
}
|
||||
|
||||
// Bytes returns a mutable reference to the underlying byte slice.
|
||||
func (b *Buffer) Bytes() []byte {
|
||||
return b.bs
|
||||
func (buff *Buffer) Bytes() []byte {
|
||||
return buff.bs
|
||||
}
|
||||
|
||||
// String returns a string copy of the underlying byte slice.
|
||||
func (b *Buffer) String() string {
|
||||
return string(b.bs)
|
||||
func (buff *Buffer) String() string {
|
||||
return string(buff.bs)
|
||||
}
|
||||
|
||||
// Reset resets the underlying byte slice. Subsequent writes re-use the slice's
|
||||
// backing array.
|
||||
func (b *Buffer) Reset() {
|
||||
b.bs = b.bs[:0]
|
||||
func (buff *Buffer) Reset() {
|
||||
buff.bs = buff.bs[:0]
|
||||
}
|
||||
|
||||
// Write implements io.Writer.
|
||||
func (b *Buffer) Write(bs []byte) (int, error) {
|
||||
b.bs = append(b.bs, bs...)
|
||||
func (buff *Buffer) Write(bs []byte) (int, error) {
|
||||
buff.bs = append(buff.bs, bs...)
|
||||
return len(bs), nil
|
||||
}
|
||||
|
||||
// TrimNewline trims any final "\n" byte from the end of the buffer.
|
||||
func (b *Buffer) TrimNewline() {
|
||||
if i := len(b.bs) - 1; i >= 0 {
|
||||
if b.bs[i] == '\n' {
|
||||
b.bs = b.bs[:i]
|
||||
func (buff *Buffer) TrimNewline() {
|
||||
if i := len(buff.bs) - 1; i >= 0 {
|
||||
if buff.bs[i] == '\n' {
|
||||
buff.bs = buff.bs[:i]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -10,16 +10,21 @@ import (
|
||||
"sync"
|
||||
)
|
||||
|
||||
const defaultSkip = 7
|
||||
|
||||
type IOriginHandler interface {
|
||||
slog.Handler
|
||||
Lock()
|
||||
UnLock()
|
||||
SetSkip(skip int)
|
||||
GetSkip() int
|
||||
}
|
||||
|
||||
type BaseHandler struct {
|
||||
addSource bool
|
||||
w io.Writer
|
||||
locker sync.Mutex
|
||||
skip int
|
||||
}
|
||||
|
||||
type OriginTextHandler struct {
|
||||
@@ -32,6 +37,14 @@ type OriginJsonHandler struct {
|
||||
*slog.JSONHandler
|
||||
}
|
||||
|
||||
func (bh *BaseHandler) SetSkip(skip int) {
|
||||
bh.skip = skip
|
||||
}
|
||||
|
||||
func (bh *BaseHandler) GetSkip() int {
|
||||
return bh.skip
|
||||
}
|
||||
|
||||
func getStrLevel(level slog.Level) string {
|
||||
switch level {
|
||||
case LevelTrace:
|
||||
@@ -78,6 +91,7 @@ func NewOriginTextHandler(level slog.Level,w io.Writer,addSource bool,replaceAtt
|
||||
ReplaceAttr: replaceAttr,
|
||||
})
|
||||
|
||||
textHandler.skip = defaultSkip
|
||||
return &textHandler
|
||||
}
|
||||
|
||||
@@ -98,20 +112,19 @@ func (oh *OriginTextHandler) Handle(context context.Context, record slog.Record)
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
return oh.TextHandler.Handle(context, record)
|
||||
}
|
||||
|
||||
func (b *BaseHandler) logStack(record *slog.Record){
|
||||
b.w.Write(debug.Stack())
|
||||
func (bh *BaseHandler) logStack(record *slog.Record) {
|
||||
bh.w.Write(debug.Stack())
|
||||
}
|
||||
|
||||
func (b *BaseHandler) Lock(){
|
||||
b.locker.Lock()
|
||||
func (bh *BaseHandler) Lock() {
|
||||
bh.locker.Lock()
|
||||
}
|
||||
|
||||
func (b *BaseHandler) UnLock(){
|
||||
b.locker.Unlock()
|
||||
func (bh *BaseHandler) UnLock() {
|
||||
bh.locker.Unlock()
|
||||
}
|
||||
|
||||
func NewOriginJsonHandler(level slog.Level, w io.Writer, addSource bool, replaceAttr func([]string, slog.Attr) slog.Attr) slog.Handler {
|
||||
@@ -124,6 +137,7 @@ func NewOriginJsonHandler(level slog.Level,w io.Writer,addSource bool,replaceAtt
|
||||
ReplaceAttr: replaceAttr,
|
||||
})
|
||||
|
||||
jsonHandler.skip = defaultSkip
|
||||
return &jsonHandler
|
||||
}
|
||||
|
||||
@@ -138,10 +152,10 @@ func (oh *OriginJsonHandler) Handle(context context.Context, record slog.Record)
|
||||
return oh.JSONHandler.Handle(context, record)
|
||||
}
|
||||
|
||||
func (b *BaseHandler) Fill(context context.Context, record *slog.Record) {
|
||||
if b.addSource {
|
||||
func (bh *BaseHandler) Fill(_ context.Context, record *slog.Record) {
|
||||
if bh.addSource {
|
||||
var pcs [1]uintptr
|
||||
runtime.Callers(7, pcs[:])
|
||||
runtime.Callers(bh.skip, pcs[:])
|
||||
record.PC = pcs[0]
|
||||
}
|
||||
}
|
||||
|
||||
103
log/log.go
103
log/log.go
@@ -4,7 +4,6 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/duanhf2012/origin/v2/util/bytespool"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
"io"
|
||||
"log/slog"
|
||||
"os"
|
||||
@@ -16,13 +15,11 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
var json = jsoniter.ConfigCompatibleWithStandardLibrary
|
||||
var OpenConsole bool
|
||||
var LogSize int64
|
||||
var LogChannelCap int
|
||||
var LogPath string
|
||||
var LogLevel slog.Level = LevelTrace
|
||||
|
||||
var LogLevel = LevelTrace
|
||||
|
||||
var gLogger, _ = NewTextLogger(LevelDebug, "", "", true, LogChannelCap)
|
||||
var isSetLogger bool
|
||||
@@ -51,12 +48,12 @@ type ILogger interface {
|
||||
Fatal(msg string, args ...any)
|
||||
|
||||
DoSPrintf(level slog.Level, a []interface{})
|
||||
FormatHeader(buf *Buffer,level slog.Level,calldepth int)
|
||||
FormatHeader(buf *Buffer, level slog.Level, callDepth int)
|
||||
Close()
|
||||
}
|
||||
|
||||
type Logger struct {
|
||||
Slogger *slog.Logger
|
||||
SLogger *slog.Logger
|
||||
|
||||
ioWriter IoWriter
|
||||
|
||||
@@ -73,7 +70,7 @@ type IoWriter struct {
|
||||
lockWrite sync.Mutex
|
||||
|
||||
filePath string
|
||||
fileprefix string
|
||||
filePrefix string
|
||||
fileDay int
|
||||
fileCreateTime int64 //second
|
||||
}
|
||||
@@ -104,8 +101,8 @@ func (iw *IoWriter) close() error {
|
||||
}
|
||||
|
||||
func (iw *IoWriter) writeFile(p []byte) (n int, err error) {
|
||||
//swich log file
|
||||
iw.swichFile()
|
||||
//switch log file
|
||||
iw.switchFile()
|
||||
|
||||
if iw.outFile != nil {
|
||||
n, err = iw.outFile.Write(p)
|
||||
@@ -204,7 +201,7 @@ func (logger *Logger) setLogChannel(logChannel int) (err error){
|
||||
return logger.ioWriter.setLogChannel(logChannel)
|
||||
}
|
||||
|
||||
func (iw *IoWriter) swichFile() error{
|
||||
func (iw *IoWriter) switchFile() error {
|
||||
now := time.Now()
|
||||
if iw.fileCreateTime == now.Unix() {
|
||||
return nil
|
||||
@@ -217,7 +214,7 @@ func (iw *IoWriter) swichFile() error{
|
||||
if iw.filePath != "" {
|
||||
var err error
|
||||
fileName := fmt.Sprintf("%s%d%02d%02d_%02d_%02d_%02d.log",
|
||||
iw.fileprefix,
|
||||
iw.filePrefix,
|
||||
now.Year(),
|
||||
now.Month(),
|
||||
now.Day(),
|
||||
@@ -239,14 +236,18 @@ func (iw *IoWriter) swichFile() error{
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetDefaultHandler() IOriginHandler {
|
||||
return gLogger.(*Logger).SLogger.Handler().(IOriginHandler)
|
||||
}
|
||||
|
||||
func NewTextLogger(level slog.Level, pathName string, filePrefix string, addSource bool, logChannelCap int) (ILogger, error) {
|
||||
var logger Logger
|
||||
logger.ioWriter.filePath = pathName
|
||||
logger.ioWriter.fileprefix = filePrefix
|
||||
logger.ioWriter.filePrefix = filePrefix
|
||||
|
||||
logger.Slogger = slog.New(NewOriginTextHandler(level,&logger.ioWriter,addSource,defaultReplaceAttr))
|
||||
logger.SLogger = slog.New(NewOriginTextHandler(level, &logger.ioWriter, addSource, defaultReplaceAttr))
|
||||
logger.setLogChannel(logChannelCap)
|
||||
err := logger.ioWriter.swichFile()
|
||||
err := logger.ioWriter.switchFile()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -257,11 +258,11 @@ func NewTextLogger(level slog.Level,pathName string,filePrefix string,addSource
|
||||
func NewJsonLogger(level slog.Level, pathName string, filePrefix string, addSource bool, logChannelCap int) (ILogger, error) {
|
||||
var logger Logger
|
||||
logger.ioWriter.filePath = pathName
|
||||
logger.ioWriter.fileprefix = filePrefix
|
||||
logger.ioWriter.filePrefix = filePrefix
|
||||
|
||||
logger.Slogger = slog.New(NewOriginJsonHandler(level,&logger.ioWriter,true,defaultReplaceAttr))
|
||||
logger.SLogger = slog.New(NewOriginJsonHandler(level, &logger.ioWriter, true, defaultReplaceAttr))
|
||||
logger.setLogChannel(logChannelCap)
|
||||
err := logger.ioWriter.swichFile()
|
||||
err := logger.ioWriter.switchFile()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -269,46 +270,46 @@ func NewJsonLogger(level slog.Level,pathName string,filePrefix string,addSource
|
||||
return &logger, nil
|
||||
}
|
||||
|
||||
// It's dangerous to call the method on logging
|
||||
// Close It's dangerous to call the method on logging
|
||||
func (logger *Logger) Close() {
|
||||
logger.ioWriter.Close()
|
||||
}
|
||||
|
||||
func (logger *Logger) Trace(msg string, args ...any) {
|
||||
logger.Slogger.Log(context.Background(),LevelTrace,msg,args...)
|
||||
logger.SLogger.Log(context.Background(), LevelTrace, msg, args...)
|
||||
}
|
||||
|
||||
func (logger *Logger) Debug(msg string, args ...any) {
|
||||
|
||||
logger.Slogger.Log(context.Background(),LevelDebug,msg,args...)
|
||||
logger.SLogger.Log(context.Background(), LevelDebug, msg, args...)
|
||||
}
|
||||
|
||||
func (logger *Logger) Info(msg string, args ...any) {
|
||||
logger.Slogger.Log(context.Background(),LevelInfo,msg,args...)
|
||||
logger.SLogger.Log(context.Background(), LevelInfo, msg, args...)
|
||||
}
|
||||
|
||||
func (logger *Logger) Warning(msg string, args ...any) {
|
||||
logger.Slogger.Log(context.Background(),LevelWarning,msg,args...)
|
||||
logger.SLogger.Log(context.Background(), LevelWarning, msg, args...)
|
||||
}
|
||||
|
||||
func (logger *Logger) Error(msg string, args ...any) {
|
||||
logger.Slogger.Log(context.Background(),LevelError,msg,args...)
|
||||
logger.SLogger.Log(context.Background(), LevelError, msg, args...)
|
||||
}
|
||||
|
||||
func (logger *Logger) Stack(msg string, args ...any) {
|
||||
logger.Slogger.Log(context.Background(),LevelStack,msg,args...)
|
||||
logger.SLogger.Log(context.Background(), LevelStack, msg, args...)
|
||||
}
|
||||
|
||||
func (logger *Logger) Dump(msg string, args ...any) {
|
||||
logger.Slogger.Log(context.Background(),LevelDump,msg,args...)
|
||||
logger.SLogger.Log(context.Background(), LevelDump, msg, args...)
|
||||
}
|
||||
|
||||
func (logger *Logger) Fatal(msg string, args ...any) {
|
||||
logger.Slogger.Log(context.Background(),LevelFatal,msg,args...)
|
||||
logger.SLogger.Log(context.Background(), LevelFatal, msg, args...)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// It's non-thread-safe
|
||||
// SetLogger It's non-thread-safe
|
||||
func SetLogger(logger ILogger) {
|
||||
if logger != nil && isSetLogger == false {
|
||||
gLogger = logger
|
||||
@@ -358,74 +359,74 @@ func Close() {
|
||||
|
||||
func ErrorAttr(key string, value error) slog.Attr {
|
||||
if value == nil {
|
||||
return slog.Attr{key, slog.StringValue("nil")}
|
||||
return slog.Attr{Key: key, Value: slog.StringValue("nil")}
|
||||
}
|
||||
|
||||
return slog.Attr{key, slog.StringValue(value.Error())}
|
||||
return slog.Attr{Key: key, Value: slog.StringValue(value.Error())}
|
||||
}
|
||||
|
||||
func String(key, value string) slog.Attr {
|
||||
return slog.Attr{key, slog.StringValue(value)}
|
||||
return slog.Attr{Key: key, Value: slog.StringValue(value)}
|
||||
}
|
||||
|
||||
func Int(key string, value int) slog.Attr {
|
||||
return slog.Attr{key, slog.Int64Value(int64(value))}
|
||||
return slog.Attr{Key: key, Value: slog.Int64Value(int64(value))}
|
||||
}
|
||||
|
||||
func Int64(key string, value int64) slog.Attr {
|
||||
return slog.Attr{key, slog.Int64Value(value)}
|
||||
return slog.Attr{Key: key, Value: slog.Int64Value(value)}
|
||||
}
|
||||
|
||||
func Int32(key string, value int32) slog.Attr {
|
||||
return slog.Attr{key, slog.Int64Value(int64(value))}
|
||||
return slog.Attr{Key: key, Value: slog.Int64Value(int64(value))}
|
||||
}
|
||||
|
||||
func Int16(key string, value int16) slog.Attr {
|
||||
return slog.Attr{key, slog.Int64Value(int64(value))}
|
||||
return slog.Attr{Key: key, Value: slog.Int64Value(int64(value))}
|
||||
}
|
||||
|
||||
func Int8(key string, value int8) slog.Attr {
|
||||
return slog.Attr{key, slog.Int64Value(int64(value))}
|
||||
return slog.Attr{Key: key, Value: slog.Int64Value(int64(value))}
|
||||
}
|
||||
|
||||
func Uint(key string, value uint) slog.Attr {
|
||||
return slog.Attr{key, slog.Uint64Value(uint64(value))}
|
||||
return slog.Attr{Key: key, Value: slog.Uint64Value(uint64(value))}
|
||||
}
|
||||
|
||||
func Uint64(key string, v uint64) slog.Attr {
|
||||
return slog.Attr{key, slog.Uint64Value(v)}
|
||||
return slog.Attr{Key: key, Value: slog.Uint64Value(v)}
|
||||
}
|
||||
|
||||
func Uint32(key string, value uint32) slog.Attr {
|
||||
return slog.Attr{key, slog.Uint64Value(uint64(value))}
|
||||
return slog.Attr{Key: key, Value: slog.Uint64Value(uint64(value))}
|
||||
}
|
||||
|
||||
func Uint16(key string, value uint16) slog.Attr {
|
||||
return slog.Attr{key, slog.Uint64Value(uint64(value))}
|
||||
return slog.Attr{Key: key, Value: slog.Uint64Value(uint64(value))}
|
||||
}
|
||||
|
||||
func Uint8(key string, value uint8) slog.Attr {
|
||||
return slog.Attr{key, slog.Uint64Value(uint64(value))}
|
||||
return slog.Attr{Key: key, Value: slog.Uint64Value(uint64(value))}
|
||||
}
|
||||
|
||||
func Float64(key string, v float64) slog.Attr {
|
||||
return slog.Attr{key, slog.Float64Value(v)}
|
||||
return slog.Attr{Key: key, Value: slog.Float64Value(v)}
|
||||
}
|
||||
|
||||
func Bool(key string, v bool) slog.Attr {
|
||||
return slog.Attr{key, slog.BoolValue(v)}
|
||||
return slog.Attr{Key: key, Value: slog.BoolValue(v)}
|
||||
}
|
||||
|
||||
func Time(key string, v time.Time) slog.Attr {
|
||||
return slog.Attr{key, slog.TimeValue(v)}
|
||||
return slog.Attr{Key: key, Value: slog.TimeValue(v)}
|
||||
}
|
||||
|
||||
func Duration(key string, v time.Duration) slog.Attr {
|
||||
return slog.Attr{key, slog.DurationValue(v)}
|
||||
return slog.Attr{Key: key, Value: slog.DurationValue(v)}
|
||||
}
|
||||
|
||||
func Any(key string, value any) slog.Attr {
|
||||
return slog.Attr{key, slog.AnyValue(value)}
|
||||
return slog.Attr{Key: key, Value: slog.AnyValue(value)}
|
||||
}
|
||||
|
||||
func Group(key string, args ...any) slog.Attr {
|
||||
@@ -433,12 +434,12 @@ func Group(key string, args ...any) slog.Attr {
|
||||
}
|
||||
|
||||
func (logger *Logger) DoSPrintf(level slog.Level, a []interface{}) {
|
||||
if logger.Slogger.Enabled(context.Background(),level) == false{
|
||||
if logger.SLogger.Enabled(context.Background(), level) == false {
|
||||
return
|
||||
}
|
||||
|
||||
logger.Slogger.Handler().(IOriginHandler).Lock()
|
||||
defer logger.Slogger.Handler().(IOriginHandler).UnLock()
|
||||
logger.SLogger.Handler().(IOriginHandler).Lock()
|
||||
defer logger.SLogger.Handler().(IOriginHandler).UnLock()
|
||||
|
||||
logger.sBuff.Reset()
|
||||
|
||||
@@ -448,7 +449,7 @@ func (logger *Logger) DoSPrintf(level slog.Level,a []interface{}) {
|
||||
logger.sBuff.AppendString(slog.AnyValue(s).String())
|
||||
}
|
||||
logger.sBuff.AppendString("\"\n")
|
||||
logger.ioWriter.Write([]byte(logger.sBuff.Bytes()))
|
||||
logger.ioWriter.Write(logger.sBuff.Bytes())
|
||||
}
|
||||
|
||||
func (logger *Logger) STrace(a ...interface{}) {
|
||||
@@ -491,14 +492,14 @@ func SError(a ...interface{}) {
|
||||
gLogger.DoSPrintf(LevelError, a)
|
||||
}
|
||||
|
||||
func (logger *Logger) FormatHeader(buf *Buffer,level slog.Level,calldepth int) {
|
||||
func (logger *Logger) FormatHeader(buf *Buffer, level slog.Level, callDepth int) {
|
||||
t := time.Now()
|
||||
var file string
|
||||
var line int
|
||||
|
||||
// Release lock while getting caller info - it's expensive.
|
||||
var ok bool
|
||||
_, file, line, ok = runtime.Caller(calldepth)
|
||||
_, file, line, ok = runtime.Caller(callDepth)
|
||||
if !ok {
|
||||
file = "???"
|
||||
line = 0
|
||||
|
||||
160
network/conn.go
160
network/conn.go
@@ -1,7 +1,12 @@
|
||||
package network
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/duanhf2012/origin/v2/log"
|
||||
"net"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Conn interface {
|
||||
@@ -13,3 +18,158 @@ type Conn interface {
|
||||
Destroy()
|
||||
ReleaseReadMsg(byteBuff []byte)
|
||||
}
|
||||
|
||||
type ConnSet map[net.Conn]struct{}
|
||||
|
||||
type NetConn struct {
|
||||
sync.Mutex
|
||||
conn net.Conn
|
||||
writeChan chan []byte
|
||||
closeFlag int32
|
||||
msgParser *MsgParser
|
||||
}
|
||||
|
||||
func freeChannel(conn *NetConn) {
|
||||
for len(conn.writeChan) > 0 {
|
||||
byteBuff := <-conn.writeChan
|
||||
if byteBuff != nil {
|
||||
conn.ReleaseReadMsg(byteBuff)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func newNetConn(conn net.Conn, pendingWriteNum int, msgParser *MsgParser, writeDeadline time.Duration) *NetConn {
|
||||
netConn := new(NetConn)
|
||||
netConn.conn = conn
|
||||
netConn.writeChan = make(chan []byte, pendingWriteNum)
|
||||
netConn.msgParser = msgParser
|
||||
go func() {
|
||||
for b := range netConn.writeChan {
|
||||
if b == nil {
|
||||
break
|
||||
}
|
||||
|
||||
conn.SetWriteDeadline(time.Now().Add(writeDeadline))
|
||||
_, err := conn.Write(b)
|
||||
netConn.msgParser.ReleaseBytes(b)
|
||||
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
}
|
||||
conn.Close()
|
||||
netConn.Lock()
|
||||
freeChannel(netConn)
|
||||
atomic.StoreInt32(&netConn.closeFlag, 1)
|
||||
netConn.Unlock()
|
||||
}()
|
||||
|
||||
return netConn
|
||||
}
|
||||
|
||||
func (netConn *NetConn) doDestroy() {
|
||||
netConn.conn.Close()
|
||||
|
||||
if atomic.LoadInt32(&netConn.closeFlag) == 0 {
|
||||
close(netConn.writeChan)
|
||||
atomic.StoreInt32(&netConn.closeFlag, 1)
|
||||
}
|
||||
}
|
||||
|
||||
func (netConn *NetConn) Destroy() {
|
||||
netConn.Lock()
|
||||
defer netConn.Unlock()
|
||||
|
||||
netConn.doDestroy()
|
||||
}
|
||||
|
||||
func (netConn *NetConn) Close() {
|
||||
netConn.Lock()
|
||||
defer netConn.Unlock()
|
||||
if atomic.LoadInt32(&netConn.closeFlag) == 1 {
|
||||
return
|
||||
}
|
||||
|
||||
netConn.doWrite(nil)
|
||||
atomic.StoreInt32(&netConn.closeFlag, 1)
|
||||
}
|
||||
|
||||
func (netConn *NetConn) GetRemoteIp() string {
|
||||
return netConn.conn.RemoteAddr().String()
|
||||
}
|
||||
|
||||
func (netConn *NetConn) doWrite(b []byte) error {
|
||||
if len(netConn.writeChan) == cap(netConn.writeChan) {
|
||||
netConn.ReleaseReadMsg(b)
|
||||
log.Error("close conn: channel full")
|
||||
netConn.doDestroy()
|
||||
return errors.New("close conn: channel full")
|
||||
}
|
||||
|
||||
netConn.writeChan <- b
|
||||
return nil
|
||||
}
|
||||
|
||||
// b must not be modified by the others goroutines
|
||||
func (netConn *NetConn) Write(b []byte) (int,error) {
|
||||
netConn.Lock()
|
||||
defer netConn.Unlock()
|
||||
if atomic.LoadInt32(&netConn.closeFlag) == 1 || b == nil {
|
||||
netConn.ReleaseReadMsg(b)
|
||||
return 0,errors.New("conn is close")
|
||||
}
|
||||
|
||||
return len(b),netConn.doWrite(b)
|
||||
}
|
||||
|
||||
func (netConn *NetConn) Read(b []byte) (int, error) {
|
||||
return netConn.conn.Read(b)
|
||||
}
|
||||
|
||||
func (netConn *NetConn) LocalAddr() net.Addr {
|
||||
return netConn.conn.LocalAddr()
|
||||
}
|
||||
|
||||
func (netConn *NetConn) RemoteAddr() net.Addr {
|
||||
return netConn.conn.RemoteAddr()
|
||||
}
|
||||
|
||||
func (netConn *NetConn) ReadMsg() ([]byte, error) {
|
||||
return netConn.msgParser.Read(netConn)
|
||||
}
|
||||
|
||||
func (netConn *NetConn) GetRecyclerReaderBytes() func(data []byte) {
|
||||
return netConn.msgParser.GetRecyclerReaderBytes()
|
||||
}
|
||||
|
||||
func (netConn *NetConn) ReleaseReadMsg(byteBuff []byte) {
|
||||
netConn.msgParser.ReleaseBytes(byteBuff)
|
||||
}
|
||||
|
||||
func (netConn *NetConn) WriteMsg(args ...[]byte) error {
|
||||
if atomic.LoadInt32(&netConn.closeFlag) == 1 {
|
||||
return errors.New("conn is close")
|
||||
}
|
||||
return netConn.msgParser.Write(netConn, args...)
|
||||
}
|
||||
|
||||
func (netConn *NetConn) WriteRawMsg(args []byte) error {
|
||||
if atomic.LoadInt32(&netConn.closeFlag) == 1 {
|
||||
return errors.New("conn is close")
|
||||
}
|
||||
|
||||
_,err:= netConn.Write(args)
|
||||
return err
|
||||
}
|
||||
|
||||
func (netConn *NetConn) IsConnected() bool {
|
||||
return atomic.LoadInt32(&netConn.closeFlag) == 0
|
||||
}
|
||||
|
||||
func (netConn *NetConn) SetReadDeadline(d time.Duration) {
|
||||
netConn.conn.SetReadDeadline(time.Now().Add(d))
|
||||
}
|
||||
|
||||
func (netConn *NetConn) SetWriteDeadline(d time.Duration) {
|
||||
netConn.conn.SetWriteDeadline(time.Now().Add(d))
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
var DefaultMaxHeaderBytes int = 1<<20
|
||||
var DefaultMaxHeaderBytes = 1 << 20
|
||||
|
||||
type CAFile struct {
|
||||
CertFile string
|
||||
@@ -39,7 +39,7 @@ func (slf *HttpServer) Start() {
|
||||
|
||||
func (slf *HttpServer) startListen() error {
|
||||
if slf.httpServer != nil {
|
||||
return errors.New("Duplicate start not allowed")
|
||||
return errors.New("duplicate start not allowed")
|
||||
}
|
||||
|
||||
var tlsCaList []tls.Certificate
|
||||
@@ -81,7 +81,6 @@ func (slf *HttpServer) startListen() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
func (slf *HttpServer) SetCAFile(caFile []CAFile) {
|
||||
slf.caFileList = caFile
|
||||
}
|
||||
|
||||
161
network/kcp_client.go
Normal file
161
network/kcp_client.go
Normal file
@@ -0,0 +1,161 @@
|
||||
package network
|
||||
|
||||
import (
|
||||
"github.com/duanhf2012/origin/v2/log"
|
||||
"github.com/xtaci/kcp-go/v5"
|
||||
"net"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
type KCPClient struct {
|
||||
sync.Mutex
|
||||
Addr string
|
||||
ConnNum int
|
||||
ConnectInterval time.Duration
|
||||
PendingWriteNum int
|
||||
ReadDeadline time.Duration
|
||||
WriteDeadline time.Duration
|
||||
AutoReconnect bool
|
||||
NewAgent func(conn *NetConn) Agent
|
||||
cons ConnSet
|
||||
wg sync.WaitGroup
|
||||
closeFlag bool
|
||||
|
||||
// msg parser
|
||||
MsgParser
|
||||
}
|
||||
|
||||
func (client *KCPClient) Start() {
|
||||
client.init()
|
||||
|
||||
for i := 0; i < client.ConnNum; i++ {
|
||||
client.wg.Add(1)
|
||||
go client.connect()
|
||||
}
|
||||
}
|
||||
|
||||
func (client *KCPClient) init() {
|
||||
client.Lock()
|
||||
defer client.Unlock()
|
||||
|
||||
if client.ConnNum <= 0 {
|
||||
client.ConnNum = 1
|
||||
log.Info("invalid ConnNum", log.Int("reset", client.ConnNum))
|
||||
}
|
||||
if client.ConnectInterval <= 0 {
|
||||
client.ConnectInterval = 3 * time.Second
|
||||
log.Info("invalid ConnectInterval", log.Duration("reset", client.ConnectInterval))
|
||||
}
|
||||
if client.PendingWriteNum <= 0 {
|
||||
client.PendingWriteNum = 1000
|
||||
log.Info("invalid PendingWriteNum", log.Int("reset", client.PendingWriteNum))
|
||||
}
|
||||
if client.ReadDeadline == 0 {
|
||||
client.ReadDeadline = 15 * time.Second
|
||||
log.Info("invalid ReadDeadline", log.Int64("reset", int64(client.ReadDeadline.Seconds())))
|
||||
}
|
||||
if client.WriteDeadline == 0 {
|
||||
client.WriteDeadline = 15 * time.Second
|
||||
log.Info("invalid WriteDeadline", log.Int64("reset", int64(client.WriteDeadline.Seconds())))
|
||||
}
|
||||
if client.NewAgent == nil {
|
||||
log.Fatal("NewAgent must not be nil")
|
||||
}
|
||||
if client.cons != nil {
|
||||
log.Fatal("client is running")
|
||||
}
|
||||
|
||||
if client.MinMsgLen == 0 {
|
||||
client.MinMsgLen = Default_MinMsgLen
|
||||
}
|
||||
if client.MaxMsgLen == 0 {
|
||||
client.MaxMsgLen = Default_MaxMsgLen
|
||||
}
|
||||
if client.LenMsgLen == 0 {
|
||||
client.LenMsgLen = Default_LenMsgLen
|
||||
}
|
||||
maxMsgLen := client.MsgParser.getMaxMsgLen()
|
||||
if client.MaxMsgLen > maxMsgLen {
|
||||
client.MaxMsgLen = maxMsgLen
|
||||
log.Info("invalid MaxMsgLen", log.Uint32("reset", maxMsgLen))
|
||||
}
|
||||
|
||||
client.cons = make(ConnSet)
|
||||
client.closeFlag = false
|
||||
client.MsgParser.Init()
|
||||
}
|
||||
|
||||
func (client *KCPClient) GetCloseFlag() bool {
|
||||
client.Lock()
|
||||
defer client.Unlock()
|
||||
|
||||
return client.closeFlag
|
||||
}
|
||||
|
||||
func (client *KCPClient) dial() net.Conn {
|
||||
for {
|
||||
conn, err := kcp.DialWithOptions(client.Addr, nil, 10, 3)
|
||||
if client.closeFlag {
|
||||
return conn
|
||||
} else if err == nil && conn != nil {
|
||||
conn.SetNoDelay(1, 10, 2, 1)
|
||||
conn.SetDSCP(46)
|
||||
conn.SetStreamMode(true)
|
||||
conn.SetWindowSize(1024, 1024)
|
||||
return conn
|
||||
}
|
||||
|
||||
log.Warning("connect error ", log.String("error", err.Error()), log.String("Addr", client.Addr))
|
||||
time.Sleep(client.ConnectInterval)
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
func (client *KCPClient) connect() {
|
||||
defer client.wg.Done()
|
||||
reconnect:
|
||||
conn := client.dial()
|
||||
if conn == nil {
|
||||
return
|
||||
}
|
||||
|
||||
client.Lock()
|
||||
if client.closeFlag {
|
||||
client.Unlock()
|
||||
conn.Close()
|
||||
return
|
||||
}
|
||||
client.cons[conn] = struct{}{}
|
||||
client.Unlock()
|
||||
|
||||
netConn := newNetConn(conn, client.PendingWriteNum, &client.MsgParser, client.WriteDeadline)
|
||||
agent := client.NewAgent(netConn)
|
||||
agent.Run()
|
||||
|
||||
// cleanup
|
||||
netConn.Close()
|
||||
client.Lock()
|
||||
delete(client.cons, conn)
|
||||
client.Unlock()
|
||||
agent.OnClose()
|
||||
|
||||
if client.AutoReconnect {
|
||||
time.Sleep(client.ConnectInterval)
|
||||
goto reconnect
|
||||
}
|
||||
}
|
||||
|
||||
func (client *KCPClient) Close(waitDone bool) {
|
||||
client.Lock()
|
||||
client.closeFlag = true
|
||||
for conn := range client.cons {
|
||||
conn.Close()
|
||||
}
|
||||
client.cons = nil
|
||||
client.Unlock()
|
||||
|
||||
if waitDone == true {
|
||||
client.wg.Wait()
|
||||
}
|
||||
}
|
||||
257
network/kcp_server.go
Normal file
257
network/kcp_server.go
Normal file
@@ -0,0 +1,257 @@
|
||||
package network
|
||||
|
||||
import (
|
||||
"github.com/duanhf2012/origin/v2/log"
|
||||
"github.com/duanhf2012/origin/v2/network/processor"
|
||||
"github.com/xtaci/kcp-go/v5"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
type KCPServer struct {
|
||||
NewAgent func(Conn) Agent
|
||||
|
||||
kcpCfg *KcpCfg
|
||||
blockCrypt kcp.BlockCrypt
|
||||
|
||||
process processor.IRawProcessor
|
||||
msgParser MsgParser
|
||||
conns ConnSet
|
||||
mutexConns sync.Mutex
|
||||
wgLn sync.WaitGroup
|
||||
wgConns sync.WaitGroup
|
||||
|
||||
listener *kcp.Listener
|
||||
}
|
||||
|
||||
/*
|
||||
NoDelayCfg
|
||||
|
||||
普通模式: ikcp_nodelay(kcp, 0, 40, 0, 0);
|
||||
极速模式: ikcp_nodelay(kcp, 1, 10, 2, 1);
|
||||
*/
|
||||
type NoDelayCfg struct {
|
||||
NoDelay int // 是否启用 nodelay模式,0不启用;1启用
|
||||
IntervalMill int // 协议内部工作的 interval,单位毫秒,比如 10ms或者 20ms
|
||||
Resend int // 快速重传模式,默认0关闭,可以设置2(2次ACK跨越将会直接重传)
|
||||
CongestionControl int // 是否关闭流控,默认是0代表不关闭,1代表关闭
|
||||
}
|
||||
|
||||
const (
|
||||
DefaultNoDelay = 1
|
||||
DefaultIntervalMill = 10
|
||||
DefaultResend = 2
|
||||
DefaultCongestionControl = 1
|
||||
|
||||
DefaultMtu = 1400
|
||||
DefaultSndWndSize = 4096
|
||||
DefaultRcvWndSize = 4096
|
||||
DefaultStreamMode = true
|
||||
DefaultDSCP = 46
|
||||
DefaultDataShards = 10
|
||||
DefaultParityShards = 0
|
||||
|
||||
DefaultReadDeadlineMill = 15 * time.Second
|
||||
DefaultWriteDeadlineMill = 15 * time.Second
|
||||
|
||||
DefaultMaxConnNum = 20000
|
||||
)
|
||||
|
||||
type KcpCfg struct {
|
||||
ListenAddr string // 监听地址
|
||||
MaxConnNum int //最大连接数
|
||||
NoDelay *NoDelayCfg
|
||||
|
||||
Mtu *int // mtu大小
|
||||
SndWndSize *int // 发送窗口大小,默认1024
|
||||
RcvWndSize *int // 接收窗口大小,默认1024
|
||||
ReadDeadlineMill *time.Duration // 读超时毫秒
|
||||
WriteDeadlineMill *time.Duration // 写超时毫秒
|
||||
StreamMode *bool // 是否打开流模式,默认true
|
||||
DSCP *int // 差分服务代码点,默认46
|
||||
ReadBuffSize *int // 读Buff大小,默认
|
||||
WriteBuffSize *int // 写Buff大小
|
||||
|
||||
// 用于 FEC(前向纠错)的数据分片和校验分片数量,,默认10,0
|
||||
DataShards *int
|
||||
ParityShards *int
|
||||
|
||||
// 包体内容
|
||||
|
||||
LittleEndian bool //是否小端序
|
||||
LenMsgLen int //消息头占用byte数量,只能是1byte,2byte,4byte。如果是4byte,意味着消息最大可以是math.MaxUint32(4GB)
|
||||
MinMsgLen uint32 //最小消息长度
|
||||
MaxMsgLen uint32 //最大消息长度,超过判定不合法,断开连接
|
||||
PendingWriteNum int //写channel最大消息数量
|
||||
}
|
||||
|
||||
func (kp *KCPServer) Init(kcpCfg *KcpCfg) {
|
||||
kp.kcpCfg = kcpCfg
|
||||
kp.msgParser.Init()
|
||||
kp.msgParser.LenMsgLen = kp.kcpCfg.LenMsgLen
|
||||
kp.msgParser.MaxMsgLen = kp.kcpCfg.MaxMsgLen
|
||||
kp.msgParser.MinMsgLen = kp.kcpCfg.MinMsgLen
|
||||
kp.msgParser.LittleEndian = kp.kcpCfg.LittleEndian
|
||||
|
||||
// setting default noDelay
|
||||
if kp.kcpCfg.NoDelay == nil {
|
||||
var noDelay NoDelayCfg
|
||||
noDelay.NoDelay = DefaultNoDelay
|
||||
noDelay.IntervalMill = DefaultIntervalMill
|
||||
noDelay.Resend = DefaultResend
|
||||
noDelay.CongestionControl = DefaultCongestionControl
|
||||
kp.kcpCfg.NoDelay = &noDelay
|
||||
}
|
||||
|
||||
if kp.kcpCfg.Mtu == nil {
|
||||
mtu := DefaultMtu
|
||||
kp.kcpCfg.Mtu = &mtu
|
||||
}
|
||||
|
||||
if kp.kcpCfg.SndWndSize == nil {
|
||||
sndWndSize := DefaultSndWndSize
|
||||
kp.kcpCfg.SndWndSize = &sndWndSize
|
||||
}
|
||||
if kp.kcpCfg.RcvWndSize == nil {
|
||||
rcvWndSize := DefaultRcvWndSize
|
||||
kp.kcpCfg.RcvWndSize = &rcvWndSize
|
||||
}
|
||||
if kp.kcpCfg.ReadDeadlineMill == nil {
|
||||
readDeadlineMill := DefaultReadDeadlineMill
|
||||
kp.kcpCfg.ReadDeadlineMill = &readDeadlineMill
|
||||
} else {
|
||||
*kp.kcpCfg.ReadDeadlineMill *= time.Millisecond
|
||||
}
|
||||
if kp.kcpCfg.WriteDeadlineMill == nil {
|
||||
writeDeadlineMill := DefaultWriteDeadlineMill
|
||||
kp.kcpCfg.WriteDeadlineMill = &writeDeadlineMill
|
||||
} else {
|
||||
*kp.kcpCfg.WriteDeadlineMill *= time.Millisecond
|
||||
}
|
||||
if kp.kcpCfg.StreamMode == nil {
|
||||
streamMode := DefaultStreamMode
|
||||
kp.kcpCfg.StreamMode = &streamMode
|
||||
}
|
||||
if kp.kcpCfg.DataShards == nil {
|
||||
dataShards := DefaultDataShards
|
||||
kp.kcpCfg.DataShards = &dataShards
|
||||
}
|
||||
if kp.kcpCfg.ParityShards == nil {
|
||||
parityShards := DefaultParityShards
|
||||
kp.kcpCfg.ParityShards = &parityShards
|
||||
}
|
||||
if kp.kcpCfg.DSCP == nil {
|
||||
dss := DefaultDSCP
|
||||
kp.kcpCfg.DSCP = &dss
|
||||
}
|
||||
|
||||
if kp.kcpCfg.MaxConnNum == 0 {
|
||||
kp.kcpCfg.MaxConnNum = DefaultMaxConnNum
|
||||
}
|
||||
|
||||
kp.conns = make(ConnSet, 2048)
|
||||
kp.msgParser.Init()
|
||||
return
|
||||
}
|
||||
|
||||
func (kp *KCPServer) Start() error {
|
||||
listener, err := kcp.ListenWithOptions(kp.kcpCfg.ListenAddr, kp.blockCrypt, *kp.kcpCfg.DataShards, *kp.kcpCfg.ParityShards)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if kp.kcpCfg.ReadBuffSize != nil {
|
||||
err = listener.SetReadBuffer(*kp.kcpCfg.ReadBuffSize)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if kp.kcpCfg.WriteBuffSize != nil {
|
||||
err = listener.SetWriteBuffer(*kp.kcpCfg.WriteBuffSize)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
err = listener.SetDSCP(*kp.kcpCfg.DSCP)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
kp.listener = listener
|
||||
|
||||
kp.wgLn.Add(1)
|
||||
go func() {
|
||||
defer kp.wgLn.Done()
|
||||
for kp.run(listener) {
|
||||
}
|
||||
}()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (kp *KCPServer) initSession(session *kcp.UDPSession) {
|
||||
session.SetStreamMode(*kp.kcpCfg.StreamMode)
|
||||
session.SetWindowSize(*kp.kcpCfg.SndWndSize, *kp.kcpCfg.RcvWndSize)
|
||||
session.SetNoDelay(kp.kcpCfg.NoDelay.NoDelay, kp.kcpCfg.NoDelay.IntervalMill, kp.kcpCfg.NoDelay.Resend, kp.kcpCfg.NoDelay.CongestionControl)
|
||||
session.SetDSCP(*kp.kcpCfg.DSCP)
|
||||
session.SetMtu(*kp.kcpCfg.Mtu)
|
||||
session.SetACKNoDelay(false)
|
||||
|
||||
//session.SetWriteDeadline(time.Now().Add(*kp.kcpCfg.WriteDeadlineMill))
|
||||
}
|
||||
|
||||
func (kp *KCPServer) run(listener *kcp.Listener) bool {
|
||||
conn, err := listener.Accept()
|
||||
if err != nil {
|
||||
log.Error("accept error", log.String("ListenAddr", kp.kcpCfg.ListenAddr), log.ErrorAttr("err", err))
|
||||
return false
|
||||
}
|
||||
|
||||
kp.mutexConns.Lock()
|
||||
if len(kp.conns) >= kp.kcpCfg.MaxConnNum {
|
||||
kp.mutexConns.Unlock()
|
||||
conn.Close()
|
||||
log.Warning("too many connections")
|
||||
return true
|
||||
}
|
||||
kp.conns[conn] = struct{}{}
|
||||
kp.mutexConns.Unlock()
|
||||
|
||||
if kp.kcpCfg.ReadBuffSize != nil {
|
||||
conn.(*kcp.UDPSession).SetReadBuffer(*kp.kcpCfg.ReadBuffSize)
|
||||
}
|
||||
if kp.kcpCfg.WriteBuffSize != nil {
|
||||
conn.(*kcp.UDPSession).SetWriteBuffer(*kp.kcpCfg.WriteBuffSize)
|
||||
}
|
||||
kp.initSession(conn.(*kcp.UDPSession))
|
||||
|
||||
netConn := newNetConn(conn, kp.kcpCfg.PendingWriteNum, &kp.msgParser, *kp.kcpCfg.WriteDeadlineMill)
|
||||
agent := kp.NewAgent(netConn)
|
||||
kp.wgConns.Add(1)
|
||||
go func() {
|
||||
agent.Run()
|
||||
// cleanup
|
||||
conn.Close()
|
||||
kp.mutexConns.Lock()
|
||||
delete(kp.conns, conn)
|
||||
kp.mutexConns.Unlock()
|
||||
agent.OnClose()
|
||||
|
||||
kp.wgConns.Done()
|
||||
}()
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func (kp *KCPServer) Close() {
|
||||
kp.listener.Close()
|
||||
kp.wgLn.Wait()
|
||||
|
||||
kp.mutexConns.Lock()
|
||||
for conn := range kp.conns {
|
||||
conn.Close()
|
||||
}
|
||||
kp.conns = nil
|
||||
kp.mutexConns.Unlock()
|
||||
kp.wgConns.Wait()
|
||||
}
|
||||
@@ -24,7 +24,7 @@ type JsonProcessor struct {
|
||||
unknownMessageHandler UnknownMessageJsonHandler
|
||||
connectHandler ConnectJsonHandler
|
||||
disconnectHandler ConnectJsonHandler
|
||||
bytespool.IBytesMempool
|
||||
bytespool.IBytesMemPool
|
||||
}
|
||||
|
||||
type JsonPackInfo struct {
|
||||
@@ -35,7 +35,7 @@ type JsonPackInfo struct {
|
||||
|
||||
func NewJsonProcessor() *JsonProcessor {
|
||||
processor := &JsonProcessor{mapMsg: map[uint16]MessageJsonInfo{}}
|
||||
processor.IBytesMempool = bytespool.NewMemAreaPool()
|
||||
processor.IBytesMemPool = bytespool.NewMemAreaPool()
|
||||
|
||||
return processor
|
||||
}
|
||||
@@ -44,12 +44,14 @@ func (jsonProcessor *JsonProcessor) SetByteOrder(littleEndian bool) {
|
||||
jsonProcessor.LittleEndian = littleEndian
|
||||
}
|
||||
|
||||
// must goroutine safe
|
||||
func (jsonProcessor *JsonProcessor ) MsgRoute(clientId string,msg interface{}) error{
|
||||
// MsgRoute must goroutine safe
|
||||
func (jsonProcessor *JsonProcessor) MsgRoute(clientId string, msg interface{}, recyclerReaderBytes func(data []byte)) error {
|
||||
pPackInfo := msg.(*JsonPackInfo)
|
||||
defer recyclerReaderBytes(pPackInfo.rawMsg)
|
||||
|
||||
v, ok := jsonProcessor.mapMsg[pPackInfo.typ]
|
||||
if ok == false {
|
||||
return fmt.Errorf("cannot find msgtype %d is register!",pPackInfo.typ)
|
||||
return fmt.Errorf("cannot find msgtype %d is register", pPackInfo.typ)
|
||||
}
|
||||
|
||||
v.msgHandler(clientId, pPackInfo.msg)
|
||||
@@ -57,8 +59,9 @@ func (jsonProcessor *JsonProcessor ) MsgRoute(clientId string,msg interface{}) e
|
||||
}
|
||||
|
||||
func (jsonProcessor *JsonProcessor) Unmarshal(clientId string, data []byte) (interface{}, error) {
|
||||
typeStruct := struct {Type int `json:"typ"`}{}
|
||||
defer jsonProcessor.ReleaseBytes(data)
|
||||
typeStruct := struct {
|
||||
Type int `json:"typ"`
|
||||
}{}
|
||||
err := json.Unmarshal(data, &typeStruct)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -67,7 +70,7 @@ func (jsonProcessor *JsonProcessor) Unmarshal(clientId string,data []byte) (inte
|
||||
msgType := uint16(typeStruct.Type)
|
||||
info, ok := jsonProcessor.mapMsg[msgType]
|
||||
if ok == false {
|
||||
return nil,fmt.Errorf("Cannot find register %d msgType!",msgType)
|
||||
return nil, fmt.Errorf("cannot find register %d msgType", msgType)
|
||||
}
|
||||
|
||||
msgData := reflect.New(info.msgType.Elem()).Interface()
|
||||
@@ -76,7 +79,7 @@ func (jsonProcessor *JsonProcessor) Unmarshal(clientId string,data []byte) (inte
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &JsonPackInfo{typ:msgType,msg:msgData},nil
|
||||
return &JsonPackInfo{typ: msgType, msg: msgData, rawMsg: data}, nil
|
||||
}
|
||||
|
||||
func (jsonProcessor *JsonProcessor) Marshal(clientId string, msg interface{}) ([]byte, error) {
|
||||
@@ -88,12 +91,12 @@ func (jsonProcessor *JsonProcessor) Marshal(clientId string,msg interface{}) ([]
|
||||
return rawMsg, nil
|
||||
}
|
||||
|
||||
func (jsonProcessor *JsonProcessor) Register(msgtype uint16,msg interface{},handle MessageJsonHandler) {
|
||||
func (jsonProcessor *JsonProcessor) Register(msgType uint16, msg interface{}, handle MessageJsonHandler) {
|
||||
var info MessageJsonInfo
|
||||
|
||||
info.msgType = reflect.TypeOf(msg)
|
||||
info.msgHandler = handle
|
||||
jsonProcessor.mapMsg[msgtype] = info
|
||||
jsonProcessor.mapMsg[msgType] = info
|
||||
}
|
||||
|
||||
func (jsonProcessor *JsonProcessor) MakeMsg(msgType uint16, msg interface{}) *JsonPackInfo {
|
||||
@@ -104,7 +107,8 @@ func (jsonProcessor *JsonProcessor) MakeRawMsg(msgType uint16,msg []byte) *JsonP
|
||||
return &JsonPackInfo{typ: msgType, rawMsg: msg}
|
||||
}
|
||||
|
||||
func (jsonProcessor *JsonProcessor) UnknownMsgRoute(clientId string,msg interface{}){
|
||||
func (jsonProcessor *JsonProcessor) UnknownMsgRoute(clientId string, msg interface{}, recyclerReaderBytes func(data []byte)) {
|
||||
defer recyclerReaderBytes(msg.([]byte))
|
||||
if jsonProcessor.unknownMessageHandler == nil {
|
||||
log.Debug("Unknown message", log.String("clientId", clientId))
|
||||
return
|
||||
|
||||
@@ -26,7 +26,7 @@ type PBProcessor struct {
|
||||
unknownMessageHandler UnknownMessageHandler
|
||||
connectHandler ConnectHandler
|
||||
disconnectHandler ConnectHandler
|
||||
bytespool.IBytesMempool
|
||||
bytespool.IBytesMemPool
|
||||
}
|
||||
|
||||
type PBPackInfo struct {
|
||||
@@ -37,7 +37,7 @@ type PBPackInfo struct {
|
||||
|
||||
func NewPBProcessor() *PBProcessor {
|
||||
processor := &PBProcessor{mapMsg: map[uint16]MessageInfo{}}
|
||||
processor.IBytesMempool = bytespool.NewMemAreaPool()
|
||||
processor.IBytesMemPool = bytespool.NewMemAreaPool()
|
||||
return processor
|
||||
}
|
||||
|
||||
@@ -53,25 +53,26 @@ func (slf *PBPackInfo) GetMsg() proto.Message {
|
||||
return slf.msg
|
||||
}
|
||||
|
||||
// must goroutine safe
|
||||
func (pbProcessor *PBProcessor) MsgRoute(clientId string, msg interface{}) error {
|
||||
// MsgRoute must goroutine safe
|
||||
func (pbProcessor *PBProcessor) MsgRoute(clientId string, msg interface{}, recyclerReaderBytes func(data []byte)) error {
|
||||
pPackInfo := msg.(*PBPackInfo)
|
||||
defer recyclerReaderBytes(pPackInfo.rawMsg)
|
||||
|
||||
v, ok := pbProcessor.mapMsg[pPackInfo.typ]
|
||||
if ok == false {
|
||||
return fmt.Errorf("Cannot find msgtype %d is register!", pPackInfo.typ)
|
||||
return fmt.Errorf("cannot find msgtype %d is register", pPackInfo.typ)
|
||||
}
|
||||
|
||||
v.msgHandler(clientId, pPackInfo.msg)
|
||||
return nil
|
||||
}
|
||||
|
||||
// must goroutine safe
|
||||
// Unmarshal must goroutine safe
|
||||
func (pbProcessor *PBProcessor) Unmarshal(clientId string, data []byte) (interface{}, error) {
|
||||
defer pbProcessor.ReleaseBytes(data)
|
||||
return pbProcessor.UnmarshalWithOutRelease(clientId, data)
|
||||
}
|
||||
|
||||
// unmarshal but not release data
|
||||
// UnmarshalWithOutRelease not release data
|
||||
func (pbProcessor *PBProcessor) UnmarshalWithOutRelease(clientId string, data []byte) (interface{}, error) {
|
||||
var msgType uint16
|
||||
if pbProcessor.LittleEndian == true {
|
||||
@@ -82,7 +83,7 @@ func (pbProcessor *PBProcessor) UnmarshalWithOutRelease(clientId string, data []
|
||||
|
||||
info, ok := pbProcessor.mapMsg[msgType]
|
||||
if ok == false {
|
||||
return nil, fmt.Errorf("cannot find register %d msgtype!", msgType)
|
||||
return nil, fmt.Errorf("cannot find register %d msgtype", msgType)
|
||||
}
|
||||
msg := reflect.New(info.msgType.Elem()).Interface()
|
||||
protoMsg := msg.(proto.Message)
|
||||
@@ -91,10 +92,10 @@ func (pbProcessor *PBProcessor) UnmarshalWithOutRelease(clientId string, data []
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &PBPackInfo{typ: msgType, msg: protoMsg}, nil
|
||||
return &PBPackInfo{typ: msgType, msg: protoMsg, rawMsg: data}, nil
|
||||
}
|
||||
|
||||
// must goroutine safe
|
||||
// Marshal must goroutine safe
|
||||
func (pbProcessor *PBProcessor) Marshal(clientId string, msg interface{}) ([]byte, error) {
|
||||
pMsg := msg.(*PBPackInfo)
|
||||
|
||||
@@ -117,12 +118,12 @@ func (pbProcessor *PBProcessor) Marshal(clientId string, msg interface{}) ([]byt
|
||||
return buff, nil
|
||||
}
|
||||
|
||||
func (pbProcessor *PBProcessor) Register(msgtype uint16, msg proto.Message, handle MessageHandler) {
|
||||
func (pbProcessor *PBProcessor) Register(msgType uint16, msg proto.Message, handle MessageHandler) {
|
||||
var info MessageInfo
|
||||
|
||||
info.msgType = reflect.TypeOf(msg.(proto.Message))
|
||||
info.msgHandler = handle
|
||||
pbProcessor.mapMsg[msgtype] = info
|
||||
pbProcessor.mapMsg[msgType] = info
|
||||
}
|
||||
|
||||
func (pbProcessor *PBProcessor) MakeMsg(msgType uint16, protoMsg proto.Message) *PBPackInfo {
|
||||
@@ -133,11 +134,11 @@ func (pbProcessor *PBProcessor) MakeRawMsg(msgType uint16, msg []byte) *PBPackIn
|
||||
return &PBPackInfo{typ: msgType, rawMsg: msg}
|
||||
}
|
||||
|
||||
func (pbProcessor *PBProcessor) UnknownMsgRoute(clientId string, msg interface{}) {
|
||||
func (pbProcessor *PBProcessor) UnknownMsgRoute(clientId string, msg interface{}, recyclerReaderBytes func(data []byte)) {
|
||||
pbProcessor.unknownMessageHandler(clientId, msg.([]byte))
|
||||
recyclerReaderBytes(msg.([]byte))
|
||||
}
|
||||
|
||||
// connect event
|
||||
func (pbProcessor *PBProcessor) ConnectedRoute(clientId string) {
|
||||
pbProcessor.connectHandler(clientId)
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@ type RawConnectHandler func(clientId string)
|
||||
type UnknownRawMessageHandler func(clientId string, msg []byte)
|
||||
|
||||
const RawMsgTypeSize = 2
|
||||
|
||||
type PBRawProcessor struct {
|
||||
msgHandler RawMessageHandler
|
||||
LittleEndian bool
|
||||
@@ -37,14 +38,16 @@ func (pbRawProcessor *PBRawProcessor) SetByteOrder(littleEndian bool) {
|
||||
pbRawProcessor.LittleEndian = littleEndian
|
||||
}
|
||||
|
||||
// must goroutine safe
|
||||
func (pbRawProcessor *PBRawProcessor ) MsgRoute(clientId string, msg interface{}) error{
|
||||
// MsgRoute must goroutine safe
|
||||
func (pbRawProcessor *PBRawProcessor) MsgRoute(clientId string, msg interface{}, recyclerReaderBytes func(data []byte)) error {
|
||||
pPackInfo := msg.(*PBRawPackInfo)
|
||||
pbRawProcessor.msgHandler(clientId, pPackInfo.typ, pPackInfo.rawMsg)
|
||||
recyclerReaderBytes(pPackInfo.rawMsg)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// must goroutine safe
|
||||
// Unmarshal must goroutine safe
|
||||
func (pbRawProcessor *PBRawProcessor) Unmarshal(clientId string, data []byte) (interface{}, error) {
|
||||
var msgType uint16
|
||||
if pbRawProcessor.LittleEndian == true {
|
||||
@@ -56,7 +59,7 @@ func (pbRawProcessor *PBRawProcessor ) Unmarshal(clientId string,data []byte) (i
|
||||
return &PBRawPackInfo{typ: msgType, rawMsg: data}, nil
|
||||
}
|
||||
|
||||
// must goroutine safe
|
||||
// Marshal must goroutine safe
|
||||
func (pbRawProcessor *PBRawProcessor) Marshal(clientId string, msg interface{}) ([]byte, error) {
|
||||
pMsg := msg.(*PBRawPackInfo)
|
||||
|
||||
@@ -80,14 +83,14 @@ func (pbRawProcessor *PBRawProcessor) MakeRawMsg(msgType uint16,msg []byte,pbRaw
|
||||
pbRawPackInfo.rawMsg = msg
|
||||
}
|
||||
|
||||
func (pbRawProcessor *PBRawProcessor) UnknownMsgRoute(clientId string,msg interface{}){
|
||||
func (pbRawProcessor *PBRawProcessor) UnknownMsgRoute(clientId string, msg interface{}, recyclerReaderBytes func(data []byte)) {
|
||||
defer recyclerReaderBytes(msg.([]byte))
|
||||
if pbRawProcessor.unknownMessageHandler == nil {
|
||||
return
|
||||
}
|
||||
pbRawProcessor.unknownMessageHandler(clientId, msg.([]byte))
|
||||
}
|
||||
|
||||
// connect event
|
||||
func (pbRawProcessor *PBRawProcessor) ConnectedRoute(clientId string) {
|
||||
pbRawProcessor.connectHandler(clientId)
|
||||
}
|
||||
|
||||
@@ -1,18 +1,17 @@
|
||||
package processor
|
||||
|
||||
|
||||
type IProcessor interface {
|
||||
// must goroutine safe
|
||||
MsgRoute(clientId string,msg interface{}) error
|
||||
//must goroutine safe
|
||||
UnknownMsgRoute(clientId string,msg interface{})
|
||||
// connect event
|
||||
// MsgRoute must goroutine safe
|
||||
MsgRoute(clientId string, msg interface{}, recyclerReaderBytes func(data []byte)) error
|
||||
// UnknownMsgRoute must goroutine safe
|
||||
UnknownMsgRoute(clientId string, msg interface{}, recyclerReaderBytes func(data []byte))
|
||||
// ConnectedRoute connect event
|
||||
ConnectedRoute(clientId string)
|
||||
DisConnectedRoute(clientId string)
|
||||
|
||||
// must goroutine safe
|
||||
// Unmarshal must goroutine safe
|
||||
Unmarshal(clientId string, data []byte) (interface{}, error)
|
||||
// must goroutine safe
|
||||
// Marshal must goroutine safe
|
||||
Marshal(clientId string, msg interface{}) ([]byte, error)
|
||||
}
|
||||
|
||||
@@ -26,4 +25,3 @@ type IRawProcessor interface {
|
||||
SetConnectedHandler(connectHandler RawConnectHandler)
|
||||
SetDisConnectedHandler(disconnectHandler RawConnectHandler)
|
||||
}
|
||||
|
||||
|
||||
@@ -16,7 +16,7 @@ type TCPClient struct {
|
||||
ReadDeadline time.Duration
|
||||
WriteDeadline time.Duration
|
||||
AutoReconnect bool
|
||||
NewAgent func(*TCPConn) Agent
|
||||
NewAgent func(conn *NetConn) Agent
|
||||
cons ConnSet
|
||||
wg sync.WaitGroup
|
||||
closeFlag bool
|
||||
@@ -74,7 +74,7 @@ func (client *TCPClient) init() {
|
||||
if client.LenMsgLen == 0 {
|
||||
client.LenMsgLen = Default_LenMsgLen
|
||||
}
|
||||
maxMsgLen := client.MsgParser.getMaxMsgLen(client.LenMsgLen)
|
||||
maxMsgLen := client.MsgParser.getMaxMsgLen()
|
||||
if client.MaxMsgLen > maxMsgLen {
|
||||
client.MaxMsgLen = maxMsgLen
|
||||
log.Info("invalid MaxMsgLen", log.Uint32("reset", maxMsgLen))
|
||||
@@ -82,7 +82,7 @@ func (client *TCPClient) init() {
|
||||
|
||||
client.cons = make(ConnSet)
|
||||
client.closeFlag = false
|
||||
client.MsgParser.init()
|
||||
client.MsgParser.Init()
|
||||
}
|
||||
|
||||
func (client *TCPClient) GetCloseFlag() bool {
|
||||
@@ -126,7 +126,7 @@ reconnect:
|
||||
client.cons[conn] = struct{}{}
|
||||
client.Unlock()
|
||||
|
||||
tcpConn := newTCPConn(conn, client.PendingWriteNum, &client.MsgParser,client.WriteDeadline)
|
||||
tcpConn := newNetConn(conn, client.PendingWriteNum, &client.MsgParser, client.WriteDeadline)
|
||||
agent := client.NewAgent(tcpConn)
|
||||
agent.Run()
|
||||
|
||||
@@ -156,4 +156,3 @@ func (client *TCPClient) Close(waitDone bool) {
|
||||
client.wg.Wait()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,162 +0,0 @@
|
||||
package network
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/duanhf2012/origin/v2/log"
|
||||
"net"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
)
|
||||
|
||||
type ConnSet map[net.Conn]struct{}
|
||||
|
||||
type TCPConn struct {
|
||||
sync.Mutex
|
||||
conn net.Conn
|
||||
writeChan chan []byte
|
||||
closeFlag int32
|
||||
msgParser *MsgParser
|
||||
}
|
||||
|
||||
func freeChannel(conn *TCPConn){
|
||||
for;len(conn.writeChan)>0;{
|
||||
byteBuff := <- conn.writeChan
|
||||
if byteBuff != nil {
|
||||
conn.ReleaseReadMsg(byteBuff)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func newTCPConn(conn net.Conn, pendingWriteNum int, msgParser *MsgParser,writeDeadline time.Duration) *TCPConn {
|
||||
tcpConn := new(TCPConn)
|
||||
tcpConn.conn = conn
|
||||
tcpConn.writeChan = make(chan []byte, pendingWriteNum)
|
||||
tcpConn.msgParser = msgParser
|
||||
go func() {
|
||||
for b := range tcpConn.writeChan {
|
||||
if b == nil {
|
||||
break
|
||||
}
|
||||
|
||||
conn.SetWriteDeadline(time.Now().Add(writeDeadline))
|
||||
_, err := conn.Write(b)
|
||||
tcpConn.msgParser.ReleaseBytes(b)
|
||||
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
}
|
||||
conn.Close()
|
||||
tcpConn.Lock()
|
||||
freeChannel(tcpConn)
|
||||
atomic.StoreInt32(&tcpConn.closeFlag,1)
|
||||
tcpConn.Unlock()
|
||||
}()
|
||||
|
||||
return tcpConn
|
||||
}
|
||||
|
||||
func (tcpConn *TCPConn) doDestroy() {
|
||||
tcpConn.conn.(*net.TCPConn).SetLinger(0)
|
||||
tcpConn.conn.Close()
|
||||
|
||||
if atomic.LoadInt32(&tcpConn.closeFlag)==0 {
|
||||
close(tcpConn.writeChan)
|
||||
atomic.StoreInt32(&tcpConn.closeFlag,1)
|
||||
}
|
||||
}
|
||||
|
||||
func (tcpConn *TCPConn) Destroy() {
|
||||
tcpConn.Lock()
|
||||
defer tcpConn.Unlock()
|
||||
|
||||
tcpConn.doDestroy()
|
||||
}
|
||||
|
||||
func (tcpConn *TCPConn) Close() {
|
||||
tcpConn.Lock()
|
||||
defer tcpConn.Unlock()
|
||||
if atomic.LoadInt32(&tcpConn.closeFlag)==1 {
|
||||
return
|
||||
}
|
||||
|
||||
tcpConn.doWrite(nil)
|
||||
atomic.StoreInt32(&tcpConn.closeFlag,1)
|
||||
}
|
||||
|
||||
func (tcpConn *TCPConn) GetRemoteIp() string {
|
||||
return tcpConn.conn.RemoteAddr().String()
|
||||
}
|
||||
|
||||
func (tcpConn *TCPConn) doWrite(b []byte) error{
|
||||
if len(tcpConn.writeChan) == cap(tcpConn.writeChan) {
|
||||
tcpConn.ReleaseReadMsg(b)
|
||||
log.Error("close conn: channel full")
|
||||
tcpConn.doDestroy()
|
||||
return errors.New("close conn: channel full")
|
||||
}
|
||||
|
||||
tcpConn.writeChan <- b
|
||||
return nil
|
||||
}
|
||||
|
||||
// b must not be modified by the others goroutines
|
||||
func (tcpConn *TCPConn) Write(b []byte) error{
|
||||
tcpConn.Lock()
|
||||
defer tcpConn.Unlock()
|
||||
if atomic.LoadInt32(&tcpConn.closeFlag)==1 || b == nil {
|
||||
tcpConn.ReleaseReadMsg(b)
|
||||
return errors.New("conn is close")
|
||||
}
|
||||
|
||||
return tcpConn.doWrite(b)
|
||||
}
|
||||
|
||||
func (tcpConn *TCPConn) Read(b []byte) (int, error) {
|
||||
return tcpConn.conn.Read(b)
|
||||
}
|
||||
|
||||
func (tcpConn *TCPConn) LocalAddr() net.Addr {
|
||||
return tcpConn.conn.LocalAddr()
|
||||
}
|
||||
|
||||
func (tcpConn *TCPConn) RemoteAddr() net.Addr {
|
||||
return tcpConn.conn.RemoteAddr()
|
||||
}
|
||||
|
||||
func (tcpConn *TCPConn) ReadMsg() ([]byte, error) {
|
||||
return tcpConn.msgParser.Read(tcpConn)
|
||||
}
|
||||
|
||||
func (tcpConn *TCPConn) ReleaseReadMsg(byteBuff []byte){
|
||||
tcpConn.msgParser.ReleaseBytes(byteBuff)
|
||||
}
|
||||
|
||||
func (tcpConn *TCPConn) WriteMsg(args ...[]byte) error {
|
||||
if atomic.LoadInt32(&tcpConn.closeFlag) == 1 {
|
||||
return errors.New("conn is close")
|
||||
}
|
||||
return tcpConn.msgParser.Write(tcpConn, args...)
|
||||
}
|
||||
|
||||
func (tcpConn *TCPConn) WriteRawMsg(args []byte) error {
|
||||
if atomic.LoadInt32(&tcpConn.closeFlag) == 1 {
|
||||
return errors.New("conn is close")
|
||||
}
|
||||
|
||||
return tcpConn.Write(args)
|
||||
}
|
||||
|
||||
|
||||
func (tcpConn *TCPConn) IsConnected() bool {
|
||||
return atomic.LoadInt32(&tcpConn.closeFlag) == 0
|
||||
}
|
||||
|
||||
func (tcpConn *TCPConn) SetReadDeadline(d time.Duration) {
|
||||
tcpConn.conn.SetReadDeadline(time.Now().Add(d))
|
||||
}
|
||||
|
||||
func (tcpConn *TCPConn) SetWriteDeadline(d time.Duration) {
|
||||
tcpConn.conn.SetWriteDeadline(time.Now().Add(d))
|
||||
}
|
||||
@@ -8,7 +8,7 @@ import (
|
||||
"math"
|
||||
)
|
||||
|
||||
// --------------
|
||||
// MsgParser --------------
|
||||
// | len | data |
|
||||
// --------------
|
||||
type MsgParser struct {
|
||||
@@ -17,11 +17,10 @@ type MsgParser struct {
|
||||
MaxMsgLen uint32
|
||||
LittleEndian bool
|
||||
|
||||
bytespool.IBytesMempool
|
||||
bytespool.IBytesMemPool
|
||||
}
|
||||
|
||||
|
||||
func (p *MsgParser) getMaxMsgLen(lenMsgLen int) uint32 {
|
||||
func (p *MsgParser) getMaxMsgLen() uint32 {
|
||||
switch p.LenMsgLen {
|
||||
case 1:
|
||||
return math.MaxUint8
|
||||
@@ -34,17 +33,17 @@ func (p *MsgParser) getMaxMsgLen(lenMsgLen int) uint32 {
|
||||
}
|
||||
}
|
||||
|
||||
func (p *MsgParser) init(){
|
||||
p.IBytesMempool = bytespool.NewMemAreaPool()
|
||||
func (p *MsgParser) Init() {
|
||||
p.IBytesMemPool = bytespool.NewMemAreaPool()
|
||||
}
|
||||
|
||||
// goroutine safe
|
||||
func (p *MsgParser) Read(conn *TCPConn) ([]byte, error) {
|
||||
func (p *MsgParser) Read(r io.Reader) ([]byte, error) {
|
||||
var b [4]byte
|
||||
bufMsgLen := b[:p.LenMsgLen]
|
||||
|
||||
// read len
|
||||
if _, err := io.ReadFull(conn, bufMsgLen); err != nil {
|
||||
if _, err := io.ReadFull(r, bufMsgLen); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -76,7 +75,7 @@ func (p *MsgParser) Read(conn *TCPConn) ([]byte, error) {
|
||||
|
||||
// data
|
||||
msgData := p.MakeBytes(int(msgLen))
|
||||
if _, err := io.ReadFull(conn, msgData[:msgLen]); err != nil {
|
||||
if _, err := io.ReadFull(r, msgData[:msgLen]); err != nil {
|
||||
p.ReleaseBytes(msgData)
|
||||
return nil, err
|
||||
}
|
||||
@@ -85,7 +84,7 @@ func (p *MsgParser) Read(conn *TCPConn) ([]byte, error) {
|
||||
}
|
||||
|
||||
// goroutine safe
|
||||
func (p *MsgParser) Write(conn *TCPConn, args ...[]byte) error {
|
||||
func (p *MsgParser) Write(conn io.Writer, args ...[]byte) error {
|
||||
// get len
|
||||
var msgLen uint32
|
||||
for i := 0; i < len(args); i++ {
|
||||
@@ -130,3 +129,9 @@ func (p *MsgParser) Write(conn *TCPConn, args ...[]byte) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *MsgParser) GetRecyclerReaderBytes() func(data []byte) {
|
||||
return func(data []byte) {
|
||||
p.IBytesMemPool.ReleaseBytes(data)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
package network
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/duanhf2012/origin/v2/log"
|
||||
"github.com/duanhf2012/origin/v2/util/bytespool"
|
||||
"net"
|
||||
"sync"
|
||||
"time"
|
||||
"fmt"
|
||||
"errors"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -15,7 +15,6 @@ const(
|
||||
Default_WriteDeadline = time.Second * 30 //默认写超时30s
|
||||
Default_MaxConnNum = 1000000 //默认最大连接数
|
||||
Default_PendingWriteNum = 100000 //单连接写消息Channel容量
|
||||
Default_LittleEndian = false //默认大小端
|
||||
Default_MinMsgLen = 2 //最小消息长度2byte
|
||||
Default_LenMsgLen = 2 //包头字段长度占用2byte
|
||||
Default_MaxMsgLen = 65535 //最大消息长度
|
||||
@@ -28,7 +27,7 @@ type TCPServer struct {
|
||||
ReadDeadline time.Duration
|
||||
WriteDeadline time.Duration
|
||||
|
||||
NewAgent func(*TCPConn) Agent
|
||||
NewAgent func(conn Conn) Agent
|
||||
ln net.Listener
|
||||
conns ConnSet
|
||||
mutexConns sync.Mutex
|
||||
@@ -43,6 +42,8 @@ func (server *TCPServer) Start() error{
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
server.wgLn.Add(1)
|
||||
go server.run()
|
||||
|
||||
return nil
|
||||
@@ -51,7 +52,7 @@ func (server *TCPServer) Start() error{
|
||||
func (server *TCPServer) init() error {
|
||||
ln, err := net.Listen("tcp", server.Addr)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Listen tcp fail,error:%s",err.Error())
|
||||
return fmt.Errorf("listen tcp fail,error:%s", err.Error())
|
||||
}
|
||||
|
||||
if server.MaxConnNum <= 0 {
|
||||
@@ -74,7 +75,7 @@ func (server *TCPServer) init() error{
|
||||
log.Info("invalid MaxMsgLen", log.Uint32("reset to", server.MaxMsgLen))
|
||||
}
|
||||
|
||||
maxMsgLen := server.MsgParser.getMaxMsgLen(server.LenMsgLen)
|
||||
maxMsgLen := server.MsgParser.getMaxMsgLen()
|
||||
if server.MaxMsgLen > maxMsgLen {
|
||||
server.MaxMsgLen = maxMsgLen
|
||||
log.Info("invalid MaxMsgLen", log.Uint32("reset", maxMsgLen))
|
||||
@@ -100,44 +101,44 @@ func (server *TCPServer) init() error{
|
||||
}
|
||||
|
||||
server.ln = ln
|
||||
server.conns = make(ConnSet)
|
||||
server.MsgParser.init()
|
||||
server.conns = make(ConnSet, 2048)
|
||||
server.MsgParser.Init()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (server *TCPServer) SetNetMempool(mempool bytespool.IBytesMempool){
|
||||
server.IBytesMempool = mempool
|
||||
func (server *TCPServer) SetNetMemPool(memPool bytespool.IBytesMemPool) {
|
||||
server.IBytesMemPool = memPool
|
||||
}
|
||||
|
||||
func (server *TCPServer) GetNetMempool() bytespool.IBytesMempool {
|
||||
return server.IBytesMempool
|
||||
func (server *TCPServer) GetNetMemPool() bytespool.IBytesMemPool {
|
||||
return server.IBytesMemPool
|
||||
}
|
||||
|
||||
func (server *TCPServer) run() {
|
||||
server.wgLn.Add(1)
|
||||
defer server.wgLn.Done()
|
||||
|
||||
var tempDelay time.Duration
|
||||
for {
|
||||
conn, err := server.ln.Accept()
|
||||
if err != nil {
|
||||
if ne, ok := err.(net.Error); ok && ne.Temporary() {
|
||||
var ne net.Error
|
||||
if errors.As(err, &ne) && ne.Timeout() {
|
||||
if tempDelay == 0 {
|
||||
tempDelay = 5 * time.Millisecond
|
||||
} else {
|
||||
tempDelay *= 2
|
||||
}
|
||||
if max := 1 * time.Second; tempDelay > max {
|
||||
tempDelay = max
|
||||
}
|
||||
|
||||
log.Info("accept fail", log.String("error", err.Error()), log.Duration("sleep time", tempDelay))
|
||||
tempDelay = min(1*time.Second, tempDelay)
|
||||
time.Sleep(tempDelay)
|
||||
continue
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
conn.(*net.TCPConn).SetLinger(0)
|
||||
conn.(*net.TCPConn).SetNoDelay(true)
|
||||
tempDelay = 0
|
||||
|
||||
@@ -153,7 +154,7 @@ func (server *TCPServer) run() {
|
||||
server.mutexConns.Unlock()
|
||||
server.wgConns.Add(1)
|
||||
|
||||
tcpConn := newTCPConn(conn, server.PendingWriteNum, &server.MsgParser,server.WriteDeadline)
|
||||
tcpConn := newNetConn(conn, server.PendingWriteNum, &server.MsgParser, server.WriteDeadline)
|
||||
agent := server.NewAgent(tcpConn)
|
||||
|
||||
go func() {
|
||||
|
||||
@@ -108,7 +108,7 @@ reconnect:
|
||||
client.cons[conn] = struct{}{}
|
||||
client.Unlock()
|
||||
|
||||
wsConn := newWSConn(conn, client.PendingWriteNum, client.MaxMsgLen,client.MessageType)
|
||||
wsConn := newWSConn(conn,nil, client.PendingWriteNum, client.MaxMsgLen,client.MessageType)
|
||||
agent := client.NewAgent(wsConn)
|
||||
agent.Run()
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"github.com/duanhf2012/origin/v2/log"
|
||||
"github.com/gorilla/websocket"
|
||||
"net"
|
||||
"net/http"
|
||||
"sync"
|
||||
)
|
||||
|
||||
@@ -16,13 +17,15 @@ type WSConn struct {
|
||||
writeChan chan []byte
|
||||
maxMsgLen uint32
|
||||
closeFlag bool
|
||||
header http.Header
|
||||
}
|
||||
|
||||
func newWSConn(conn *websocket.Conn, pendingWriteNum int, maxMsgLen uint32,messageType int) *WSConn {
|
||||
func newWSConn(conn *websocket.Conn, header http.Header, pendingWriteNum int, maxMsgLen uint32, messageType int) *WSConn {
|
||||
wsConn := new(WSConn)
|
||||
wsConn.conn = conn
|
||||
wsConn.writeChan = make(chan []byte, pendingWriteNum)
|
||||
wsConn.maxMsgLen = maxMsgLen
|
||||
wsConn.header = header
|
||||
|
||||
go func() {
|
||||
for b := range wsConn.writeChan {
|
||||
@@ -46,7 +49,6 @@ func newWSConn(conn *websocket.Conn, pendingWriteNum int, maxMsgLen uint32,messa
|
||||
}
|
||||
|
||||
func (wsConn *WSConn) doDestroy() {
|
||||
wsConn.conn.UnderlyingConn().(*net.TCPConn).SetLinger(0)
|
||||
wsConn.conn.Close()
|
||||
|
||||
if !wsConn.closeFlag {
|
||||
@@ -83,6 +85,10 @@ func (wsConn *WSConn) doWrite(b []byte) {
|
||||
wsConn.writeChan <- b
|
||||
}
|
||||
|
||||
func (wsConn *WSConn) GetHeader() http.Header {
|
||||
return wsConn.header
|
||||
}
|
||||
|
||||
func (wsConn *WSConn) LocalAddr() net.Addr {
|
||||
return wsConn.conn.LocalAddr()
|
||||
}
|
||||
@@ -91,13 +97,13 @@ func (wsConn *WSConn) RemoteAddr() net.Addr {
|
||||
return wsConn.conn.RemoteAddr()
|
||||
}
|
||||
|
||||
// goroutine not safe
|
||||
// ReadMsg goroutine not safe
|
||||
func (wsConn *WSConn) ReadMsg() ([]byte, error) {
|
||||
_, b, err := wsConn.conn.ReadMessage()
|
||||
return b, err
|
||||
}
|
||||
|
||||
// args must not be modified by the others goroutines
|
||||
// WriteMsg args must not be modified by the others goroutines
|
||||
func (wsConn *WSConn) WriteMsg(args ...[]byte) error {
|
||||
wsConn.Lock()
|
||||
defer wsConn.Unlock()
|
||||
|
||||
@@ -2,6 +2,7 @@ package network
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"errors"
|
||||
"github.com/duanhf2012/origin/v2/log"
|
||||
"github.com/gorilla/websocket"
|
||||
"net"
|
||||
@@ -73,7 +74,9 @@ func (handler *WSHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
handler.conns[conn] = struct{}{}
|
||||
handler.mutexConns.Unlock()
|
||||
|
||||
wsConn := newWSConn(conn, handler.pendingWriteNum, handler.maxMsgLen, handler.messageType)
|
||||
conn.UnderlyingConn().(*net.TCPConn).SetLinger(0)
|
||||
conn.UnderlyingConn().(*net.TCPConn).SetNoDelay(true)
|
||||
wsConn := newWSConn(conn, r.Header, handler.pendingWriteNum, handler.maxMsgLen, handler.messageType)
|
||||
agent := handler.newAgent(wsConn)
|
||||
agent.Run()
|
||||
|
||||
@@ -92,10 +95,11 @@ func (server *WSServer) SetMessageType(messageType int) {
|
||||
}
|
||||
}
|
||||
|
||||
func (server *WSServer) Start() {
|
||||
func (server *WSServer) Start() error {
|
||||
ln, err := net.Listen("tcp", server.Addr)
|
||||
if err != nil {
|
||||
log.Fatal("WSServer Listen fail",log.String("error", err.Error()))
|
||||
log.Error("WSServer Listen fail", log.String("error", err.Error()))
|
||||
return err
|
||||
}
|
||||
|
||||
if server.MaxConnNum <= 0 {
|
||||
@@ -115,18 +119,19 @@ func (server *WSServer) Start() {
|
||||
log.Info("invalid HTTPTimeout", log.Duration("reset", server.HTTPTimeout))
|
||||
}
|
||||
if server.NewAgent == nil {
|
||||
log.Fatal("NewAgent must not be nil")
|
||||
log.Error("NewAgent must not be nil")
|
||||
return errors.New("NewAgent must not be nil")
|
||||
}
|
||||
|
||||
if server.CertFile != "" || server.KeyFile != "" {
|
||||
config := &tls.Config{}
|
||||
config.NextProtos = []string{"http/1.1"}
|
||||
|
||||
var err error
|
||||
config.Certificates = make([]tls.Certificate, 1)
|
||||
config.Certificates[0], err = tls.LoadX509KeyPair(server.CertFile, server.KeyFile)
|
||||
if err != nil {
|
||||
log.Fatal("LoadX509KeyPair fail",log.String("error", err.Error()))
|
||||
log.Error("LoadX509KeyPair fail", log.String("error", err.Error()))
|
||||
return err
|
||||
}
|
||||
|
||||
ln = tls.NewListener(ln, config)
|
||||
@@ -155,6 +160,7 @@ func (server *WSServer) Start() {
|
||||
}
|
||||
|
||||
go httpServer.Serve(ln)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (server *WSServer) Close() {
|
||||
|
||||
84
node/node.go
84
node/node.go
@@ -9,6 +9,7 @@ import (
|
||||
"github.com/duanhf2012/origin/v2/profiler"
|
||||
"github.com/duanhf2012/origin/v2/service"
|
||||
"github.com/duanhf2012/origin/v2/util/buildtime"
|
||||
"github.com/duanhf2012/origin/v2/util/sysprocess"
|
||||
"github.com/duanhf2012/origin/v2/util/timer"
|
||||
"io"
|
||||
"net/http"
|
||||
@@ -19,15 +20,15 @@ import (
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
"github.com/duanhf2012/origin/v2/util/sysprocess"
|
||||
)
|
||||
|
||||
var sig chan os.Signal
|
||||
var nodeId string
|
||||
var preSetupService []service.IService //预安装
|
||||
var preSetupTemplateService []func() service.IService
|
||||
var profilerInterval time.Duration
|
||||
var bValid bool
|
||||
var configDir = "./config/"
|
||||
var NodeIsRun = false
|
||||
|
||||
const (
|
||||
SingleStop syscall.Signal = 10
|
||||
@@ -56,11 +57,10 @@ func init() {
|
||||
console.RegisterCommandString("loglevel", "debug", "<-loglevel debug|release|warning|error|fatal> Set loglevel.", setLevel)
|
||||
console.RegisterCommandString("logpath", "", "<-logpath path> Set log file path.", setLogPath)
|
||||
console.RegisterCommandInt("logsize", 0, "<-logsize size> Set log size(MB).", setLogSize)
|
||||
console.RegisterCommandInt("logchannelcap", 0, "<-logchannelcap num> Set log channel cap.", setLogChannelCapNum)
|
||||
console.RegisterCommandInt("logchannelcap", -1, "<-logchannelcap num> Set log channel cap.", setLogChannelCapNum)
|
||||
console.RegisterCommandString("pprof", "", "<-pprof ip:port> Open performance analysis.", setPprof)
|
||||
}
|
||||
|
||||
|
||||
func notifyAllServiceRetire() {
|
||||
service.NotifyAllServiceRetire()
|
||||
}
|
||||
@@ -81,7 +81,7 @@ func usage(val interface{}) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func setName(val interface{}) error {
|
||||
func setName(_ interface{}) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -108,7 +108,7 @@ func setConfigPath(val interface{}) error {
|
||||
}
|
||||
_, err := os.Stat(configPath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Cannot find file path %s", configPath)
|
||||
return fmt.Errorf("cannot find file path %s", configPath)
|
||||
}
|
||||
|
||||
cluster.SetConfigDir(configPath)
|
||||
@@ -117,7 +117,7 @@ func setConfigPath(val interface{}) error {
|
||||
}
|
||||
|
||||
func getRunProcessPid(nodeId string) (int, error) {
|
||||
f, err := os.OpenFile(fmt.Sprintf("%s_%d.pid", os.Args[0], nodeId), os.O_RDONLY, 0600)
|
||||
f, err := os.OpenFile(fmt.Sprintf("%s_%s.pid", os.Args[0], nodeId), os.O_RDONLY, 0600)
|
||||
defer f.Close()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
@@ -169,6 +169,31 @@ func initNode(id string) {
|
||||
serviceOrder := cluster.GetCluster().GetLocalNodeInfo().ServiceList
|
||||
for _, serviceName := range serviceOrder {
|
||||
bSetup := false
|
||||
|
||||
//判断是否有配置模板服务
|
||||
splitServiceName := strings.Split(serviceName, ":")
|
||||
if len(splitServiceName) == 2 {
|
||||
serviceName = splitServiceName[0]
|
||||
templateServiceName := splitServiceName[1]
|
||||
for _, newSer := range preSetupTemplateService {
|
||||
ser := newSer()
|
||||
ser.OnSetup(ser)
|
||||
if ser.GetName() == templateServiceName {
|
||||
ser.SetName(serviceName)
|
||||
ser.Init(ser, cluster.GetRpcClient, cluster.GetRpcServer, cluster.GetCluster().GetServiceCfg(ser.GetName()))
|
||||
service.Setup(ser)
|
||||
|
||||
bSetup = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if bSetup == false {
|
||||
log.Error("Template service not found", log.String("service name", serviceName), log.String("template service name", templateServiceName))
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
for _, s := range preSetupService {
|
||||
if s.GetName() != serviceName {
|
||||
continue
|
||||
@@ -194,9 +219,9 @@ func initLog() error {
|
||||
setLogPath("./log")
|
||||
}
|
||||
|
||||
localnodeinfo := cluster.GetCluster().GetLocalNodeInfo()
|
||||
filepre := fmt.Sprintf("%s_", localnodeinfo.NodeId)
|
||||
logger, err := log.NewTextLogger(log.LogLevel,log.LogPath,filepre,true,log.LogChannelCap)
|
||||
localNodeInfo := cluster.GetCluster().GetLocalNodeInfo()
|
||||
filePre := fmt.Sprintf("%s_", localNodeInfo.NodeId)
|
||||
logger, err := log.NewTextLogger(log.LogLevel, log.LogPath, filePre, true, log.LogChannelCap)
|
||||
if err != nil {
|
||||
fmt.Printf("cannot create log file!\n")
|
||||
return err
|
||||
@@ -213,7 +238,6 @@ func Start() {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func retireNode(args interface{}) error {
|
||||
//1.解析参数
|
||||
param := args.(string)
|
||||
@@ -238,12 +262,10 @@ func retireNode(args interface{}) error {
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
RetireProcess(processId)
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
func stopNode(args interface{}) error {
|
||||
//1.解析参数
|
||||
param := args.(string)
|
||||
@@ -301,13 +323,13 @@ func startNode(args interface{}) error {
|
||||
myName, mErr := sysprocess.GetMyProcessName()
|
||||
//当前进程名获取失败,不应该发生
|
||||
if mErr != nil {
|
||||
log.SInfo("get my process's name is error,", mErr.Error())
|
||||
log.Info("get my process's name is error", log.ErrorAttr("err", mErr))
|
||||
os.Exit(-1)
|
||||
}
|
||||
|
||||
//进程id存在,而且进程名也相同,被认为是当前进程重复运行
|
||||
if cErr == nil && name == myName {
|
||||
log.SInfo(fmt.Sprintf("repeat runs are not allowed,node is %s,processid is %d",strNodeId,processId))
|
||||
log.Info("repeat runs are not allowed", log.String("nodeId", strNodeId), log.Int("processId", processId))
|
||||
os.Exit(-1)
|
||||
}
|
||||
break
|
||||
@@ -328,13 +350,14 @@ func startNode(args interface{}) error {
|
||||
cluster.GetCluster().Start()
|
||||
|
||||
//6.监听程序退出信号&性能报告
|
||||
bRun := true
|
||||
|
||||
var pProfilerTicker *time.Ticker = &time.Ticker{}
|
||||
if profilerInterval > 0 {
|
||||
pProfilerTicker = time.NewTicker(profilerInterval)
|
||||
}
|
||||
|
||||
for bRun {
|
||||
NodeIsRun = true
|
||||
for NodeIsRun {
|
||||
select {
|
||||
case s := <-sig:
|
||||
signal := s.(syscall.Signal)
|
||||
@@ -342,7 +365,7 @@ func startNode(args interface{}) error {
|
||||
log.Info("receipt retire signal.")
|
||||
notifyAllServiceRetire()
|
||||
} else {
|
||||
bRun = false
|
||||
NodeIsRun = false
|
||||
log.Info("receipt stop signal.")
|
||||
}
|
||||
case <-pProfilerTicker.C:
|
||||
@@ -350,7 +373,6 @@ func startNode(args interface{}) error {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//7.退出
|
||||
service.StopAllService()
|
||||
cluster.GetCluster().Stop()
|
||||
@@ -360,6 +382,11 @@ func startNode(args interface{}) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
type templateServicePoint[T any] interface {
|
||||
*T
|
||||
service.IService
|
||||
}
|
||||
|
||||
func Setup(s ...service.IService) {
|
||||
for _, sv := range s {
|
||||
sv.OnSetup(sv)
|
||||
@@ -367,6 +394,19 @@ func Setup(s ...service.IService) {
|
||||
}
|
||||
}
|
||||
|
||||
func SetupTemplateFunc(fs ...func() service.IService) {
|
||||
for _, f := range fs {
|
||||
preSetupTemplateService = append(preSetupTemplateService, f)
|
||||
}
|
||||
}
|
||||
|
||||
func SetupTemplate[T any,P templateServicePoint[T]]() {
|
||||
SetupTemplateFunc(func() service.IService{
|
||||
var t T
|
||||
return P(&t)
|
||||
})
|
||||
}
|
||||
|
||||
func GetService(serviceName string) service.IService {
|
||||
return service.GetService(serviceName)
|
||||
}
|
||||
@@ -394,7 +434,7 @@ func openConsole(args interface{}) error {
|
||||
} else if strOpen == "true" {
|
||||
log.OpenConsole = true
|
||||
} else {
|
||||
return errors.New("Parameter console error!")
|
||||
return errors.New("parameter console error")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -472,6 +512,10 @@ func setLogChannelCapNum(args interface{}) error {
|
||||
return errors.New("param logsize is error")
|
||||
}
|
||||
|
||||
if logChannelCap == -1 {
|
||||
return nil
|
||||
}
|
||||
|
||||
log.LogChannelCap = logChannelCap
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -2,19 +2,21 @@ package profiler
|
||||
|
||||
import (
|
||||
"container/list"
|
||||
"fmt"
|
||||
"github.com/duanhf2012/origin/v2/log"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
//最大超长时间,一般可以认为是死锁或者死循环,或者极差的性能问题
|
||||
var DefaultMaxOvertime time.Duration = 5*time.Second
|
||||
//超过该时间将会监控报告
|
||||
var DefaultOvertime time.Duration = 10*time.Millisecond
|
||||
var DefaultMaxRecordNum int = 100 //最大记录条数
|
||||
// DefaultMaxOvertime 最大超长时间,一般可以认为是死锁或者死循环,或者极差的性能问题
|
||||
var DefaultMaxOvertime = 5 * time.Second
|
||||
|
||||
// DefaultOvertime 超过该时间将会监控报告
|
||||
var DefaultOvertime = 10 * time.Millisecond
|
||||
var DefaultMaxRecordNum = 100 //最大记录条数
|
||||
var mapProfiler map[string]*Profiler
|
||||
|
||||
type ReportFunType func(name string, callNum int, costTime time.Duration, record *list.List)
|
||||
|
||||
var reportFunc ReportFunType = DefaultReportFunction
|
||||
|
||||
type Element struct {
|
||||
@@ -23,6 +25,7 @@ type Element struct {
|
||||
}
|
||||
|
||||
type RecordType int
|
||||
|
||||
const (
|
||||
MaxOvertimeType = 1
|
||||
OvertimeType = 2
|
||||
@@ -145,14 +148,12 @@ func DefaultReportFunction(name string,callNum int,costTime time.Duration,record
|
||||
return
|
||||
}
|
||||
|
||||
var strReport string
|
||||
strReport = "Profiler report tag "+name+":\n"
|
||||
var average int64
|
||||
if callNum > 0 {
|
||||
average = costTime.Milliseconds() / int64(callNum)
|
||||
}
|
||||
|
||||
strReport += fmt.Sprintf("process count %d,take time %d Milliseconds,average %d Milliseconds/per.\n",callNum,costTime.Milliseconds(),average)
|
||||
log.Info("Profiler report tag "+name, log.Int("process count", callNum), log.Int64("take time", costTime.Milliseconds()), log.Int64("average", average))
|
||||
elem := record.Front()
|
||||
var strTypes string
|
||||
for elem != nil {
|
||||
@@ -163,11 +164,9 @@ func DefaultReportFunction(name string,callNum int,costTime time.Duration,record
|
||||
strTypes = "slow process"
|
||||
}
|
||||
|
||||
strReport += fmt.Sprintf("%s:%s is take %d Milliseconds\n",strTypes,pRecord.RecordName,pRecord.CostTime.Milliseconds())
|
||||
log.Info("Profiler report type", log.String("Types", strTypes), log.String("RecordName", pRecord.RecordName), log.Int64("take time", pRecord.CostTime.Milliseconds()))
|
||||
elem = elem.Next()
|
||||
}
|
||||
|
||||
log.SInfo("report",strReport)
|
||||
}
|
||||
|
||||
func Report() {
|
||||
@@ -200,4 +199,3 @@ func Report() {
|
||||
DefaultReportFunction(name, callNum, totalCostTime, record)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -3,11 +3,11 @@ package rpc
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/duanhf2012/origin/v2/log"
|
||||
"strconv"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
"github.com/duanhf2012/origin/v2/log"
|
||||
)
|
||||
|
||||
type CallSet struct {
|
||||
@@ -20,8 +20,6 @@ type CallSet struct {
|
||||
callTimerHeap CallTimerHeap
|
||||
}
|
||||
|
||||
|
||||
|
||||
func (cs *CallSet) Init() {
|
||||
cs.pendingLock.Lock()
|
||||
cs.callTimerHeap.Init()
|
||||
@@ -34,7 +32,7 @@ func (cs *CallSet) Init(){
|
||||
cs.pendingLock.Unlock()
|
||||
}
|
||||
|
||||
func (bc *CallSet) makeCallFail(call *Call) {
|
||||
func (cs *CallSet) makeCallFail(call *Call) {
|
||||
if call.callback != nil && call.callback.IsValid() {
|
||||
call.rpcHandler.PushRpcResponse(call)
|
||||
} else {
|
||||
@@ -42,105 +40,105 @@ func (bc *CallSet) makeCallFail(call *Call) {
|
||||
}
|
||||
}
|
||||
|
||||
func (bc *CallSet) checkRpcCallTimeout() {
|
||||
func (cs *CallSet) checkRpcCallTimeout() {
|
||||
for {
|
||||
time.Sleep(DefaultCheckRpcCallTimeoutInterval)
|
||||
for i := 0; i < bc.maxCheckCallRpcCount; i++ {
|
||||
bc.pendingLock.Lock()
|
||||
for i := 0; i < cs.maxCheckCallRpcCount; i++ {
|
||||
cs.pendingLock.Lock()
|
||||
|
||||
callSeq := bc.callTimerHeap.PopTimeout()
|
||||
callSeq := cs.callTimerHeap.PopTimeout()
|
||||
if callSeq == 0 {
|
||||
bc.pendingLock.Unlock()
|
||||
cs.pendingLock.Unlock()
|
||||
break
|
||||
}
|
||||
|
||||
pCall := bc.pending[callSeq]
|
||||
pCall := cs.pending[callSeq]
|
||||
if pCall == nil {
|
||||
bc.pendingLock.Unlock()
|
||||
cs.pendingLock.Unlock()
|
||||
log.Error("call seq is not find", log.Uint64("seq", callSeq))
|
||||
continue
|
||||
}
|
||||
|
||||
delete(bc.pending,callSeq)
|
||||
delete(cs.pending, callSeq)
|
||||
strTimeout := strconv.FormatInt(int64(pCall.TimeOut.Seconds()), 10)
|
||||
pCall.Err = errors.New("RPC call takes more than " + strTimeout + " seconds,method is " + pCall.ServiceMethod)
|
||||
log.Error("call timeout", log.String("error", pCall.Err.Error()))
|
||||
bc.makeCallFail(pCall)
|
||||
bc.pendingLock.Unlock()
|
||||
cs.makeCallFail(pCall)
|
||||
cs.pendingLock.Unlock()
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (bc *CallSet) AddPending(call *Call) {
|
||||
bc.pendingLock.Lock()
|
||||
func (cs *CallSet) AddPending(call *Call) {
|
||||
cs.pendingLock.Lock()
|
||||
|
||||
if call.Seq == 0 {
|
||||
bc.pendingLock.Unlock()
|
||||
cs.pendingLock.Unlock()
|
||||
log.Stack("call is error.")
|
||||
return
|
||||
}
|
||||
|
||||
bc.pending[call.Seq] = call
|
||||
bc.callTimerHeap.AddTimer(call.Seq,call.TimeOut)
|
||||
cs.pending[call.Seq] = call
|
||||
cs.callTimerHeap.AddTimer(call.Seq, call.TimeOut)
|
||||
|
||||
bc.pendingLock.Unlock()
|
||||
cs.pendingLock.Unlock()
|
||||
}
|
||||
|
||||
func (bc *CallSet) RemovePending(seq uint64) *Call {
|
||||
func (cs *CallSet) RemovePending(seq uint64) *Call {
|
||||
if seq == 0 {
|
||||
return nil
|
||||
}
|
||||
bc.pendingLock.Lock()
|
||||
call := bc.removePending(seq)
|
||||
bc.pendingLock.Unlock()
|
||||
cs.pendingLock.Lock()
|
||||
call := cs.removePending(seq)
|
||||
cs.pendingLock.Unlock()
|
||||
return call
|
||||
}
|
||||
|
||||
func (bc *CallSet) removePending(seq uint64) *Call {
|
||||
v, ok := bc.pending[seq]
|
||||
func (cs *CallSet) removePending(seq uint64) *Call {
|
||||
v, ok := cs.pending[seq]
|
||||
if ok == false {
|
||||
return nil
|
||||
}
|
||||
|
||||
bc.callTimerHeap.Cancel(seq)
|
||||
delete(bc.pending, seq)
|
||||
cs.callTimerHeap.Cancel(seq)
|
||||
delete(cs.pending, seq)
|
||||
return v
|
||||
}
|
||||
|
||||
func (bc *CallSet) FindPending(seq uint64) (pCall *Call) {
|
||||
func (cs *CallSet) FindPending(seq uint64) (pCall *Call) {
|
||||
if seq == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
bc.pendingLock.Lock()
|
||||
pCall = bc.pending[seq]
|
||||
bc.pendingLock.Unlock()
|
||||
cs.pendingLock.Lock()
|
||||
pCall = cs.pending[seq]
|
||||
cs.pendingLock.Unlock()
|
||||
|
||||
return pCall
|
||||
}
|
||||
|
||||
func (bc *CallSet) cleanPending(){
|
||||
bc.pendingLock.Lock()
|
||||
func (cs *CallSet) cleanPending() {
|
||||
cs.pendingLock.Lock()
|
||||
for {
|
||||
callSeq := bc.callTimerHeap.PopFirst()
|
||||
callSeq := cs.callTimerHeap.PopFirst()
|
||||
if callSeq == 0 {
|
||||
break
|
||||
}
|
||||
pCall := bc.pending[callSeq]
|
||||
pCall := cs.pending[callSeq]
|
||||
if pCall == nil {
|
||||
log.Error("call Seq is not find", log.Uint64("seq", callSeq))
|
||||
continue
|
||||
}
|
||||
|
||||
delete(bc.pending,callSeq)
|
||||
pCall.Err = errors.New("nodeid is disconnect ")
|
||||
bc.makeCallFail(pCall)
|
||||
delete(cs.pending, callSeq)
|
||||
pCall.Err = errors.New("node is disconnect ")
|
||||
cs.makeCallFail(pCall)
|
||||
}
|
||||
|
||||
bc.pendingLock.Unlock()
|
||||
cs.pendingLock.Unlock()
|
||||
}
|
||||
|
||||
func (bc *CallSet) generateSeq() uint64 {
|
||||
return atomic.AddUint64(&bc.startSeq, 1)
|
||||
func (cs *CallSet) generateSeq() uint64 {
|
||||
return atomic.AddUint64(&cs.startSeq, 1)
|
||||
}
|
||||
|
||||
@@ -2,11 +2,11 @@ package rpc
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/duanhf2012/origin/v2/log"
|
||||
"github.com/duanhf2012/origin/v2/network"
|
||||
"reflect"
|
||||
"time"
|
||||
"github.com/duanhf2012/origin/v2/log"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -29,7 +29,7 @@ type IWriter interface {
|
||||
}
|
||||
|
||||
type IRealClient interface {
|
||||
SetConn(conn *network.TCPConn)
|
||||
SetConn(conn *network.NetConn)
|
||||
Close(waitDone bool)
|
||||
|
||||
AsyncCall(NodeId string, timeout time.Duration, rpcHandler IRpcHandler, serviceMethod string, callback reflect.Value, args interface{}, replyParam interface{}, cancelable bool) (CancelRpc, error)
|
||||
@@ -43,8 +43,6 @@ type IRealClient interface {
|
||||
Bind(server IServer)
|
||||
}
|
||||
|
||||
|
||||
|
||||
type Client struct {
|
||||
clientId uint32
|
||||
targetNodeId string
|
||||
@@ -54,7 +52,7 @@ type Client struct {
|
||||
IRealClient
|
||||
}
|
||||
|
||||
func (client *Client) NewClientAgent(conn *network.TCPConn) network.Agent {
|
||||
func (client *Client) NewClientAgent(conn *network.NetConn) network.Agent {
|
||||
client.SetConn(conn)
|
||||
|
||||
return client
|
||||
@@ -68,7 +66,6 @@ func (client *Client) GetClientId() uint32 {
|
||||
return client.clientId
|
||||
}
|
||||
|
||||
|
||||
func (client *Client) processRpcResponse(responseData []byte) error {
|
||||
bCompress := (responseData[0] >> 7) > 0
|
||||
processor := GetProcessor(responseData[0] & 0x7f)
|
||||
@@ -140,7 +137,6 @@ func (client *Client) processRpcResponse(responseData []byte) error{
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
//func (rc *Client) Go(timeout time.Duration,rpcHandler IRpcHandler,noReply bool, serviceMethod string, args interface{}, reply interface{}) *Call {
|
||||
// _, processor := GetProcessorType(args)
|
||||
// InParam, err := processor.Marshal(args)
|
||||
@@ -154,11 +150,11 @@ func (client *Client) processRpcResponse(responseData []byte) error{
|
||||
// return rc.RawGo(timeout,rpcHandler,processor, noReply, 0, serviceMethod, InParam, reply)
|
||||
//}
|
||||
|
||||
func (rc *Client) rawGo(nodeId string,w IWriter,timeout time.Duration,rpcHandler IRpcHandler,processor IRpcProcessor, noReply bool, rpcMethodId uint32, serviceMethod string, rawArgs []byte, reply interface{}) *Call {
|
||||
func (client *Client) rawGo(nodeId string, w IWriter, timeout time.Duration, rpcHandler IRpcHandler, processor IRpcProcessor, noReply bool, rpcMethodId uint32, serviceMethod string, rawArgs []byte, reply interface{}) *Call {
|
||||
call := MakeCall()
|
||||
call.ServiceMethod = serviceMethod
|
||||
call.Reply = reply
|
||||
call.Seq = rc.generateSeq()
|
||||
call.Seq = client.generateSeq()
|
||||
call.TimeOut = timeout
|
||||
|
||||
request := MakeRpcRequest(processor, call.Seq, rpcMethodId, serviceMethod, noReply, rawArgs)
|
||||
@@ -182,7 +178,7 @@ func (rc *Client) rawGo(nodeId string,w IWriter,timeout time.Duration,rpcHandler
|
||||
|
||||
var compressBuff []byte
|
||||
bCompress := uint8(0)
|
||||
if rc.compressBytesLen > 0 && len(bytes) >= rc.compressBytesLen {
|
||||
if client.compressBytesLen > 0 && len(bytes) >= client.compressBytesLen {
|
||||
var cErr error
|
||||
compressBuff, cErr = compressor.CompressBlock(bytes)
|
||||
if cErr != nil {
|
||||
@@ -198,7 +194,7 @@ func (rc *Client) rawGo(nodeId string,w IWriter,timeout time.Duration,rpcHandler
|
||||
}
|
||||
|
||||
if noReply == false {
|
||||
rc.AddPending(call)
|
||||
client.AddPending(call)
|
||||
}
|
||||
|
||||
err = w.WriteMsg(nodeId, []byte{uint8(processor.GetProcessorType()) | bCompress}, bytes)
|
||||
@@ -206,8 +202,8 @@ func (rc *Client) rawGo(nodeId string,w IWriter,timeout time.Duration,rpcHandler
|
||||
compressor.CompressBufferCollection(compressBuff)
|
||||
}
|
||||
if err != nil {
|
||||
rc.RemovePending(call.Seq)
|
||||
log.Error("WiteMsg is fail",log.ErrorAttr("error",err))
|
||||
client.RemovePending(call.Seq)
|
||||
log.Error("WriteMsg is fail", log.ErrorAttr("error", err))
|
||||
call.Seq = 0
|
||||
call.DoError(err)
|
||||
}
|
||||
@@ -215,14 +211,14 @@ func (rc *Client) rawGo(nodeId string,w IWriter,timeout time.Duration,rpcHandler
|
||||
return call
|
||||
}
|
||||
|
||||
func (rc *Client) asyncCall(nodeId string,w IWriter,timeout time.Duration,rpcHandler IRpcHandler, serviceMethod string, callback reflect.Value, args interface{}, replyParam interface{},cancelable bool) (CancelRpc,error) {
|
||||
func (client *Client) asyncCall(nodeId string, w IWriter, timeout time.Duration, rpcHandler IRpcHandler, serviceMethod string, callback reflect.Value, args interface{}, replyParam interface{}, cancelable bool) (CancelRpc, error) {
|
||||
processorType, processor := GetProcessorType(args)
|
||||
InParam, herr := processor.Marshal(args)
|
||||
if herr != nil {
|
||||
return emptyCancelRpc, herr
|
||||
}
|
||||
|
||||
seq := rc.generateSeq()
|
||||
seq := client.generateSeq()
|
||||
request := MakeRpcRequest(processor, seq, 0, serviceMethod, false, InParam)
|
||||
bytes, err := processor.Marshal(request.RpcRequestData)
|
||||
ReleaseRpcRequest(request)
|
||||
@@ -236,7 +232,7 @@ func (rc *Client) asyncCall(nodeId string,w IWriter,timeout time.Duration,rpcHan
|
||||
|
||||
var compressBuff []byte
|
||||
bCompress := uint8(0)
|
||||
if rc.compressBytesLen>0 &&len(bytes) >= rc.compressBytesLen {
|
||||
if client.compressBytesLen > 0 && len(bytes) >= client.compressBytesLen {
|
||||
var cErr error
|
||||
compressBuff, cErr = compressor.CompressBlock(bytes)
|
||||
if cErr != nil {
|
||||
@@ -256,20 +252,20 @@ func (rc *Client) asyncCall(nodeId string,w IWriter,timeout time.Duration,rpcHan
|
||||
call.ServiceMethod = serviceMethod
|
||||
call.Seq = seq
|
||||
call.TimeOut = timeout
|
||||
rc.AddPending(call)
|
||||
client.AddPending(call)
|
||||
|
||||
err = w.WriteMsg(nodeId, []byte{uint8(processorType) | bCompress}, bytes)
|
||||
if cap(compressBuff) > 0 {
|
||||
compressor.CompressBufferCollection(compressBuff)
|
||||
}
|
||||
if err != nil {
|
||||
rc.RemovePending(call.Seq)
|
||||
client.RemovePending(call.Seq)
|
||||
ReleaseCall(call)
|
||||
return emptyCancelRpc, err
|
||||
}
|
||||
|
||||
if cancelable {
|
||||
rpcCancel := RpcCancel{CallSeq:seq,Cli: rc}
|
||||
rpcCancel := RpcCancel{CallSeq: seq, Cli: client}
|
||||
return rpcCancel.CancelRpc, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@ import (
|
||||
"runtime"
|
||||
)
|
||||
|
||||
var memPool bytespool.IBytesMempool = bytespool.NewMemAreaPool()
|
||||
var memPool bytespool.IBytesMemPool = bytespool.NewMemAreaPool()
|
||||
|
||||
type ICompressor interface {
|
||||
CompressBlock(src []byte) ([]byte, error) //dst如果有预申请使用dst内存,传入nil时内部申请
|
||||
@@ -19,6 +19,7 @@ type ICompressor interface {
|
||||
}
|
||||
|
||||
var compressor ICompressor
|
||||
|
||||
func init() {
|
||||
SetCompressor(&Lz4Compressor{})
|
||||
}
|
||||
@@ -56,7 +57,7 @@ func (lc *Lz4Compressor) CompressBlock(src []byte) (dest []byte, err error) {
|
||||
|
||||
if ratio > 255 {
|
||||
memPool.ReleaseBytes(dest)
|
||||
return nil,fmt.Errorf("Impermissible errors")
|
||||
return nil, fmt.Errorf("impermissible errors")
|
||||
}
|
||||
|
||||
dest[0] = uint8(ratio)
|
||||
@@ -76,7 +77,7 @@ func (lc *Lz4Compressor) UncompressBlock(src []byte) (dest []byte, err error) {
|
||||
|
||||
radio := uint8(src[0])
|
||||
if radio == 0 {
|
||||
return nil,fmt.Errorf("Impermissible errors")
|
||||
return nil, fmt.Errorf("impermissible errors")
|
||||
}
|
||||
|
||||
dest = memPool.MakeBytes(len(src) * int(radio))
|
||||
|
||||
@@ -10,15 +10,15 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
//本结点的Client
|
||||
// LClient 本结点的Client
|
||||
type LClient struct {
|
||||
selfClient *Client
|
||||
}
|
||||
|
||||
func (rc *LClient) Lock(){
|
||||
func (lc *LClient) Lock() {
|
||||
}
|
||||
|
||||
func (rc *LClient) Unlock(){
|
||||
func (lc *LClient) Unlock() {
|
||||
}
|
||||
|
||||
func (lc *LClient) Run() {
|
||||
@@ -31,7 +31,7 @@ func (lc *LClient) IsConnected() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (lc *LClient) SetConn(conn *network.TCPConn){
|
||||
func (lc *LClient) SetConn(conn *network.NetConn) {
|
||||
}
|
||||
|
||||
func (lc *LClient) Close(waitDone bool) {
|
||||
@@ -69,8 +69,7 @@ func (lc *LClient) Go(nodeId string,timeout time.Duration,rpcHandler IRpcHandler
|
||||
return pLocalRpcServer.selfNodeRpcHandlerGo(timeout, nil, lc.selfClient, noReply, serviceName, 0, serviceMethod, args, reply, nil)
|
||||
}
|
||||
|
||||
|
||||
func (rc *LClient) RawGo(nodeId string,timeout time.Duration,rpcHandler IRpcHandler,processor IRpcProcessor, noReply bool, rpcMethodId uint32, serviceName string, rawArgs []byte, reply interface{}) *Call {
|
||||
func (lc *LClient) RawGo(nodeId string, timeout time.Duration, rpcHandler IRpcHandler, processor IRpcProcessor, noReply bool, rpcMethodId uint32, serviceName string, rawArgs []byte, reply interface{}) *Call {
|
||||
pLocalRpcServer := rpcHandler.GetRpcServer()()
|
||||
|
||||
//服务自我调用
|
||||
@@ -80,7 +79,7 @@ func (rc *LClient) RawGo(nodeId string,timeout time.Duration,rpcHandler IRpcHand
|
||||
call.Reply = reply
|
||||
call.TimeOut = timeout
|
||||
|
||||
err := pLocalRpcServer.myselfRpcHandlerGo(rc.selfClient,serviceName, serviceName, rawArgs, requestHandlerNull,nil)
|
||||
err := pLocalRpcServer.myselfRpcHandlerGo(lc.selfClient, serviceName, serviceName, rawArgs, requestHandlerNull, nil)
|
||||
call.Err = err
|
||||
call.done <- call
|
||||
|
||||
@@ -88,10 +87,9 @@ func (rc *LClient) RawGo(nodeId string,timeout time.Duration,rpcHandler IRpcHand
|
||||
}
|
||||
|
||||
//其他的rpcHandler的处理器
|
||||
return pLocalRpcServer.selfNodeRpcHandlerGo(timeout,processor,rc.selfClient, true, serviceName, rpcMethodId, serviceName, nil, nil, rawArgs)
|
||||
return pLocalRpcServer.selfNodeRpcHandlerGo(timeout, processor, lc.selfClient, true, serviceName, rpcMethodId, serviceName, nil, nil, rawArgs)
|
||||
}
|
||||
|
||||
|
||||
func (lc *LClient) AsyncCall(nodeId string, timeout time.Duration, rpcHandler IRpcHandler, serviceMethod string, callback reflect.Value, args interface{}, reply interface{}, cancelable bool) (CancelRpc, error) {
|
||||
pLocalRpcServer := rpcHandler.GetRpcServer()()
|
||||
|
||||
@@ -111,12 +109,12 @@ func (lc *LClient) AsyncCall(nodeId string,timeout time.Duration,rpcHandler IRpc
|
||||
}
|
||||
|
||||
//其他的rpcHandler的处理器
|
||||
calcelRpc,err := pLocalRpcServer.selfNodeRpcHandlerAsyncGo(timeout,lc.selfClient, rpcHandler, false, serviceName, serviceMethod, args, reply, callback,cancelable)
|
||||
cancelRpc, err := pLocalRpcServer.selfNodeRpcHandlerAsyncGo(timeout, lc.selfClient, rpcHandler, false, serviceName, serviceMethod, args, reply, callback, cancelable)
|
||||
if err != nil {
|
||||
callback.Call([]reflect.Value{reflect.ValueOf(reply), reflect.ValueOf(err)})
|
||||
}
|
||||
|
||||
return calcelRpc,nil
|
||||
return cancelRpc, nil
|
||||
}
|
||||
|
||||
func NewLClient(localNodeId string, callSet *CallSet) *Client {
|
||||
@@ -133,5 +131,5 @@ func NewLClient(localNodeId string,callSet *CallSet) *Client{
|
||||
return client
|
||||
}
|
||||
|
||||
func (rc *LClient) Bind(server IServer){
|
||||
func (lc *LClient) Bind(_ IServer) {
|
||||
}
|
||||
@@ -1,14 +1,14 @@
|
||||
package rpc
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/duanhf2012/origin/v2/log"
|
||||
"reflect"
|
||||
"time"
|
||||
"strings"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
|
||||
type BaseServer struct {
|
||||
localNodeId string
|
||||
compressBytesLen int
|
||||
@@ -17,13 +17,13 @@ type BaseServer struct {
|
||||
iServer IServer
|
||||
}
|
||||
|
||||
func (ls *BaseServer) initBaseServer(compressBytesLen int,rpcHandleFinder RpcHandleFinder){
|
||||
ls.compressBytesLen = compressBytesLen
|
||||
ls.rpcHandleFinder = rpcHandleFinder
|
||||
func (server *BaseServer) initBaseServer(compressBytesLen int, rpcHandleFinder RpcHandleFinder) {
|
||||
server.compressBytesLen = compressBytesLen
|
||||
server.rpcHandleFinder = rpcHandleFinder
|
||||
}
|
||||
|
||||
func (ls *BaseServer) myselfRpcHandlerGo(client *Client,handlerName string, serviceMethod string, args interface{},callBack reflect.Value, reply interface{}) error {
|
||||
rpcHandler := ls.rpcHandleFinder.FindRpcHandler(handlerName)
|
||||
func (server *BaseServer) myselfRpcHandlerGo(client *Client, handlerName string, serviceMethod string, args interface{}, callBack reflect.Value, reply interface{}) error {
|
||||
rpcHandler := server.rpcHandleFinder.FindRpcHandler(handlerName)
|
||||
if rpcHandler == nil {
|
||||
err := errors.New("service method " + serviceMethod + " not config!")
|
||||
log.Error("service method not config", log.String("serviceMethod", serviceMethod))
|
||||
@@ -33,13 +33,13 @@ func (ls *BaseServer) myselfRpcHandlerGo(client *Client,handlerName string, serv
|
||||
return rpcHandler.CallMethod(client, serviceMethod, args, callBack, reply)
|
||||
}
|
||||
|
||||
func (ls *BaseServer) selfNodeRpcHandlerGo(timeout time.Duration,processor IRpcProcessor, client *Client, noReply bool, handlerName string, rpcMethodId uint32, serviceMethod string, args interface{}, reply interface{}, rawArgs []byte) *Call {
|
||||
func (server *BaseServer) selfNodeRpcHandlerGo(timeout time.Duration, processor IRpcProcessor, client *Client, noReply bool, handlerName string, rpcMethodId uint32, serviceMethod string, args interface{}, reply interface{}, rawArgs []byte) *Call {
|
||||
pCall := MakeCall()
|
||||
pCall.Seq = client.generateSeq()
|
||||
pCall.TimeOut = timeout
|
||||
pCall.ServiceMethod = serviceMethod
|
||||
|
||||
rpcHandler := ls.rpcHandleFinder.FindRpcHandler(handlerName)
|
||||
rpcHandler := server.rpcHandleFinder.FindRpcHandler(handlerName)
|
||||
if rpcHandler == nil {
|
||||
err := errors.New("service method " + serviceMethod + " not config!")
|
||||
log.Error("service method not config", log.String("serviceMethod", serviceMethod), log.ErrorAttr("error", err))
|
||||
@@ -194,7 +194,7 @@ func (server *BaseServer) selfNodeRpcHandlerAsyncGo(timeout time.Duration,client
|
||||
return cancelRpc, nil
|
||||
}
|
||||
|
||||
func (bs *BaseServer) processRpcRequest(data []byte,connTag string,wrResponse writeResponse) error{
|
||||
func (server *BaseServer) processRpcRequest(data []byte, connTag string, wrResponse writeResponse) error {
|
||||
bCompress := (data[0] >> 7) > 0
|
||||
processor := GetProcessor(data[0] & 0x7f)
|
||||
if processor == nil {
|
||||
@@ -209,7 +209,7 @@ func (bs *BaseServer) processRpcRequest(data []byte,connTag string,wrResponse wr
|
||||
|
||||
compressBuff, unCompressErr = compressor.UncompressBlock(byteData)
|
||||
if unCompressErr != nil {
|
||||
return errors.New("UncompressBlock failed")
|
||||
return errors.New("uncompressBlock failed")
|
||||
}
|
||||
|
||||
byteData = compressBuff
|
||||
@@ -245,7 +245,7 @@ func (bs *BaseServer) processRpcRequest(data []byte,connTag string,wrResponse wr
|
||||
return nil
|
||||
}
|
||||
|
||||
rpcHandler := bs.rpcHandleFinder.FindRpcHandler(serviceMethod[0])
|
||||
rpcHandler := server.rpcHandleFinder.FindRpcHandler(serviceMethod[0])
|
||||
if rpcHandler == nil {
|
||||
rpcError := RpcError(fmt.Sprintf("service method %s not config!", req.RpcRequestData.GetServiceMethod()))
|
||||
if req.RpcRequestData.IsNoReply() == false {
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
// Code generated by protoc-gen-go. DO NOT EDIT.
|
||||
// versions:
|
||||
// protoc-gen-go v1.31.0
|
||||
// protoc v3.11.4
|
||||
// source: test/rpc/messagequeue.proto
|
||||
// protoc v4.24.0
|
||||
// source: rpcproto/messagequeue.proto
|
||||
|
||||
package rpc
|
||||
|
||||
@@ -50,11 +50,11 @@ func (x SubscribeType) String() string {
|
||||
}
|
||||
|
||||
func (SubscribeType) Descriptor() protoreflect.EnumDescriptor {
|
||||
return file_test_rpc_messagequeue_proto_enumTypes[0].Descriptor()
|
||||
return file_rpcproto_messagequeue_proto_enumTypes[0].Descriptor()
|
||||
}
|
||||
|
||||
func (SubscribeType) Type() protoreflect.EnumType {
|
||||
return &file_test_rpc_messagequeue_proto_enumTypes[0]
|
||||
return &file_rpcproto_messagequeue_proto_enumTypes[0]
|
||||
}
|
||||
|
||||
func (x SubscribeType) Number() protoreflect.EnumNumber {
|
||||
@@ -63,7 +63,7 @@ func (x SubscribeType) Number() protoreflect.EnumNumber {
|
||||
|
||||
// Deprecated: Use SubscribeType.Descriptor instead.
|
||||
func (SubscribeType) EnumDescriptor() ([]byte, []int) {
|
||||
return file_test_rpc_messagequeue_proto_rawDescGZIP(), []int{0}
|
||||
return file_rpcproto_messagequeue_proto_rawDescGZIP(), []int{0}
|
||||
}
|
||||
|
||||
type SubscribeMethod int32
|
||||
@@ -96,11 +96,11 @@ func (x SubscribeMethod) String() string {
|
||||
}
|
||||
|
||||
func (SubscribeMethod) Descriptor() protoreflect.EnumDescriptor {
|
||||
return file_test_rpc_messagequeue_proto_enumTypes[1].Descriptor()
|
||||
return file_rpcproto_messagequeue_proto_enumTypes[1].Descriptor()
|
||||
}
|
||||
|
||||
func (SubscribeMethod) Type() protoreflect.EnumType {
|
||||
return &file_test_rpc_messagequeue_proto_enumTypes[1]
|
||||
return &file_rpcproto_messagequeue_proto_enumTypes[1]
|
||||
}
|
||||
|
||||
func (x SubscribeMethod) Number() protoreflect.EnumNumber {
|
||||
@@ -109,7 +109,7 @@ func (x SubscribeMethod) Number() protoreflect.EnumNumber {
|
||||
|
||||
// Deprecated: Use SubscribeMethod.Descriptor instead.
|
||||
func (SubscribeMethod) EnumDescriptor() ([]byte, []int) {
|
||||
return file_test_rpc_messagequeue_proto_rawDescGZIP(), []int{1}
|
||||
return file_rpcproto_messagequeue_proto_rawDescGZIP(), []int{1}
|
||||
}
|
||||
|
||||
type DBQueuePopReq struct {
|
||||
@@ -127,7 +127,7 @@ type DBQueuePopReq struct {
|
||||
func (x *DBQueuePopReq) Reset() {
|
||||
*x = DBQueuePopReq{}
|
||||
if protoimpl.UnsafeEnabled {
|
||||
mi := &file_test_rpc_messagequeue_proto_msgTypes[0]
|
||||
mi := &file_rpcproto_messagequeue_proto_msgTypes[0]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
@@ -140,7 +140,7 @@ func (x *DBQueuePopReq) String() string {
|
||||
func (*DBQueuePopReq) ProtoMessage() {}
|
||||
|
||||
func (x *DBQueuePopReq) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_test_rpc_messagequeue_proto_msgTypes[0]
|
||||
mi := &file_rpcproto_messagequeue_proto_msgTypes[0]
|
||||
if protoimpl.UnsafeEnabled && x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
@@ -153,7 +153,7 @@ func (x *DBQueuePopReq) ProtoReflect() protoreflect.Message {
|
||||
|
||||
// Deprecated: Use DBQueuePopReq.ProtoReflect.Descriptor instead.
|
||||
func (*DBQueuePopReq) Descriptor() ([]byte, []int) {
|
||||
return file_test_rpc_messagequeue_proto_rawDescGZIP(), []int{0}
|
||||
return file_rpcproto_messagequeue_proto_rawDescGZIP(), []int{0}
|
||||
}
|
||||
|
||||
func (x *DBQueuePopReq) GetCustomerId() string {
|
||||
@@ -203,7 +203,7 @@ type DBQueuePopRes struct {
|
||||
func (x *DBQueuePopRes) Reset() {
|
||||
*x = DBQueuePopRes{}
|
||||
if protoimpl.UnsafeEnabled {
|
||||
mi := &file_test_rpc_messagequeue_proto_msgTypes[1]
|
||||
mi := &file_rpcproto_messagequeue_proto_msgTypes[1]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
@@ -216,7 +216,7 @@ func (x *DBQueuePopRes) String() string {
|
||||
func (*DBQueuePopRes) ProtoMessage() {}
|
||||
|
||||
func (x *DBQueuePopRes) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_test_rpc_messagequeue_proto_msgTypes[1]
|
||||
mi := &file_rpcproto_messagequeue_proto_msgTypes[1]
|
||||
if protoimpl.UnsafeEnabled && x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
@@ -229,7 +229,7 @@ func (x *DBQueuePopRes) ProtoReflect() protoreflect.Message {
|
||||
|
||||
// Deprecated: Use DBQueuePopRes.ProtoReflect.Descriptor instead.
|
||||
func (*DBQueuePopRes) Descriptor() ([]byte, []int) {
|
||||
return file_test_rpc_messagequeue_proto_rawDescGZIP(), []int{1}
|
||||
return file_rpcproto_messagequeue_proto_rawDescGZIP(), []int{1}
|
||||
}
|
||||
|
||||
func (x *DBQueuePopRes) GetQueueName() string {
|
||||
@@ -255,7 +255,7 @@ type DBQueueSubscribeReq struct {
|
||||
SubType SubscribeType `protobuf:"varint,1,opt,name=SubType,proto3,enum=SubscribeType" json:"SubType,omitempty"` //订阅类型
|
||||
Method SubscribeMethod `protobuf:"varint,2,opt,name=Method,proto3,enum=SubscribeMethod" json:"Method,omitempty"` //订阅方法
|
||||
CustomerId string `protobuf:"bytes,3,opt,name=CustomerId,proto3" json:"CustomerId,omitempty"` //消费者Id
|
||||
FromNodeId int32 `protobuf:"varint,4,opt,name=FromNodeId,proto3" json:"FromNodeId,omitempty"`
|
||||
FromNodeId string `protobuf:"bytes,4,opt,name=FromNodeId,proto3" json:"FromNodeId,omitempty"`
|
||||
RpcMethod string `protobuf:"bytes,5,opt,name=RpcMethod,proto3" json:"RpcMethod,omitempty"`
|
||||
TopicName string `protobuf:"bytes,6,opt,name=TopicName,proto3" json:"TopicName,omitempty"` //主题名称
|
||||
StartIndex uint64 `protobuf:"varint,7,opt,name=StartIndex,proto3" json:"StartIndex,omitempty"` //开始位置 ,格式前4位是时间戳秒,后面是序号。如果填0时,服务自动修改成:(4bit 当前时间秒)| (0000 4bit)
|
||||
@@ -265,7 +265,7 @@ type DBQueueSubscribeReq struct {
|
||||
func (x *DBQueueSubscribeReq) Reset() {
|
||||
*x = DBQueueSubscribeReq{}
|
||||
if protoimpl.UnsafeEnabled {
|
||||
mi := &file_test_rpc_messagequeue_proto_msgTypes[2]
|
||||
mi := &file_rpcproto_messagequeue_proto_msgTypes[2]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
@@ -278,7 +278,7 @@ func (x *DBQueueSubscribeReq) String() string {
|
||||
func (*DBQueueSubscribeReq) ProtoMessage() {}
|
||||
|
||||
func (x *DBQueueSubscribeReq) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_test_rpc_messagequeue_proto_msgTypes[2]
|
||||
mi := &file_rpcproto_messagequeue_proto_msgTypes[2]
|
||||
if protoimpl.UnsafeEnabled && x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
@@ -291,7 +291,7 @@ func (x *DBQueueSubscribeReq) ProtoReflect() protoreflect.Message {
|
||||
|
||||
// Deprecated: Use DBQueueSubscribeReq.ProtoReflect.Descriptor instead.
|
||||
func (*DBQueueSubscribeReq) Descriptor() ([]byte, []int) {
|
||||
return file_test_rpc_messagequeue_proto_rawDescGZIP(), []int{2}
|
||||
return file_rpcproto_messagequeue_proto_rawDescGZIP(), []int{2}
|
||||
}
|
||||
|
||||
func (x *DBQueueSubscribeReq) GetSubType() SubscribeType {
|
||||
@@ -315,11 +315,11 @@ func (x *DBQueueSubscribeReq) GetCustomerId() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *DBQueueSubscribeReq) GetFromNodeId() int32 {
|
||||
func (x *DBQueueSubscribeReq) GetFromNodeId() string {
|
||||
if x != nil {
|
||||
return x.FromNodeId
|
||||
}
|
||||
return 0
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *DBQueueSubscribeReq) GetRpcMethod() string {
|
||||
@@ -359,7 +359,7 @@ type DBQueueSubscribeRes struct {
|
||||
func (x *DBQueueSubscribeRes) Reset() {
|
||||
*x = DBQueueSubscribeRes{}
|
||||
if protoimpl.UnsafeEnabled {
|
||||
mi := &file_test_rpc_messagequeue_proto_msgTypes[3]
|
||||
mi := &file_rpcproto_messagequeue_proto_msgTypes[3]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
@@ -372,7 +372,7 @@ func (x *DBQueueSubscribeRes) String() string {
|
||||
func (*DBQueueSubscribeRes) ProtoMessage() {}
|
||||
|
||||
func (x *DBQueueSubscribeRes) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_test_rpc_messagequeue_proto_msgTypes[3]
|
||||
mi := &file_rpcproto_messagequeue_proto_msgTypes[3]
|
||||
if protoimpl.UnsafeEnabled && x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
@@ -385,7 +385,7 @@ func (x *DBQueueSubscribeRes) ProtoReflect() protoreflect.Message {
|
||||
|
||||
// Deprecated: Use DBQueueSubscribeRes.ProtoReflect.Descriptor instead.
|
||||
func (*DBQueueSubscribeRes) Descriptor() ([]byte, []int) {
|
||||
return file_test_rpc_messagequeue_proto_rawDescGZIP(), []int{3}
|
||||
return file_rpcproto_messagequeue_proto_rawDescGZIP(), []int{3}
|
||||
}
|
||||
|
||||
type DBQueuePublishReq struct {
|
||||
@@ -400,7 +400,7 @@ type DBQueuePublishReq struct {
|
||||
func (x *DBQueuePublishReq) Reset() {
|
||||
*x = DBQueuePublishReq{}
|
||||
if protoimpl.UnsafeEnabled {
|
||||
mi := &file_test_rpc_messagequeue_proto_msgTypes[4]
|
||||
mi := &file_rpcproto_messagequeue_proto_msgTypes[4]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
@@ -413,7 +413,7 @@ func (x *DBQueuePublishReq) String() string {
|
||||
func (*DBQueuePublishReq) ProtoMessage() {}
|
||||
|
||||
func (x *DBQueuePublishReq) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_test_rpc_messagequeue_proto_msgTypes[4]
|
||||
mi := &file_rpcproto_messagequeue_proto_msgTypes[4]
|
||||
if protoimpl.UnsafeEnabled && x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
@@ -426,7 +426,7 @@ func (x *DBQueuePublishReq) ProtoReflect() protoreflect.Message {
|
||||
|
||||
// Deprecated: Use DBQueuePublishReq.ProtoReflect.Descriptor instead.
|
||||
func (*DBQueuePublishReq) Descriptor() ([]byte, []int) {
|
||||
return file_test_rpc_messagequeue_proto_rawDescGZIP(), []int{4}
|
||||
return file_rpcproto_messagequeue_proto_rawDescGZIP(), []int{4}
|
||||
}
|
||||
|
||||
func (x *DBQueuePublishReq) GetTopicName() string {
|
||||
@@ -452,7 +452,7 @@ type DBQueuePublishRes struct {
|
||||
func (x *DBQueuePublishRes) Reset() {
|
||||
*x = DBQueuePublishRes{}
|
||||
if protoimpl.UnsafeEnabled {
|
||||
mi := &file_test_rpc_messagequeue_proto_msgTypes[5]
|
||||
mi := &file_rpcproto_messagequeue_proto_msgTypes[5]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
@@ -465,7 +465,7 @@ func (x *DBQueuePublishRes) String() string {
|
||||
func (*DBQueuePublishRes) ProtoMessage() {}
|
||||
|
||||
func (x *DBQueuePublishRes) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_test_rpc_messagequeue_proto_msgTypes[5]
|
||||
mi := &file_rpcproto_messagequeue_proto_msgTypes[5]
|
||||
if protoimpl.UnsafeEnabled && x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
@@ -478,13 +478,13 @@ func (x *DBQueuePublishRes) ProtoReflect() protoreflect.Message {
|
||||
|
||||
// Deprecated: Use DBQueuePublishRes.ProtoReflect.Descriptor instead.
|
||||
func (*DBQueuePublishRes) Descriptor() ([]byte, []int) {
|
||||
return file_test_rpc_messagequeue_proto_rawDescGZIP(), []int{5}
|
||||
return file_rpcproto_messagequeue_proto_rawDescGZIP(), []int{5}
|
||||
}
|
||||
|
||||
var File_test_rpc_messagequeue_proto protoreflect.FileDescriptor
|
||||
var File_rpcproto_messagequeue_proto protoreflect.FileDescriptor
|
||||
|
||||
var file_test_rpc_messagequeue_proto_rawDesc = []byte{
|
||||
0x0a, 0x1b, 0x74, 0x65, 0x73, 0x74, 0x2f, 0x72, 0x70, 0x63, 0x2f, 0x6d, 0x65, 0x73, 0x73, 0x61,
|
||||
var file_rpcproto_messagequeue_proto_rawDesc = []byte{
|
||||
0x0a, 0x1b, 0x72, 0x70, 0x63, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x6d, 0x65, 0x73, 0x73, 0x61,
|
||||
0x67, 0x65, 0x71, 0x75, 0x65, 0x75, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0xa3, 0x01,
|
||||
0x0a, 0x0d, 0x44, 0x42, 0x51, 0x75, 0x65, 0x75, 0x65, 0x50, 0x6f, 0x70, 0x52, 0x65, 0x71, 0x12,
|
||||
0x1e, 0x0a, 0x0a, 0x43, 0x75, 0x73, 0x74, 0x6f, 0x6d, 0x65, 0x72, 0x49, 0x64, 0x18, 0x01, 0x20,
|
||||
@@ -510,7 +510,7 @@ var file_test_rpc_messagequeue_proto_rawDesc = []byte{
|
||||
0x6f, 0x64, 0x52, 0x06, 0x4d, 0x65, 0x74, 0x68, 0x6f, 0x64, 0x12, 0x1e, 0x0a, 0x0a, 0x43, 0x75,
|
||||
0x73, 0x74, 0x6f, 0x6d, 0x65, 0x72, 0x49, 0x64, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0a,
|
||||
0x43, 0x75, 0x73, 0x74, 0x6f, 0x6d, 0x65, 0x72, 0x49, 0x64, 0x12, 0x1e, 0x0a, 0x0a, 0x46, 0x72,
|
||||
0x6f, 0x6d, 0x4e, 0x6f, 0x64, 0x65, 0x49, 0x64, 0x18, 0x04, 0x20, 0x01, 0x28, 0x05, 0x52, 0x0a,
|
||||
0x6f, 0x6d, 0x4e, 0x6f, 0x64, 0x65, 0x49, 0x64, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0a,
|
||||
0x46, 0x72, 0x6f, 0x6d, 0x4e, 0x6f, 0x64, 0x65, 0x49, 0x64, 0x12, 0x1c, 0x0a, 0x09, 0x52, 0x70,
|
||||
0x63, 0x4d, 0x65, 0x74, 0x68, 0x6f, 0x64, 0x18, 0x05, 0x20, 0x01, 0x28, 0x09, 0x52, 0x09, 0x52,
|
||||
0x70, 0x63, 0x4d, 0x65, 0x74, 0x68, 0x6f, 0x64, 0x12, 0x1c, 0x0a, 0x09, 0x54, 0x6f, 0x70, 0x69,
|
||||
@@ -539,20 +539,20 @@ var file_test_rpc_messagequeue_proto_rawDesc = []byte{
|
||||
}
|
||||
|
||||
var (
|
||||
file_test_rpc_messagequeue_proto_rawDescOnce sync.Once
|
||||
file_test_rpc_messagequeue_proto_rawDescData = file_test_rpc_messagequeue_proto_rawDesc
|
||||
file_rpcproto_messagequeue_proto_rawDescOnce sync.Once
|
||||
file_rpcproto_messagequeue_proto_rawDescData = file_rpcproto_messagequeue_proto_rawDesc
|
||||
)
|
||||
|
||||
func file_test_rpc_messagequeue_proto_rawDescGZIP() []byte {
|
||||
file_test_rpc_messagequeue_proto_rawDescOnce.Do(func() {
|
||||
file_test_rpc_messagequeue_proto_rawDescData = protoimpl.X.CompressGZIP(file_test_rpc_messagequeue_proto_rawDescData)
|
||||
func file_rpcproto_messagequeue_proto_rawDescGZIP() []byte {
|
||||
file_rpcproto_messagequeue_proto_rawDescOnce.Do(func() {
|
||||
file_rpcproto_messagequeue_proto_rawDescData = protoimpl.X.CompressGZIP(file_rpcproto_messagequeue_proto_rawDescData)
|
||||
})
|
||||
return file_test_rpc_messagequeue_proto_rawDescData
|
||||
return file_rpcproto_messagequeue_proto_rawDescData
|
||||
}
|
||||
|
||||
var file_test_rpc_messagequeue_proto_enumTypes = make([]protoimpl.EnumInfo, 2)
|
||||
var file_test_rpc_messagequeue_proto_msgTypes = make([]protoimpl.MessageInfo, 6)
|
||||
var file_test_rpc_messagequeue_proto_goTypes = []interface{}{
|
||||
var file_rpcproto_messagequeue_proto_enumTypes = make([]protoimpl.EnumInfo, 2)
|
||||
var file_rpcproto_messagequeue_proto_msgTypes = make([]protoimpl.MessageInfo, 6)
|
||||
var file_rpcproto_messagequeue_proto_goTypes = []interface{}{
|
||||
(SubscribeType)(0), // 0: SubscribeType
|
||||
(SubscribeMethod)(0), // 1: SubscribeMethod
|
||||
(*DBQueuePopReq)(nil), // 2: DBQueuePopReq
|
||||
@@ -562,7 +562,7 @@ var file_test_rpc_messagequeue_proto_goTypes = []interface{}{
|
||||
(*DBQueuePublishReq)(nil), // 6: DBQueuePublishReq
|
||||
(*DBQueuePublishRes)(nil), // 7: DBQueuePublishRes
|
||||
}
|
||||
var file_test_rpc_messagequeue_proto_depIdxs = []int32{
|
||||
var file_rpcproto_messagequeue_proto_depIdxs = []int32{
|
||||
0, // 0: DBQueueSubscribeReq.SubType:type_name -> SubscribeType
|
||||
1, // 1: DBQueueSubscribeReq.Method:type_name -> SubscribeMethod
|
||||
2, // [2:2] is the sub-list for method output_type
|
||||
@@ -572,13 +572,13 @@ var file_test_rpc_messagequeue_proto_depIdxs = []int32{
|
||||
0, // [0:2] is the sub-list for field type_name
|
||||
}
|
||||
|
||||
func init() { file_test_rpc_messagequeue_proto_init() }
|
||||
func file_test_rpc_messagequeue_proto_init() {
|
||||
if File_test_rpc_messagequeue_proto != nil {
|
||||
func init() { file_rpcproto_messagequeue_proto_init() }
|
||||
func file_rpcproto_messagequeue_proto_init() {
|
||||
if File_rpcproto_messagequeue_proto != nil {
|
||||
return
|
||||
}
|
||||
if !protoimpl.UnsafeEnabled {
|
||||
file_test_rpc_messagequeue_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} {
|
||||
file_rpcproto_messagequeue_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} {
|
||||
switch v := v.(*DBQueuePopReq); i {
|
||||
case 0:
|
||||
return &v.state
|
||||
@@ -590,7 +590,7 @@ func file_test_rpc_messagequeue_proto_init() {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
file_test_rpc_messagequeue_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} {
|
||||
file_rpcproto_messagequeue_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} {
|
||||
switch v := v.(*DBQueuePopRes); i {
|
||||
case 0:
|
||||
return &v.state
|
||||
@@ -602,7 +602,7 @@ func file_test_rpc_messagequeue_proto_init() {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
file_test_rpc_messagequeue_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} {
|
||||
file_rpcproto_messagequeue_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} {
|
||||
switch v := v.(*DBQueueSubscribeReq); i {
|
||||
case 0:
|
||||
return &v.state
|
||||
@@ -614,7 +614,7 @@ func file_test_rpc_messagequeue_proto_init() {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
file_test_rpc_messagequeue_proto_msgTypes[3].Exporter = func(v interface{}, i int) interface{} {
|
||||
file_rpcproto_messagequeue_proto_msgTypes[3].Exporter = func(v interface{}, i int) interface{} {
|
||||
switch v := v.(*DBQueueSubscribeRes); i {
|
||||
case 0:
|
||||
return &v.state
|
||||
@@ -626,7 +626,7 @@ func file_test_rpc_messagequeue_proto_init() {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
file_test_rpc_messagequeue_proto_msgTypes[4].Exporter = func(v interface{}, i int) interface{} {
|
||||
file_rpcproto_messagequeue_proto_msgTypes[4].Exporter = func(v interface{}, i int) interface{} {
|
||||
switch v := v.(*DBQueuePublishReq); i {
|
||||
case 0:
|
||||
return &v.state
|
||||
@@ -638,7 +638,7 @@ func file_test_rpc_messagequeue_proto_init() {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
file_test_rpc_messagequeue_proto_msgTypes[5].Exporter = func(v interface{}, i int) interface{} {
|
||||
file_rpcproto_messagequeue_proto_msgTypes[5].Exporter = func(v interface{}, i int) interface{} {
|
||||
switch v := v.(*DBQueuePublishRes); i {
|
||||
case 0:
|
||||
return &v.state
|
||||
@@ -655,19 +655,19 @@ func file_test_rpc_messagequeue_proto_init() {
|
||||
out := protoimpl.TypeBuilder{
|
||||
File: protoimpl.DescBuilder{
|
||||
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
|
||||
RawDescriptor: file_test_rpc_messagequeue_proto_rawDesc,
|
||||
RawDescriptor: file_rpcproto_messagequeue_proto_rawDesc,
|
||||
NumEnums: 2,
|
||||
NumMessages: 6,
|
||||
NumExtensions: 0,
|
||||
NumServices: 0,
|
||||
},
|
||||
GoTypes: file_test_rpc_messagequeue_proto_goTypes,
|
||||
DependencyIndexes: file_test_rpc_messagequeue_proto_depIdxs,
|
||||
EnumInfos: file_test_rpc_messagequeue_proto_enumTypes,
|
||||
MessageInfos: file_test_rpc_messagequeue_proto_msgTypes,
|
||||
GoTypes: file_rpcproto_messagequeue_proto_goTypes,
|
||||
DependencyIndexes: file_rpcproto_messagequeue_proto_depIdxs,
|
||||
EnumInfos: file_rpcproto_messagequeue_proto_enumTypes,
|
||||
MessageInfos: file_rpcproto_messagequeue_proto_msgTypes,
|
||||
}.Build()
|
||||
File_test_rpc_messagequeue_proto = out.File
|
||||
file_test_rpc_messagequeue_proto_rawDesc = nil
|
||||
file_test_rpc_messagequeue_proto_goTypes = nil
|
||||
file_test_rpc_messagequeue_proto_depIdxs = nil
|
||||
File_rpcproto_messagequeue_proto = out.File
|
||||
file_rpcproto_messagequeue_proto_rawDesc = nil
|
||||
file_rpcproto_messagequeue_proto_goTypes = nil
|
||||
file_rpcproto_messagequeue_proto_depIdxs = nil
|
||||
}
|
||||
|
||||
@@ -31,7 +31,7 @@ message DBQueueSubscribeReq {
|
||||
SubscribeType SubType = 1; //订阅类型
|
||||
SubscribeMethod Method = 2; //订阅方法
|
||||
string CustomerId = 3; //消费者Id
|
||||
int32 FromNodeId = 4;
|
||||
string FromNodeId = 4;
|
||||
string RpcMethod = 5;
|
||||
string TopicName = 6; //主题名称
|
||||
uint64 StartIndex = 7; //开始位置 ,格式前4位是时间戳秒,后面是序号。如果填0时,服务自动修改成:(4bit 当前时间秒)| (0000 4bit)
|
||||
|
||||
@@ -1,13 +1,14 @@
|
||||
package rpc
|
||||
|
||||
import (
|
||||
"github.com/duanhf2012/origin/v2/log"
|
||||
"github.com/duanhf2012/origin/v2/network"
|
||||
"github.com/nats-io/nats.go"
|
||||
"reflect"
|
||||
"time"
|
||||
"github.com/nats-io/nats.go"
|
||||
"github.com/duanhf2012/origin/v2/log"
|
||||
)
|
||||
|
||||
//跨结点连接的Client
|
||||
// NatsClient 跨结点连接的Client
|
||||
type NatsClient struct {
|
||||
localNodeId string
|
||||
notifyEventFun NotifyEventFun
|
||||
@@ -28,7 +29,7 @@ func (nc *NatsClient) onSubscribe(msg *nats.Msg){
|
||||
nc.client.processRpcResponse(msg.Data)
|
||||
}
|
||||
|
||||
func (nc *NatsClient) SetConn(conn *network.TCPConn){
|
||||
func (nc *NatsClient) SetConn(conn *network.NetConn) {
|
||||
}
|
||||
|
||||
func (nc *NatsClient) Close(waitDone bool) {
|
||||
@@ -40,12 +41,12 @@ func (nc *NatsClient) Run(){
|
||||
func (nc *NatsClient) OnClose() {
|
||||
}
|
||||
|
||||
func (rc *NatsClient) Bind(server IServer){
|
||||
func (nc *NatsClient) Bind(server IServer) {
|
||||
s := server.(*NatsServer)
|
||||
rc.natsConn = s.natsConn
|
||||
nc.natsConn = s.natsConn
|
||||
}
|
||||
|
||||
func (rc *NatsClient) Go(nodeId string,timeout time.Duration,rpcHandler IRpcHandler,noReply bool, serviceMethod string, args interface{}, reply interface{}) *Call {
|
||||
func (nc *NatsClient) Go(nodeId string, timeout time.Duration, rpcHandler IRpcHandler, noReply bool, serviceMethod string, args interface{}, reply interface{}) *Call {
|
||||
_, processor := GetProcessorType(args)
|
||||
InParam, err := processor.Marshal(args)
|
||||
if err != nil {
|
||||
@@ -55,15 +56,15 @@ func (rc *NatsClient) Go(nodeId string,timeout time.Duration,rpcHandler IRpcHand
|
||||
return call
|
||||
}
|
||||
|
||||
return rc.client.rawGo(nodeId,rc,timeout,rpcHandler,processor, noReply, 0, serviceMethod, InParam, reply)
|
||||
return nc.client.rawGo(nodeId, nc, timeout, rpcHandler, processor, noReply, 0, serviceMethod, InParam, reply)
|
||||
}
|
||||
|
||||
func (rc *NatsClient) RawGo(nodeId string,timeout time.Duration,rpcHandler IRpcHandler,processor IRpcProcessor, noReply bool, rpcMethodId uint32, serviceMethod string, rawArgs []byte, reply interface{}) *Call {
|
||||
return rc.client.rawGo(nodeId,rc,timeout,rpcHandler,processor, noReply, rpcMethodId, serviceMethod, rawArgs, reply)
|
||||
func (nc *NatsClient) RawGo(nodeId string, timeout time.Duration, rpcHandler IRpcHandler, processor IRpcProcessor, noReply bool, rpcMethodId uint32, serviceMethod string, rawArgs []byte, reply interface{}) *Call {
|
||||
return nc.client.rawGo(nodeId, nc, timeout, rpcHandler, processor, noReply, rpcMethodId, serviceMethod, rawArgs, reply)
|
||||
}
|
||||
|
||||
func (rc *NatsClient) AsyncCall(nodeId string,timeout time.Duration,rpcHandler IRpcHandler, serviceMethod string, callback reflect.Value, args interface{}, replyParam interface{},cancelable bool) (CancelRpc,error) {
|
||||
cancelRpc,err := rc.client.asyncCall(nodeId,rc,timeout,rpcHandler, serviceMethod, callback, args, replyParam,cancelable)
|
||||
func (nc *NatsClient) AsyncCall(nodeId string, timeout time.Duration, rpcHandler IRpcHandler, serviceMethod string, callback reflect.Value, args interface{}, replyParam interface{}, cancelable bool) (CancelRpc, error) {
|
||||
cancelRpc, err := nc.client.asyncCall(nodeId, nc, timeout, rpcHandler, serviceMethod, callback, args, replyParam, cancelable)
|
||||
if err != nil {
|
||||
callback.Call([]reflect.Value{reflect.ValueOf(replyParam), reflect.ValueOf(err)})
|
||||
}
|
||||
@@ -71,7 +72,7 @@ func (rc *NatsClient) AsyncCall(nodeId string,timeout time.Duration,rpcHandler I
|
||||
return cancelRpc, nil
|
||||
}
|
||||
|
||||
func (rc *NatsClient) WriteMsg (nodeId string,args ...[]byte) error{
|
||||
func (nc *NatsClient) WriteMsg(nodeId string, args ...[]byte) error {
|
||||
buff := make([]byte, 0, 4096)
|
||||
for _, ar := range args {
|
||||
buff = append(buff, ar...)
|
||||
@@ -81,10 +82,10 @@ func (rc *NatsClient) WriteMsg (nodeId string,args ...[]byte) error{
|
||||
msg.Subject = "os." + nodeId
|
||||
msg.Data = buff
|
||||
msg.Header = nats.Header{}
|
||||
msg.Header.Set("fnode",rc.localNodeId)
|
||||
return rc.natsConn.PublishMsg(&msg)
|
||||
msg.Header.Set("fnode", nc.localNodeId)
|
||||
return nc.natsConn.PublishMsg(&msg)
|
||||
}
|
||||
|
||||
func (rc *NatsClient) IsConnected() bool{
|
||||
return rc.natsConn!=nil && rc.natsConn.Status() == nats.CONNECTED
|
||||
func (nc *NatsClient) IsConnected() bool {
|
||||
return nc.natsConn != nil && nc.natsConn.Status() == nats.CONNECTED
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package rpc
|
||||
|
||||
import (
|
||||
"github.com/nats-io/nats.go"
|
||||
"github.com/duanhf2012/origin/v2/log"
|
||||
"github.com/nats-io/nats.go"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -19,6 +19,7 @@ type NatsServer struct {
|
||||
}
|
||||
|
||||
const reconnectWait = 3 * time.Second
|
||||
|
||||
func (ns *NatsServer) Start() error {
|
||||
var err error
|
||||
var options []nats.Option
|
||||
@@ -31,7 +32,6 @@ func (ns *NatsServer) Start() error{
|
||||
options = append(options, nats.ConnectHandler(func(nc *nats.Conn) {
|
||||
log.Info("nats is connected", log.String("connUrl", nc.ConnectedUrl()))
|
||||
ns.notifyEventFun(&NatsConnEvent{IsConnect: true})
|
||||
//log.Error("nats is connect",log.String("connUrl",nc.ConnectedUrl()))
|
||||
}))
|
||||
|
||||
options = append(options, nats.ReconnectHandler(func(nc *nats.Conn) {
|
||||
@@ -77,7 +77,7 @@ func (ns *NatsServer) WriteResponse(processor IRpcProcessor, nodeId string,servi
|
||||
defer processor.ReleaseRpcResponse(rpcResponse.RpcResponseData)
|
||||
|
||||
if err != nil {
|
||||
log.Error("mashal RpcResponseData failed",log.String("serviceMethod",serviceMethod),log.ErrorAttr("error",err))
|
||||
log.Error("marshal RpcResponseData failed", log.String("serviceMethod", serviceMethod), log.ErrorAttr("error", err))
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
package rpc
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duanhf2012/origin/v2/util/sync"
|
||||
"google.golang.org/protobuf/proto"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type PBProcessor struct {
|
||||
@@ -27,8 +27,7 @@ func (slf *PBRpcRequestData) MakeRequest(seq uint64,rpcMethodId uint32,serviceMe
|
||||
return slf
|
||||
}
|
||||
|
||||
|
||||
func (slf *PBRpcResponseData) MakeRespone(seq uint64,err RpcError,reply []byte) *PBRpcResponseData{
|
||||
func (slf *PBRpcResponseData) MakeResponse(seq uint64, err RpcError, reply []byte) *PBRpcResponseData {
|
||||
slf.Seq = seq
|
||||
slf.Error = err.Error()
|
||||
slf.Reply = reply
|
||||
@@ -56,7 +55,7 @@ func (slf *PBProcessor) MakeRpcRequest(seq uint64,rpcMethodId uint32,serviceMeth
|
||||
|
||||
func (slf *PBProcessor) MakeRpcResponse(seq uint64, err RpcError, reply []byte) IRpcResponseData {
|
||||
pPBRpcResponseData := rpcPbResponseDataPool.Get().(*PBRpcResponseData)
|
||||
pPBRpcResponseData.MakeRespone(seq,err,reply)
|
||||
pPBRpcResponseData.MakeResponse(seq, err, reply)
|
||||
return pPBRpcResponseData
|
||||
}
|
||||
|
||||
@@ -98,9 +97,3 @@ func (slf *PBRpcResponseData) GetErr() *RpcError {
|
||||
err := RpcError(slf.GetError())
|
||||
return &err
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -11,11 +11,11 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
//跨结点连接的Client
|
||||
// RClient 跨结点连接的Client
|
||||
type RClient struct {
|
||||
selfClient *Client
|
||||
network.TCPClient
|
||||
conn *network.TCPConn
|
||||
conn *network.NetConn
|
||||
|
||||
notifyEventFun NotifyEventFun
|
||||
}
|
||||
@@ -27,7 +27,7 @@ func (rc *RClient) IsConnected() bool {
|
||||
return rc.conn != nil && rc.conn.IsConnected() == true
|
||||
}
|
||||
|
||||
func (rc *RClient) GetConn() *network.TCPConn{
|
||||
func (rc *RClient) GetConn() *network.NetConn {
|
||||
rc.Lock()
|
||||
conn := rc.conn
|
||||
rc.Unlock()
|
||||
@@ -35,7 +35,7 @@ func (rc *RClient) GetConn() *network.TCPConn{
|
||||
return conn
|
||||
}
|
||||
|
||||
func (rc *RClient) SetConn(conn *network.TCPConn){
|
||||
func (rc *RClient) SetConn(conn *network.NetConn) {
|
||||
rc.Lock()
|
||||
rc.conn = conn
|
||||
rc.Unlock()
|
||||
@@ -89,7 +89,7 @@ func (rc *RClient) Run() {
|
||||
for {
|
||||
bytes, err := rc.conn.ReadMsg()
|
||||
if err != nil {
|
||||
log.Error("rclient read msg is failed",log.ErrorAttr("error",err))
|
||||
log.Error("RClient read msg is failed", log.ErrorAttr("error", err))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -140,13 +140,11 @@ func NewRClient(targetNodeId string, addr string, maxRpcParamLen uint32,compress
|
||||
return client
|
||||
}
|
||||
|
||||
|
||||
func (rc *RClient) Close(waitDone bool) {
|
||||
rc.TCPClient.Close(waitDone)
|
||||
rc.selfClient.cleanPending()
|
||||
}
|
||||
|
||||
|
||||
func (rc *RClient) Bind(server IServer) {
|
||||
|
||||
}
|
||||
@@ -3,20 +3,21 @@ package rpc
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/duanhf2012/origin/v2/event"
|
||||
"github.com/duanhf2012/origin/v2/log"
|
||||
"reflect"
|
||||
"runtime"
|
||||
"strings"
|
||||
"time"
|
||||
"unicode"
|
||||
"unicode/utf8"
|
||||
"github.com/duanhf2012/origin/v2/event"
|
||||
"time"
|
||||
)
|
||||
|
||||
const maxClusterNode int = 128
|
||||
const maxClusterNode int = 32
|
||||
|
||||
type FuncRpcClient func(nodeId string, serviceMethod string,filterRetire bool, client []*Client) (error, int)
|
||||
type FuncRpcClient func(nodeId string, serviceMethod string, filterRetire bool, client []*Client) (error, []*Client)
|
||||
type FuncRpcServer func() IServer
|
||||
|
||||
const NodeIdNull = ""
|
||||
|
||||
var nilError = reflect.Zero(reflect.TypeOf((*error)(nil)).Elem())
|
||||
@@ -63,10 +64,10 @@ type RpcHandler struct {
|
||||
funcRpcClient FuncRpcClient
|
||||
funcRpcServer FuncRpcServer
|
||||
|
||||
pClientList []*Client
|
||||
//pClientList []*Client
|
||||
}
|
||||
|
||||
//type TriggerRpcConnEvent func(bConnect bool, clientSeq uint32, nodeId string)
|
||||
// NotifyEventToAllService type TriggerRpcConnEvent func(bConnect bool, clientSeq uint32, nodeId string)
|
||||
type NotifyEventToAllService func(event event.IEvent)
|
||||
|
||||
type INodeConnListener interface {
|
||||
@@ -85,6 +86,7 @@ type IDiscoveryServiceListener interface {
|
||||
}
|
||||
|
||||
type CancelRpc func()
|
||||
|
||||
func emptyCancelRpc() {}
|
||||
|
||||
type IRpcHandler interface {
|
||||
@@ -110,7 +112,6 @@ type IRpcHandler interface {
|
||||
GoNode(nodeId string, serviceMethod string, args interface{}) error
|
||||
RawGoNode(rpcProcessorType RpcProcessorType, nodeId string, rpcMethodId uint32, serviceName string, rawArgs []byte) error
|
||||
CastGo(serviceMethod string, args interface{}) error
|
||||
IsSingleCoroutine() bool
|
||||
UnmarshalInParam(rpcProcessor IRpcProcessor, serviceMethod string, rawRpcMethodId uint32, inParam []byte) (interface{}, error)
|
||||
GetRpcServer() FuncRpcServer
|
||||
}
|
||||
@@ -135,7 +136,6 @@ func (handler *RpcHandler) InitRpcHandler(rpcHandler IRpcHandler, getClientFun F
|
||||
handler.mapFunctions = map[string]RpcMethodInfo{}
|
||||
handler.funcRpcClient = getClientFun
|
||||
handler.funcRpcServer = getServerFun
|
||||
handler.pClientList = make([]*Client, maxClusterNode)
|
||||
handler.RegisterRpc(rpcHandler)
|
||||
}
|
||||
|
||||
@@ -311,15 +311,15 @@ func (handler *RpcHandler) HandlerRpcRequest(request *RpcRequest) {
|
||||
return
|
||||
}
|
||||
|
||||
requestHanle := request.requestHandle
|
||||
requestHandle := request.requestHandle
|
||||
returnValues := v.method.Func.Call(paramList)
|
||||
errInter := returnValues[0].Interface()
|
||||
if errInter != nil {
|
||||
err = errInter.(error)
|
||||
}
|
||||
|
||||
if v.hasResponder == false && requestHanle != nil {
|
||||
requestHanle(oParam.Interface(), ConvertError(err))
|
||||
if v.hasResponder == false && requestHandle != nil {
|
||||
requestHandle(oParam.Interface(), ConvertError(err))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -349,7 +349,7 @@ func (handler *RpcHandler) CallMethod(client *Client,ServiceMethod string, param
|
||||
//有返回值时
|
||||
if reply != nil {
|
||||
//如果是Call同步调用
|
||||
hander :=func(Returns interface{}, Err RpcError) {
|
||||
hd := func(Returns interface{}, Err RpcError) {
|
||||
rpcCall := client.RemovePending(callSeq)
|
||||
if rpcCall == nil {
|
||||
log.Error("cannot find call seq", log.Uint64("seq", callSeq))
|
||||
@@ -372,7 +372,7 @@ func (handler *RpcHandler) CallMethod(client *Client,ServiceMethod string, param
|
||||
rpcCall.Reply = reply
|
||||
rpcCall.done <- rpcCall
|
||||
}
|
||||
paramList = append(paramList, reflect.ValueOf(hander))
|
||||
paramList = append(paramList, reflect.ValueOf(hd))
|
||||
} else { //无返回值时,是一个requestHandlerNull空回调
|
||||
paramList = append(paramList, callBack)
|
||||
}
|
||||
@@ -435,9 +435,9 @@ func (handler *RpcHandler) CallMethod(client *Client,ServiceMethod string, param
|
||||
}
|
||||
|
||||
func (handler *RpcHandler) goRpc(processor IRpcProcessor, bCast bool, nodeId string, serviceMethod string, args interface{}) error {
|
||||
var pClientList [maxClusterNode]*Client
|
||||
err, count := handler.funcRpcClient(nodeId, serviceMethod,false, pClientList[:])
|
||||
if count == 0 {
|
||||
pClientList := make([]*Client, 0, maxClusterNode)
|
||||
err, pClientList := handler.funcRpcClient(nodeId, serviceMethod, false, pClientList)
|
||||
if len(pClientList) == 0 {
|
||||
if err != nil {
|
||||
log.Error("call serviceMethod is failed", log.String("serviceMethod", serviceMethod), log.ErrorAttr("error", err))
|
||||
} else {
|
||||
@@ -446,13 +446,13 @@ func (handler *RpcHandler) goRpc(processor IRpcProcessor, bCast bool, nodeId str
|
||||
return err
|
||||
}
|
||||
|
||||
if count > 1 && bCast == false {
|
||||
if len(pClientList) > 1 && bCast == false {
|
||||
log.Error("cannot call serviceMethod more then 1 node", log.String("serviceMethod", serviceMethod))
|
||||
return errors.New("cannot call more then 1 node")
|
||||
}
|
||||
|
||||
//2.rpcClient调用
|
||||
for i := 0; i < count; i++ {
|
||||
for i := 0; i < len(pClientList); i++ {
|
||||
pCall := pClientList[i].Go(pClientList[i].GetTargetNodeId(), DefaultRpcTimeout, handler.rpcHandler, true, serviceMethod, args, nil)
|
||||
if pCall.Err != nil {
|
||||
err = pCall.Err
|
||||
@@ -465,16 +465,16 @@ func (handler *RpcHandler) goRpc(processor IRpcProcessor, bCast bool, nodeId str
|
||||
}
|
||||
|
||||
func (handler *RpcHandler) callRpc(timeout time.Duration, nodeId string, serviceMethod string, args interface{}, reply interface{}) error {
|
||||
var pClientList [maxClusterNode]*Client
|
||||
err, count := handler.funcRpcClient(nodeId, serviceMethod,false, pClientList[:])
|
||||
pClientList := make([]*Client, 0, maxClusterNode)
|
||||
err, pClientList := handler.funcRpcClient(nodeId, serviceMethod, false, pClientList)
|
||||
if err != nil {
|
||||
log.Error("Call serviceMethod is failed", log.ErrorAttr("error", err))
|
||||
return err
|
||||
} else if count <= 0 {
|
||||
} else if len(pClientList) <= 0 {
|
||||
err = errors.New("Call serviceMethod is error:cannot find " + serviceMethod)
|
||||
log.Error("cannot find serviceMethod", log.String("serviceMethod", serviceMethod))
|
||||
return err
|
||||
} else if count > 1 {
|
||||
} else if len(pClientList) > 1 {
|
||||
log.Error("Cannot call more then 1 node!", log.String("serviceMethod", serviceMethod))
|
||||
return errors.New("cannot call more then 1 node")
|
||||
}
|
||||
@@ -509,14 +509,14 @@ func (handler *RpcHandler) asyncCallRpc(timeout time.Duration,nodeId string, ser
|
||||
}
|
||||
|
||||
reply := reflect.New(fVal.Type().In(0).Elem()).Interface()
|
||||
var pClientList [2]*Client
|
||||
err, count := handler.funcRpcClient(nodeId, serviceMethod,false, pClientList[:])
|
||||
if count == 0 || err != nil {
|
||||
pClientList := make([]*Client, 0, 1)
|
||||
err, pClientList := handler.funcRpcClient(nodeId, serviceMethod, false, pClientList[:])
|
||||
if len(pClientList) == 0 || err != nil {
|
||||
if err == nil {
|
||||
if nodeId != NodeIdNull {
|
||||
err = fmt.Errorf("cannot find %s from nodeId %d",serviceMethod,nodeId)
|
||||
err = fmt.Errorf("cannot find %s from nodeId %s", serviceMethod, nodeId)
|
||||
} else {
|
||||
err = fmt.Errorf("No %s service found in the origin network",serviceMethod)
|
||||
err = fmt.Errorf("no %s service found in the origin network", serviceMethod)
|
||||
}
|
||||
}
|
||||
fVal.Call([]reflect.Value{reflect.ValueOf(reply), reflect.ValueOf(err)})
|
||||
@@ -524,7 +524,7 @@ func (handler *RpcHandler) asyncCallRpc(timeout time.Duration,nodeId string, ser
|
||||
return emptyCancelRpc, nil
|
||||
}
|
||||
|
||||
if count > 1 {
|
||||
if len(pClientList) > 1 {
|
||||
err := errors.New("cannot call more then 1 node")
|
||||
fVal.Call([]reflect.Value{reflect.ValueOf(reply), reflect.ValueOf(err)})
|
||||
log.Error("cannot call more then 1 node", log.String("serviceMethod", serviceMethod))
|
||||
@@ -540,10 +540,6 @@ func (handler *RpcHandler) GetName() string {
|
||||
return handler.rpcHandler.GetName()
|
||||
}
|
||||
|
||||
func (handler *RpcHandler) IsSingleCoroutine() bool {
|
||||
return handler.rpcHandler.IsSingleCoroutine()
|
||||
}
|
||||
|
||||
func (handler *RpcHandler) CallWithTimeout(timeout time.Duration, serviceMethod string, args interface{}, reply interface{}) error {
|
||||
return handler.callRpc(timeout, NodeIdNull, serviceMethod, args, reply)
|
||||
}
|
||||
@@ -593,12 +589,13 @@ func (handler *RpcHandler) CastGo(serviceMethod string, args interface{}) error
|
||||
|
||||
func (handler *RpcHandler) RawGoNode(rpcProcessorType RpcProcessorType, nodeId string, rpcMethodId uint32, serviceName string, rawArgs []byte) error {
|
||||
processor := GetProcessor(uint8(rpcProcessorType))
|
||||
err, count := handler.funcRpcClient(nodeId, serviceName,false, handler.pClientList)
|
||||
if count == 0 || err != nil {
|
||||
pClientList := make([]*Client, 0, 1)
|
||||
err, pClientList := handler.funcRpcClient(nodeId, serviceName, false, pClientList)
|
||||
if len(pClientList) == 0 || err != nil {
|
||||
log.Error("call serviceMethod is failed", log.ErrorAttr("error", err))
|
||||
return err
|
||||
}
|
||||
if count > 1 {
|
||||
if len(pClientList) > 1 {
|
||||
err := errors.New("cannot call more then 1 node")
|
||||
log.Error("cannot call more then 1 node", log.String("serviceName", serviceName))
|
||||
return err
|
||||
@@ -606,14 +603,14 @@ func (handler *RpcHandler) RawGoNode(rpcProcessorType RpcProcessorType, nodeId s
|
||||
|
||||
//2.rpcClient调用
|
||||
//如果调用本结点服务
|
||||
for i := 0; i < count; i++ {
|
||||
for i := 0; i < len(pClientList); i++ {
|
||||
//跨node调用
|
||||
pCall := handler.pClientList[i].RawGo(handler.pClientList[i].GetTargetNodeId(),DefaultRpcTimeout,handler.rpcHandler,processor, true, rpcMethodId, serviceName, rawArgs, nil)
|
||||
pCall := pClientList[i].RawGo(pClientList[i].GetTargetNodeId(), DefaultRpcTimeout, handler.rpcHandler, processor, true, rpcMethodId, serviceName, rawArgs, nil)
|
||||
if pCall.Err != nil {
|
||||
err = pCall.Err
|
||||
}
|
||||
|
||||
handler.pClientList[i].RemovePending(pCall.Seq)
|
||||
pClientList[i].RemovePending(pCall.Seq)
|
||||
ReleaseCall(pCall)
|
||||
}
|
||||
|
||||
@@ -640,7 +637,6 @@ func (handler *RpcHandler) UnmarshalInParam(rpcProcessor IRpcProcessor, serviceM
|
||||
return param, err
|
||||
}
|
||||
|
||||
|
||||
func (handler *RpcHandler) GetRpcServer() FuncRpcServer {
|
||||
return handler.funcRpcServer
|
||||
}
|
||||
|
||||
@@ -7,12 +7,13 @@ import (
|
||||
"math"
|
||||
"net"
|
||||
"reflect"
|
||||
"runtime"
|
||||
"strings"
|
||||
"time"
|
||||
"runtime"
|
||||
)
|
||||
|
||||
const Default_ReadWriteDeadline = 15 * time.Second
|
||||
|
||||
type RpcProcessorType uint8
|
||||
|
||||
const (
|
||||
@@ -89,7 +90,6 @@ func (server *Server) Start() error{
|
||||
|
||||
server.rpcServer.Addr = ":" + splitAddr[1]
|
||||
server.rpcServer.MinMsgLen = 2
|
||||
server.compressBytesLen = server.compressBytesLen
|
||||
if server.maxRpcParamLen > 0 {
|
||||
server.rpcServer.MaxMsgLen = server.maxRpcParamLen
|
||||
} else {
|
||||
@@ -130,7 +130,7 @@ func (agent *RpcAgent) WriteResponse(processor IRpcProcessor, connTag string, se
|
||||
defer processor.ReleaseRpcResponse(rpcResponse.RpcResponseData)
|
||||
|
||||
if errM != nil {
|
||||
log.Error("mashal RpcResponseData failed",log.String("serviceMethod",serviceMethod),log.ErrorAttr("error",errM))
|
||||
log.Error("marshal RpcResponseData failed", log.String("serviceMethod", serviceMethod), log.ErrorAttr("error", errM))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -172,18 +172,20 @@ func (agent *RpcAgent) Run() {
|
||||
for {
|
||||
data, err := agent.conn.ReadMsg()
|
||||
if err != nil {
|
||||
//will close conn
|
||||
log.Error("read message is error", log.String("remoteAddress", agent.conn.RemoteAddr().String()), log.ErrorAttr("error", err))
|
||||
//will close tcpconn
|
||||
break
|
||||
}
|
||||
|
||||
defer agent.conn.ReleaseReadMsg(data)
|
||||
err = agent.rpcServer.processRpcRequest(data, "", agent.WriteResponse)
|
||||
if err != nil {
|
||||
//will close conn
|
||||
agent.conn.ReleaseReadMsg(data)
|
||||
log.Error("processRpcRequest is error", log.String("remoteAddress", agent.conn.RemoteAddr().String()), log.ErrorAttr("error", err))
|
||||
//will close tcpconn
|
||||
|
||||
break
|
||||
}
|
||||
agent.conn.ReleaseReadMsg(data)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -209,9 +211,8 @@ func (agent *RpcAgent) Destroy() {
|
||||
agent.conn.Destroy()
|
||||
}
|
||||
|
||||
func (server *Server) NewAgent(c *network.TCPConn) network.Agent {
|
||||
func (server *Server) NewAgent(c network.Conn) network.Agent {
|
||||
agent := &RpcAgent{conn: c, rpcServer: server}
|
||||
|
||||
return agent
|
||||
}
|
||||
|
||||
|
||||
@@ -6,11 +6,11 @@ import (
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/duanhf2012/origin/v2/concurrent"
|
||||
"github.com/duanhf2012/origin/v2/event"
|
||||
"github.com/duanhf2012/origin/v2/log"
|
||||
rpcHandle "github.com/duanhf2012/origin/v2/rpc"
|
||||
"github.com/duanhf2012/origin/v2/util/timer"
|
||||
"github.com/duanhf2012/origin/v2/concurrent"
|
||||
)
|
||||
|
||||
const InitModuleId = 1e9
|
||||
@@ -35,9 +35,9 @@ type IModule interface {
|
||||
}
|
||||
|
||||
type IModuleTimer interface {
|
||||
AfterFunc(d time.Duration, cb func(*timer.Timer)) *timer.Timer
|
||||
CronFunc(cronExpr *timer.CronExpr, cb func(*timer.Cron)) *timer.Cron
|
||||
NewTicker(d time.Duration, cb func(*timer.Ticker)) *timer.Ticker
|
||||
SafeAfterFunc(d time.Duration, cb func(*timer.Timer)) *timer.Timer
|
||||
SafeCronFunc(cronExpr *timer.CronExpr, cb func(*timer.Cron)) *timer.Cron
|
||||
SafeNewTicker(d time.Duration, cb func(*timer.Ticker)) *timer.Ticker
|
||||
}
|
||||
|
||||
type Module struct {
|
||||
@@ -208,6 +208,7 @@ func (m *Module) OnAddTimer(t timer.ITimer) {
|
||||
}
|
||||
}
|
||||
|
||||
// Deprecated: this function simply calls SafeAfterFunc
|
||||
func (m *Module) AfterFunc(d time.Duration, cb func(*timer.Timer)) *timer.Timer {
|
||||
if m.mapActiveTimer == nil {
|
||||
m.mapActiveTimer = map[timer.ITimer]struct{}{}
|
||||
@@ -216,6 +217,7 @@ func (m *Module) AfterFunc(d time.Duration, cb func(*timer.Timer)) *timer.Timer
|
||||
return m.dispatcher.AfterFunc(d, nil, cb, m.OnCloseTimer, m.OnAddTimer)
|
||||
}
|
||||
|
||||
// Deprecated: this function simply calls SafeCronFunc
|
||||
func (m *Module) CronFunc(cronExpr *timer.CronExpr, cb func(*timer.Cron)) *timer.Cron {
|
||||
if m.mapActiveTimer == nil {
|
||||
m.mapActiveTimer = map[timer.ITimer]struct{}{}
|
||||
@@ -224,6 +226,7 @@ func (m *Module) CronFunc(cronExpr *timer.CronExpr, cb func(*timer.Cron)) *timer
|
||||
return m.dispatcher.CronFunc(cronExpr, nil, cb, m.OnCloseTimer, m.OnAddTimer)
|
||||
}
|
||||
|
||||
// Deprecated: this function simply calls SafeNewTicker
|
||||
func (m *Module) NewTicker(d time.Duration, cb func(*timer.Ticker)) *timer.Ticker {
|
||||
if m.mapActiveTimer == nil {
|
||||
m.mapActiveTimer = map[timer.ITimer]struct{}{}
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/duanhf2012/origin/v2/concurrent"
|
||||
"github.com/duanhf2012/origin/v2/event"
|
||||
"github.com/duanhf2012/origin/v2/log"
|
||||
"github.com/duanhf2012/origin/v2/profiler"
|
||||
@@ -13,7 +15,6 @@ import (
|
||||
"strconv"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"github.com/duanhf2012/origin/v2/concurrent"
|
||||
)
|
||||
|
||||
var timerDispatcherLen = 100000
|
||||
@@ -55,6 +56,7 @@ type Service struct {
|
||||
serviceCfg interface{}
|
||||
goroutineNum int32
|
||||
startStatus bool
|
||||
isRelease int32
|
||||
retire int32
|
||||
eventProcessor event.IEventProcessor
|
||||
profiler *profiler.Profiler //性能分析器
|
||||
@@ -65,8 +67,6 @@ type Service struct {
|
||||
closeSig chan struct{}
|
||||
}
|
||||
|
||||
|
||||
|
||||
// DiscoveryServiceEvent 发现服务结点
|
||||
type DiscoveryServiceEvent struct {
|
||||
IsDiscovery bool
|
||||
@@ -82,7 +82,6 @@ type EtcdServiceRecordEvent struct {
|
||||
}
|
||||
|
||||
type Empty struct {
|
||||
|
||||
}
|
||||
|
||||
func SetMaxServiceChannel(maxEventChannel int) {
|
||||
@@ -93,7 +92,6 @@ func (rpcEventData *DiscoveryServiceEvent) GetEventType() event.EventType{
|
||||
return event.Sys_Event_DiscoverService
|
||||
}
|
||||
|
||||
|
||||
func (s *Service) OnSetup(iService IService) {
|
||||
if iService.GetName() == "" {
|
||||
s.name = reflect.Indirect(reflect.ValueOf(iService)).Type().Name()
|
||||
@@ -103,7 +101,7 @@ func (s *Service) OnSetup(iService IService){
|
||||
func (s *Service) OpenProfiler() {
|
||||
s.profiler = profiler.RegProfiler(s.GetName())
|
||||
if s.profiler == nil {
|
||||
log.Fatal("rofiler.RegProfiler "+s.GetName()+" fail.")
|
||||
log.Fatal("profiler.RegProfiler " + s.GetName() + " fail.")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -145,41 +143,43 @@ func (s *Service) Init(iService IService,getClientFun rpc.FuncRpcClient,getServe
|
||||
|
||||
func (s *Service) Start() {
|
||||
s.startStatus = true
|
||||
atomic.StoreInt32(&s.isRelease, 0)
|
||||
var waitRun sync.WaitGroup
|
||||
log.Info(s.GetName() + " service is running")
|
||||
s.self.(IService).OnStart()
|
||||
|
||||
for i := int32(0); i < s.goroutineNum; i++ {
|
||||
s.wg.Add(1)
|
||||
waitRun.Add(1)
|
||||
go func() {
|
||||
log.Info(s.GetName()+" service is running",)
|
||||
waitRun.Done()
|
||||
s.Run()
|
||||
s.run()
|
||||
}()
|
||||
}
|
||||
|
||||
waitRun.Wait()
|
||||
}
|
||||
|
||||
func (s *Service) Run() {
|
||||
func (s *Service) run() {
|
||||
defer s.wg.Done()
|
||||
var bStop = false
|
||||
|
||||
concurrent := s.IConcurrent.(*concurrent.Concurrent)
|
||||
concurrentCBChannel := concurrent.GetCallBackChannel()
|
||||
cr := s.IConcurrent.(*concurrent.Concurrent)
|
||||
concurrentCBChannel := cr.GetCallBackChannel()
|
||||
|
||||
s.self.(IService).OnStart()
|
||||
for {
|
||||
var analyzer *profiler.Analyzer
|
||||
select {
|
||||
case <-s.closeSig:
|
||||
bStop = true
|
||||
concurrent.Close()
|
||||
s.Release()
|
||||
cr.Close()
|
||||
case cb := <-concurrentCBChannel:
|
||||
concurrent.DoCallback(cb)
|
||||
cr.DoCallback(cb)
|
||||
case ev := <-s.chanEvent:
|
||||
switch ev.GetEventType() {
|
||||
case event.Sys_Event_Retire:
|
||||
log.Info("service OnRetire",log.String("servceName",s.GetName()))
|
||||
log.Info("service OnRetire", log.String("serviceName", s.GetName()))
|
||||
s.self.(IService).OnRetire()
|
||||
case event.ServiceRpcRequestEvent:
|
||||
cEvent, ok := ev.(*event.Event)
|
||||
@@ -245,10 +245,6 @@ func (s *Service) Run() {
|
||||
}
|
||||
|
||||
if bStop == true {
|
||||
if atomic.AddInt32(&s.goroutineNum,-1)<=0 {
|
||||
s.startStatus = false
|
||||
s.Release()
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
@@ -272,9 +268,12 @@ func (s *Service) Release(){
|
||||
}
|
||||
}()
|
||||
|
||||
if atomic.AddInt32(&s.isRelease, -1) == -1 {
|
||||
s.self.OnRelease()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (s *Service) OnRelease() {
|
||||
}
|
||||
|
||||
@@ -293,6 +292,24 @@ func (s *Service) GetServiceCfg()interface{}{
|
||||
return s.serviceCfg
|
||||
}
|
||||
|
||||
func (s *Service) ParseServiceCfg(cfg interface{}) error {
|
||||
if s.serviceCfg == nil {
|
||||
return errors.New("no service configuration found")
|
||||
}
|
||||
|
||||
rv := reflect.ValueOf(s.serviceCfg)
|
||||
if rv.Kind() == reflect.Ptr && rv.IsNil() {
|
||||
return errors.New("no service configuration found")
|
||||
}
|
||||
|
||||
bytes, err := json.Marshal(s.serviceCfg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return json.Unmarshal(bytes, cfg)
|
||||
}
|
||||
|
||||
func (s *Service) GetProfiler() *profiler.Profiler {
|
||||
return s.profiler
|
||||
}
|
||||
@@ -305,10 +322,6 @@ func (s *Service) UnRegEventReceiverFunc(eventType event.EventType, receiver eve
|
||||
s.eventProcessor.UnRegEventReceiverFun(eventType, receiver)
|
||||
}
|
||||
|
||||
func (s *Service) IsSingleCoroutine() bool {
|
||||
return s.goroutineNum == 1
|
||||
}
|
||||
|
||||
func (s *Service) RegRawRpc(rpcMethodId uint32, rawRpcCB rpc.RawRpcCallBack) {
|
||||
s.rpcHandler.RegRawRpc(rpcMethodId, rawRpcCB)
|
||||
}
|
||||
@@ -317,11 +330,11 @@ func (s *Service) OnStart(){
|
||||
}
|
||||
|
||||
func (s *Service) OnNodeConnEvent(ev event.IEvent) {
|
||||
event := ev.(*rpc.RpcConnEvent)
|
||||
if event.IsConnect {
|
||||
s.nodeConnLister.OnNodeConnected(event.NodeId)
|
||||
re := ev.(*rpc.RpcConnEvent)
|
||||
if re.IsConnect {
|
||||
s.nodeConnLister.OnNodeConnected(re.NodeId)
|
||||
} else {
|
||||
s.nodeConnLister.OnNodeDisconnect(event.NodeId)
|
||||
s.nodeConnLister.OnNodeDisconnect(re.NodeId)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -335,11 +348,11 @@ func (s *Service) OnNatsConnEvent(ev event.IEvent){
|
||||
}
|
||||
|
||||
func (s *Service) OnDiscoverServiceEvent(ev event.IEvent) {
|
||||
event := ev.(*DiscoveryServiceEvent)
|
||||
if event.IsDiscovery {
|
||||
s.discoveryServiceLister.OnDiscoveryService(event.NodeId,event.ServiceName)
|
||||
de := ev.(*DiscoveryServiceEvent)
|
||||
if de.IsDiscovery {
|
||||
s.discoveryServiceLister.OnDiscoveryService(de.NodeId, de.ServiceName)
|
||||
} else {
|
||||
s.discoveryServiceLister.OnUnDiscoveryService(event.NodeId,event.ServiceName)
|
||||
s.discoveryServiceLister.OnUnDiscoveryService(de.NodeId, de.ServiceName)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -368,12 +381,12 @@ func (s *Service) UnRegNatsConnListener() {
|
||||
func (s *Service) RegDiscoverListener(discoveryServiceListener rpc.IDiscoveryServiceListener) {
|
||||
s.discoveryServiceLister = discoveryServiceListener
|
||||
s.RegEventReceiverFunc(event.Sys_Event_DiscoverService, s.GetEventHandler(), s.OnDiscoverServiceEvent)
|
||||
RegDiscoveryServiceEventFun(s.GetName())
|
||||
RegRpcEventFun(s.GetName())
|
||||
}
|
||||
|
||||
func (s *Service) UnRegDiscoverListener() {
|
||||
s.UnRegEventReceiverFunc(event.Sys_Event_DiscoverService, s.GetEventHandler())
|
||||
UnRegDiscoveryServiceEventFun(s.GetName())
|
||||
UnRegRpcEventFun(s.GetName())
|
||||
}
|
||||
|
||||
func (s *Service) PushRpcRequest(rpcRequest *rpc.RpcRequest) error {
|
||||
@@ -398,7 +411,7 @@ func (s *Service) PushEvent(ev event.IEvent) error{
|
||||
|
||||
func (s *Service) pushEvent(ev event.IEvent) error {
|
||||
if len(s.chanEvent) >= maxServiceEventChannelNum {
|
||||
err := errors.New("The event channel in the service is full")
|
||||
err := errors.New("the event channel in the service is full")
|
||||
log.Error(err.Error())
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -14,9 +14,6 @@ type RegDiscoveryServiceEventFunType func(serviceName string)
|
||||
var RegRpcEventFun RegRpcEventFunType
|
||||
var UnRegRpcEventFun RegRpcEventFunType
|
||||
|
||||
var RegDiscoveryServiceEventFun RegDiscoveryServiceEventFunType
|
||||
var UnRegDiscoveryServiceEventFun RegDiscoveryServiceEventFunType
|
||||
|
||||
func init(){
|
||||
mapServiceName = map[string]IService{}
|
||||
setupServiceList = []IService{}
|
||||
@@ -26,7 +23,7 @@ func Init() {
|
||||
for _,s := range setupServiceList {
|
||||
err := s.OnInit()
|
||||
if err != nil {
|
||||
log.SError("Failed to initialize "+s.GetName()+" service:"+err.Error())
|
||||
log.Error("Failed to initialize "+s.GetName()+" service",log.ErrorAttr("err",err))
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
203
sysmodule/frametimer/FrameGroup.go
Normal file
203
sysmodule/frametimer/FrameGroup.go
Normal file
@@ -0,0 +1,203 @@
|
||||
package frametimer
|
||||
|
||||
import (
|
||||
"container/heap"
|
||||
"context"
|
||||
"errors"
|
||||
"github.com/duanhf2012/origin/v2/event"
|
||||
"github.com/duanhf2012/origin/v2/log"
|
||||
"time"
|
||||
)
|
||||
|
||||
type TimerCB func(context.Context, FrameTimerID)
|
||||
|
||||
type _timerHeap struct {
|
||||
timers []*timerData
|
||||
}
|
||||
|
||||
func (h *_timerHeap) Len() int {
|
||||
return len(h.timers)
|
||||
}
|
||||
|
||||
func (h *_timerHeap) Less(i, j int) bool {
|
||||
return h.timers[i].frameNum < h.timers[j].frameNum
|
||||
}
|
||||
|
||||
func (h *_timerHeap) Swap(i, j int) {
|
||||
h.timers[i], h.timers[j] = h.timers[j], h.timers[i]
|
||||
h.timers[i].idx = i
|
||||
h.timers[j].idx = j
|
||||
}
|
||||
|
||||
func (h *_timerHeap) Push(x interface{}) {
|
||||
td := x.(*timerData)
|
||||
h.timers = append(h.timers, td)
|
||||
td.idx = len(h.timers) - 1
|
||||
}
|
||||
|
||||
func (h *_timerHeap) Pop() (ret interface{}) {
|
||||
l := len(h.timers)
|
||||
h.timers, ret = h.timers[:l-1], h.timers[l-1]
|
||||
return
|
||||
}
|
||||
|
||||
type FrameGroup struct {
|
||||
groupID FrameGroupID
|
||||
timerHeap _timerHeap
|
||||
ft *FrameTimer
|
||||
|
||||
preTickGlobalFrameNum FrameNumber // 上次tick全局帧
|
||||
preGlobalFrameNum FrameNumber // 上次设置的全局帧,用于更新FrameTimer.mapFrameGroup关系
|
||||
frameNum FrameNumber // 当前帧
|
||||
|
||||
pause bool // 暂停状态
|
||||
multiple uint8 // 位数,默认1倍,只允许1-5倍数
|
||||
}
|
||||
|
||||
func (fg *FrameGroup) init() {
|
||||
fg.timerHeap.timers = make([]*timerData, 0, 512)
|
||||
fg.groupID = fg.ft.genGroupID()
|
||||
fg.multiple = 1
|
||||
heap.Init(&fg.timerHeap)
|
||||
}
|
||||
|
||||
func (fg *FrameGroup) convertGlobalFrameNum(frameNum FrameNumber) FrameNumber {
|
||||
return fg.ft.getGlobalFrameNumber() + (frameNum-fg.frameNum)/FrameNumber(fg.multiple)
|
||||
}
|
||||
|
||||
func (fg *FrameGroup) refreshMinFrame() {
|
||||
if fg.timerHeap.Len() == 0 || fg.pause {
|
||||
return
|
||||
}
|
||||
|
||||
globalFrameNum := fg.convertGlobalFrameNum(fg.timerHeap.timers[0].frameNum)
|
||||
fg.ft.refreshGroupMinFrame(fg.groupID, fg.preGlobalFrameNum, globalFrameNum)
|
||||
fg.preGlobalFrameNum = globalFrameNum
|
||||
}
|
||||
|
||||
func (fg *FrameGroup) tick(globalFrame FrameNumber) {
|
||||
fg.frameNum = fg.frameNum + (globalFrame-fg.preTickGlobalFrameNum)*FrameNumber(fg.multiple)
|
||||
fg.preTickGlobalFrameNum = globalFrame
|
||||
|
||||
fg.onceTick()
|
||||
|
||||
fg.refreshMinFrame()
|
||||
}
|
||||
|
||||
func (fg *FrameGroup) onceTick() {
|
||||
for fg.timerHeap.Len() > 0 {
|
||||
if fg.timerHeap.timers[0].frameNum > fg.frameNum {
|
||||
break
|
||||
}
|
||||
|
||||
t := heap.Pop(&fg.timerHeap).(*timerData)
|
||||
|
||||
ev := event.NewEvent()
|
||||
ev.Type = event.Sys_Event_FrameTick
|
||||
ev.Data = t
|
||||
fg.ft.NotifyEvent(ev)
|
||||
fg.ft.removeTimerData(t.timerID)
|
||||
|
||||
if t.tickerFrameNum != 0 {
|
||||
fg.addTicker(t.timerID, t.tickerFrameNum, t.ctx, t.cb)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (fg *FrameGroup) addTimer(timerID FrameTimerID, frame FrameNumber, ctx context.Context, cb TimerCB) {
|
||||
nextFrame := fg.frameNum + frame
|
||||
|
||||
td := fg.ft.addTimerData(timerID, nextFrame, 0, ctx, cb)
|
||||
heap.Push(&fg.timerHeap, td)
|
||||
}
|
||||
|
||||
func (fg *FrameGroup) addTicker(timerID FrameTimerID, frame FrameNumber, ctx context.Context, cb TimerCB) {
|
||||
nextFrame := fg.frameNum + frame
|
||||
|
||||
td := fg.ft.addTimerData(timerID, nextFrame, frame, ctx, cb)
|
||||
heap.Push(&fg.timerHeap, td)
|
||||
}
|
||||
|
||||
// SetMultiple 设置倍数,允许倍数范围1-5
|
||||
func (fg *FrameGroup) SetMultiple(multiple uint8) error {
|
||||
if fg.multiple == multiple {
|
||||
return nil
|
||||
}
|
||||
|
||||
if multiple < 0 || multiple > maxMultiple {
|
||||
return errors.New("invalid multiplier")
|
||||
}
|
||||
|
||||
fg.multiple = multiple
|
||||
|
||||
fg.refreshMinFrame()
|
||||
return nil
|
||||
}
|
||||
|
||||
// FrameAfterFunc 创建After定时器
|
||||
func (fg *FrameGroup) FrameAfterFunc(timerID *FrameTimerID, d time.Duration, ctx context.Context, cb TimerCB) {
|
||||
fg.ft.locker.Lock()
|
||||
defer fg.ft.locker.Unlock()
|
||||
|
||||
frame := FrameNumber(d / fg.ft.oneFrameTime)
|
||||
newTimerID := fg.ft.genTimerID()
|
||||
|
||||
fg.addTimer(newTimerID, frame, ctx, cb)
|
||||
*timerID = newTimerID
|
||||
fg.refreshMinFrame()
|
||||
}
|
||||
|
||||
// FrameNewTicker 创建Ticker定时器
|
||||
func (fg *FrameGroup) FrameNewTicker(timerID *FrameTimerID, d time.Duration, ctx context.Context, cb TimerCB) {
|
||||
fg.ft.locker.Lock()
|
||||
defer fg.ft.locker.Unlock()
|
||||
|
||||
frame := FrameNumber(d / fg.ft.oneFrameTime)
|
||||
newTimerID := fg.ft.genTimerID()
|
||||
|
||||
fg.addTicker(newTimerID, frame, ctx, cb)
|
||||
*timerID = newTimerID
|
||||
fg.refreshMinFrame()
|
||||
}
|
||||
|
||||
// Pause 暂停定时器组
|
||||
func (fg *FrameGroup) Pause() {
|
||||
fg.ft.locker.Lock()
|
||||
defer fg.ft.locker.Unlock()
|
||||
|
||||
fg.pause = true
|
||||
fg.ft.removeGroup(fg.groupID, fg.preGlobalFrameNum)
|
||||
fg.preGlobalFrameNum = 0
|
||||
}
|
||||
|
||||
// Resume 唤醒定时器组
|
||||
func (fg *FrameGroup) Resume() {
|
||||
fg.ft.locker.Lock()
|
||||
defer fg.ft.locker.Unlock()
|
||||
|
||||
fg.pause = false
|
||||
fg.refreshMinFrame()
|
||||
fg.preTickGlobalFrameNum = fg.ft.globalFrameNum
|
||||
}
|
||||
|
||||
// CancelTimer 关闭定时器
|
||||
func (fg *FrameGroup) CancelTimer(timerID FrameTimerID) {
|
||||
fg.ft.locker.Lock()
|
||||
defer fg.ft.locker.Unlock()
|
||||
|
||||
td := fg.ft.getTimerData(timerID)
|
||||
if td == nil {
|
||||
log.Error("cannot find timer", log.Uint64("timerID", uint64(timerID)))
|
||||
return
|
||||
}
|
||||
|
||||
heap.Remove(&fg.timerHeap, td.idx)
|
||||
fg.ft.removeGroup(fg.groupID, fg.preGlobalFrameNum)
|
||||
fg.preGlobalFrameNum = 0
|
||||
fg.refreshMinFrame()
|
||||
}
|
||||
|
||||
func (fg *FrameGroup) Close(){
|
||||
fg.ft.removeGroup(fg.groupID, fg.preGlobalFrameNum)
|
||||
delete(fg.ft.mapGroup,fg.groupID)
|
||||
}
|
||||
199
sysmodule/frametimer/FrameTimerModule.go
Normal file
199
sysmodule/frametimer/FrameTimerModule.go
Normal file
@@ -0,0 +1,199 @@
|
||||
package frametimer
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/duanhf2012/origin/v2/event"
|
||||
"github.com/duanhf2012/origin/v2/log"
|
||||
"github.com/duanhf2012/origin/v2/service"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
const defaultFps = 50
|
||||
const defaultSleepInterval = time.Millisecond * 3
|
||||
const maxFps = 1000
|
||||
const maxMultiple = 5
|
||||
|
||||
type FrameGroupID uint64
|
||||
type FrameTimerID uint64
|
||||
type FrameNumber uint64
|
||||
|
||||
type timerData struct {
|
||||
frameNum FrameNumber
|
||||
timerID FrameTimerID
|
||||
idx int
|
||||
cb TimerCB
|
||||
tickerFrameNum FrameNumber
|
||||
ctx context.Context
|
||||
}
|
||||
|
||||
type FrameTimer struct {
|
||||
service.Module
|
||||
fps uint32
|
||||
oneFrameTime time.Duration
|
||||
ticker *time.Ticker
|
||||
|
||||
mapFrameGroup map[FrameNumber]map[FrameGroupID]struct{}
|
||||
mapGroup map[FrameGroupID]*FrameGroup
|
||||
globalFrameNum FrameNumber // 当前帧
|
||||
|
||||
maxTimerID FrameTimerID
|
||||
maxGroupID FrameGroupID
|
||||
|
||||
mapTimer map[FrameTimerID]*timerData
|
||||
timerDataPool *sync.Pool
|
||||
|
||||
locker sync.Mutex
|
||||
sleepInterval time.Duration
|
||||
}
|
||||
|
||||
func (ft *FrameTimer) getTimerData(timerID FrameTimerID) *timerData {
|
||||
return ft.mapTimer[timerID]
|
||||
}
|
||||
|
||||
func (ft *FrameTimer) addTimerData(timerID FrameTimerID, frameNum FrameNumber, tickerFrameNum FrameNumber, ctx context.Context, cb TimerCB) *timerData {
|
||||
td := ft.timerDataPool.Get().(*timerData)
|
||||
td.timerID = timerID
|
||||
td.frameNum = frameNum
|
||||
td.cb = cb
|
||||
td.idx = -1
|
||||
td.tickerFrameNum = tickerFrameNum
|
||||
td.ctx = ctx
|
||||
ft.mapTimer[timerID] = td
|
||||
return td
|
||||
}
|
||||
|
||||
func (ft *FrameTimer) removeTimerData(timerID FrameTimerID) {
|
||||
td := ft.mapTimer[timerID]
|
||||
if td == nil {
|
||||
return
|
||||
}
|
||||
|
||||
ft.timerDataPool.Put(td)
|
||||
}
|
||||
|
||||
func (ft *FrameTimer) genGroupID() FrameGroupID {
|
||||
ft.maxGroupID++
|
||||
return ft.maxGroupID
|
||||
}
|
||||
|
||||
func (ft *FrameTimer) genTimerID() FrameTimerID {
|
||||
ft.maxTimerID++
|
||||
return ft.maxTimerID
|
||||
}
|
||||
|
||||
func (ft *FrameTimer) getGlobalFrameNumber() FrameNumber {
|
||||
return ft.globalFrameNum
|
||||
}
|
||||
|
||||
func (ft *FrameTimer) frameTick() {
|
||||
preFrameNum := ft.globalFrameNum
|
||||
ft.globalFrameNum++
|
||||
|
||||
ft.locker.Lock()
|
||||
defer ft.locker.Unlock()
|
||||
for i := preFrameNum; i <= ft.globalFrameNum; i++ {
|
||||
mapGroup := ft.mapFrameGroup[i]
|
||||
delete(ft.mapFrameGroup, i)
|
||||
for groupID := range mapGroup {
|
||||
group := ft.mapGroup[groupID]
|
||||
if group == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
group.tick(ft.globalFrameNum)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (ft *FrameTimer) removeGroup(groupID FrameGroupID, frameNum FrameNumber) {
|
||||
delete(ft.mapFrameGroup[frameNum], groupID)
|
||||
}
|
||||
|
||||
func (ft *FrameTimer) refreshGroupMinFrame(groupID FrameGroupID, preFrameNum FrameNumber, newFrameNum FrameNumber) {
|
||||
ft.removeGroup(groupID, preFrameNum)
|
||||
|
||||
mapGroup := ft.mapFrameGroup[newFrameNum]
|
||||
if mapGroup == nil {
|
||||
mapGroup = make(map[FrameGroupID]struct{}, 6)
|
||||
ft.mapFrameGroup[newFrameNum] = mapGroup
|
||||
}
|
||||
|
||||
mapGroup[groupID] = struct{}{}
|
||||
}
|
||||
|
||||
func (ft *FrameTimer) OnInit() error {
|
||||
ft.mapFrameGroup = make(map[FrameNumber]map[FrameGroupID]struct{}, 1024)
|
||||
ft.mapGroup = make(map[FrameGroupID]*FrameGroup, 1024)
|
||||
ft.mapTimer = make(map[FrameTimerID]*timerData, 2048)
|
||||
ft.timerDataPool = &sync.Pool{
|
||||
New: func() any {
|
||||
return &timerData{}
|
||||
},
|
||||
}
|
||||
|
||||
if ft.fps == 0 {
|
||||
ft.fps = defaultFps
|
||||
}
|
||||
|
||||
ft.GetEventProcessor().RegEventReceiverFunc(event.Sys_Event_FrameTick, ft.GetEventHandler(), func(e event.IEvent) {
|
||||
ev := e.(*event.Event)
|
||||
td, ok := ev.Data.(*timerData)
|
||||
if !ok {
|
||||
log.Error("convert *timerData error")
|
||||
return
|
||||
}
|
||||
td.cb(td.ctx, td.timerID)
|
||||
event.DeleteEvent(e)
|
||||
})
|
||||
|
||||
ft.oneFrameTime = time.Second / time.Duration(ft.fps)
|
||||
ft.ticker = time.NewTicker(ft.oneFrameTime)
|
||||
|
||||
if ft.sleepInterval == 0 {
|
||||
ft.sleepInterval = defaultSleepInterval
|
||||
}
|
||||
|
||||
go func() {
|
||||
preTime := time.Now()
|
||||
var preFrame FrameNumber
|
||||
|
||||
for {
|
||||
time.Sleep(ft.sleepInterval)
|
||||
frameMax := FrameNumber(time.Now().Sub(preTime) / ft.oneFrameTime)
|
||||
for i := preFrame + 1; i <= frameMax; i++ {
|
||||
ft.frameTick()
|
||||
}
|
||||
|
||||
preFrame = frameMax
|
||||
}
|
||||
}()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetFps 设置帧率,越大误差越低。如果有倍数加速需求,可以适当加大fps,以减少误差。默认50fps
|
||||
func (ft *FrameTimer) SetFps(fps uint32) {
|
||||
if fps > maxFps {
|
||||
fps = maxFps
|
||||
}
|
||||
|
||||
ft.fps = fps
|
||||
}
|
||||
|
||||
// SetAccuracyInterval 设置时间间隔精度,在循环中sleep该时间进行判断。实际上因为sleep有误差,所以暂时不使用fps得出。默认为3ms
|
||||
func (ft *FrameTimer) SetAccuracyInterval(interval time.Duration) {
|
||||
ft.sleepInterval = interval
|
||||
}
|
||||
|
||||
// NewGroup 创建定时器组
|
||||
func (ft *FrameTimer) NewGroup() *FrameGroup {
|
||||
var group FrameGroup
|
||||
group.ft = ft
|
||||
group.init()
|
||||
|
||||
ft.locker.Lock()
|
||||
defer ft.locker.Unlock()
|
||||
ft.mapGroup[group.groupID] = &group
|
||||
return &group
|
||||
}
|
||||
@@ -1,189 +0,0 @@
|
||||
package ginmodule
|
||||
|
||||
import (
|
||||
"context"
|
||||
"datacenter/common/processor"
|
||||
"github.com/duanhf2012/origin/v2/event"
|
||||
"github.com/duanhf2012/origin/v2/log"
|
||||
"github.com/duanhf2012/origin/v2/service"
|
||||
"github.com/gin-gonic/gin"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type GinModule struct {
|
||||
service.Module
|
||||
|
||||
*GinConf
|
||||
*gin.Engine
|
||||
srv *http.Server
|
||||
|
||||
processor []processor.IGinProcessor
|
||||
}
|
||||
|
||||
type GinConf struct {
|
||||
Addr string
|
||||
}
|
||||
|
||||
const Sys_Event_Gin_Event event.EventType = -11
|
||||
|
||||
func (gm *GinModule) Init(conf *GinConf, engine *gin.Engine) {
|
||||
gm.GinConf = conf
|
||||
gm.Engine = engine
|
||||
}
|
||||
|
||||
func (gm *GinModule) SetupDataProcessor(processor ...processor.IGinProcessor) {
|
||||
gm.processor = processor
|
||||
}
|
||||
|
||||
func (gm *GinModule) AppendDataProcessor(processor ...processor.IGinProcessor) {
|
||||
gm.processor = append(gm.processor, processor...)
|
||||
}
|
||||
|
||||
func (gm *GinModule) OnInit() error {
|
||||
if gm.Engine == nil {
|
||||
gm.Engine = gin.Default()
|
||||
}
|
||||
|
||||
gm.srv = &http.Server{
|
||||
Addr: gm.Addr,
|
||||
Handler: gm.Engine,
|
||||
}
|
||||
|
||||
gm.Engine.Use(Logger())
|
||||
gm.Engine.Use(gin.Recovery())
|
||||
gm.GetEventProcessor().RegEventReceiverFunc(Sys_Event_Gin_Event, gm.GetEventHandler(), gm.eventHandler)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (gm *GinModule) eventHandler(ev event.IEvent) {
|
||||
ginEvent := ev.(*GinEvent)
|
||||
for _, handler := range ginEvent.handlersChain {
|
||||
handler(ginEvent.c)
|
||||
}
|
||||
|
||||
ginEvent.chanWait <- struct{}{}
|
||||
}
|
||||
|
||||
func (gm *GinModule) Start() {
|
||||
log.Info("http start listen", slog.Any("addr", gm.Addr))
|
||||
go func() {
|
||||
err := gm.srv.ListenAndServe()
|
||||
if err != nil {
|
||||
log.Error("ListenAndServe error", slog.Any("error", err.Error()))
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func (gm *GinModule) StartTLS(certFile, keyFile string) {
|
||||
log.Info("http start listen", slog.Any("addr", gm.Addr))
|
||||
go func() {
|
||||
err := gm.srv.ListenAndServeTLS(certFile, keyFile)
|
||||
if err != nil {
|
||||
log.Fatal("ListenAndServeTLS error", slog.Any("error", err.Error()))
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func (gm *GinModule) Stop(ctx context.Context) {
|
||||
if err := gm.srv.Shutdown(ctx); err != nil {
|
||||
log.SError("Server Shutdown", slog.Any("error", err))
|
||||
}
|
||||
}
|
||||
|
||||
type GinEvent struct {
|
||||
handlersChain gin.HandlersChain
|
||||
chanWait chan struct{}
|
||||
c *gin.Context
|
||||
}
|
||||
|
||||
func (ge *GinEvent) GetEventType() event.EventType {
|
||||
return Sys_Event_Gin_Event
|
||||
}
|
||||
|
||||
func (gm *GinModule) handleMethod(httpMethod, relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {
|
||||
return gm.Engine.Handle(httpMethod, relativePath, func(c *gin.Context) {
|
||||
for _, p := range gm.processor {
|
||||
_, err := p.Process(c)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
var ev GinEvent
|
||||
chanWait := make(chan struct{})
|
||||
ev.chanWait = chanWait
|
||||
ev.handlersChain = handlers
|
||||
ev.c = c
|
||||
gm.NotifyEvent(&ev)
|
||||
|
||||
<-chanWait
|
||||
})
|
||||
}
|
||||
|
||||
func (gm *GinModule) SafeGET(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {
|
||||
return gm.handleMethod(http.MethodGet, relativePath, handlers...)
|
||||
}
|
||||
|
||||
func (gm *GinModule) SafePOST(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {
|
||||
return gm.handleMethod(http.MethodPost, relativePath, handlers...)
|
||||
}
|
||||
|
||||
func (gm *GinModule) SafeDELETE(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {
|
||||
return gm.handleMethod(http.MethodDelete, relativePath, handlers...)
|
||||
}
|
||||
|
||||
func (gm *GinModule) SafePATCH(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {
|
||||
return gm.handleMethod(http.MethodPatch, relativePath, handlers...)
|
||||
}
|
||||
|
||||
func (gm *GinModule) SafePut(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {
|
||||
return gm.handleMethod(http.MethodPut, relativePath, handlers...)
|
||||
}
|
||||
|
||||
func GetIPWithProxyHeaders(c *gin.Context) string {
|
||||
// 尝试从 X-Real-IP 头部获取真实 IP
|
||||
ip := c.GetHeader("X-Real-IP")
|
||||
|
||||
// 如果 X-Real-IP 头部不存在,则尝试从 X-Forwarded-For 头部获取
|
||||
if ip == "" {
|
||||
ip = c.GetHeader("X-Forwarded-For")
|
||||
}
|
||||
|
||||
// 如果两者都不存在,则使用默认的 ClientIP 方法获取 IP
|
||||
if ip == "" {
|
||||
ip = c.ClientIP()
|
||||
}
|
||||
|
||||
return ip
|
||||
}
|
||||
|
||||
func GetIPWithValidatedProxyHeaders(c *gin.Context) string {
|
||||
// 获取代理头部
|
||||
proxyHeaders := c.Request.Header.Get("X-Real-IP,X-Forwarded-For")
|
||||
|
||||
// 分割代理头部,取第一个 IP 作为真实 IP
|
||||
ips := strings.Split(proxyHeaders, ",")
|
||||
ip := strings.TrimSpace(ips[0])
|
||||
|
||||
// 如果 IP 格式合法,则使用获取到的 IP,否则使用默认的 ClientIP 方法获取
|
||||
if isValidIP(ip) {
|
||||
return ip
|
||||
} else {
|
||||
ip = c.ClientIP()
|
||||
return ip
|
||||
}
|
||||
}
|
||||
|
||||
// isValidIP 判断 IP 格式是否合法
|
||||
func isValidIP(ip string) bool {
|
||||
// 此处添加自定义的 IP 格式验证逻辑
|
||||
// 例如,使用正则表达式验证 IP 格式
|
||||
// ...
|
||||
if ip == "" {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
@@ -39,7 +39,7 @@ func (slf *SyncHttpResponse) Get(timeoutMs int) HttpResponse {
|
||||
return rsp
|
||||
}
|
||||
return HttpResponse{
|
||||
Err: fmt.Errorf("Getting the return result timeout [%d]ms", timeoutMs),
|
||||
Err: fmt.Errorf("getting the return result timeout [%d]ms", timeoutMs),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,7 +51,7 @@ func (m *HttpClientModule) InitHttpClient(transport http.RoundTripper,timeout ti
|
||||
}
|
||||
}
|
||||
|
||||
func (m *HttpClientModule) Init(proxyUrl string, maxpool int, dialTimeout time.Duration,dialKeepAlive time.Duration,idleConnTimeout time.Duration,timeout time.Duration) {
|
||||
func (m *HttpClientModule) Init(proxyUrl string, maxPool int, dialTimeout time.Duration, dialKeepAlive time.Duration, idleConnTimeout time.Duration, timeout time.Duration) {
|
||||
type ProxyFun func(_ *http.Request) (*url.URL, error)
|
||||
var proxyFun ProxyFun
|
||||
if proxyUrl != "" {
|
||||
@@ -66,8 +66,8 @@ func (m *HttpClientModule) Init(proxyUrl string, maxpool int, dialTimeout time.D
|
||||
Timeout: dialTimeout,
|
||||
KeepAlive: dialKeepAlive,
|
||||
}).DialContext,
|
||||
MaxIdleConns: maxpool,
|
||||
MaxIdleConnsPerHost: maxpool,
|
||||
MaxIdleConns: maxPool,
|
||||
MaxIdleConnsPerHost: maxPool,
|
||||
IdleConnTimeout: idleConnTimeout,
|
||||
Proxy: proxyFun,
|
||||
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
||||
|
||||
65
sysmodule/kafkamodule/Admin.go
Normal file
65
sysmodule/kafkamodule/Admin.go
Normal file
@@ -0,0 +1,65 @@
|
||||
package kafkamodule
|
||||
|
||||
import "github.com/IBM/sarama"
|
||||
|
||||
type KafkaAdmin struct {
|
||||
sarama.ClusterAdmin
|
||||
|
||||
mapTopic map[string]sarama.TopicDetail
|
||||
}
|
||||
|
||||
func (ka *KafkaAdmin) Setup(kafkaVersion string, addrs []string) error {
|
||||
config := sarama.NewConfig()
|
||||
var err error
|
||||
config.Version, err = sarama.ParseKafkaVersion(kafkaVersion)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
ka.ClusterAdmin, err = sarama.NewClusterAdmin(addrs, config)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
ka.mapTopic, err = ka.GetTopics()
|
||||
if err != nil {
|
||||
ka.ClusterAdmin.Close()
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ka *KafkaAdmin) RefreshTopic() error {
|
||||
var err error
|
||||
ka.mapTopic, err = ka.GetTopics()
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (ka *KafkaAdmin) HasTopic(topic string) bool {
|
||||
_, ok := ka.mapTopic[topic]
|
||||
|
||||
return ok
|
||||
}
|
||||
|
||||
func (ka *KafkaAdmin) GetTopicDetail(topic string) *sarama.TopicDetail {
|
||||
topicDetail, ok := ka.mapTopic[topic]
|
||||
if ok == false {
|
||||
return nil
|
||||
}
|
||||
|
||||
return &topicDetail
|
||||
}
|
||||
|
||||
func (ka *KafkaAdmin) GetTopics() (map[string]sarama.TopicDetail, error) {
|
||||
return ka.ListTopics()
|
||||
}
|
||||
|
||||
// CreateTopic 创建主题
|
||||
// numPartitions分区数
|
||||
// replicationFactor副本数
|
||||
// validateOnly参数执行操作时只进行参数验证而不实际执行操作
|
||||
func (ka *KafkaAdmin) CreateTopic(topic string, numPartitions int32, replicationFactor int16, validateOnly bool) error {
|
||||
return ka.ClusterAdmin.CreateTopic(topic, &sarama.TopicDetail{NumPartitions: numPartitions, ReplicationFactor: replicationFactor}, validateOnly)
|
||||
}
|
||||
289
sysmodule/kafkamodule/Consumer.go
Normal file
289
sysmodule/kafkamodule/Consumer.go
Normal file
@@ -0,0 +1,289 @@
|
||||
package kafkamodule
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/IBM/sarama"
|
||||
"github.com/duanhf2012/origin/v2/log"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
type ConsumerGroup struct {
|
||||
sarama.ConsumerGroup
|
||||
waitGroup sync.WaitGroup
|
||||
|
||||
chanExit chan error
|
||||
ready chan bool
|
||||
cancel context.CancelFunc
|
||||
groupId string
|
||||
}
|
||||
|
||||
func NewConsumerConfig(kafkaVersion string, assignor string, offsetsInitial int64) (*sarama.Config, error) {
|
||||
var err error
|
||||
|
||||
config := sarama.NewConfig()
|
||||
config.Version, err = sarama.ParseKafkaVersion(kafkaVersion)
|
||||
config.Consumer.Offsets.Initial = offsetsInitial
|
||||
config.Consumer.Offsets.AutoCommit.Enable = false
|
||||
|
||||
switch assignor {
|
||||
case "sticky":
|
||||
// 黏性roundRobin,rebalance之后首先保证前面的分配,从后面剥离
|
||||
// topic:T0{P0,P1,P2,P3,P4,P5},消费者:C1,C2
|
||||
// ---------------before rebalance:即roundRobin
|
||||
// C1: T0{P0} T0{P2} T0{P4}
|
||||
// C2: T0{P1} T0{P3} T0{P5}
|
||||
// ----------------after rebalance:增加了一个消费者
|
||||
// C1: T0{P0} T0{P2}
|
||||
// C2: T0{P1} T0{P3}
|
||||
// C3: T0{P4} T0{P5} until每个消费者的分区数误差不超过1
|
||||
config.Consumer.Group.Rebalance.GroupStrategies = []sarama.BalanceStrategy{sarama.NewBalanceStrategySticky()}
|
||||
case "roundrobin":
|
||||
// roundRobin --逐个平均分发
|
||||
// topic: T0{P0,P1,P2},T1{P0,P1,P2,P3}两个消费者C1,C2
|
||||
// C1: T0{P0} T0{P2} T1{P1} T1{P3}
|
||||
// C2: T0{P1} T1{P0} T1{P2}
|
||||
config.Consumer.Group.Rebalance.GroupStrategies = []sarama.BalanceStrategy{sarama.NewBalanceStrategyRoundRobin()}
|
||||
case "range":
|
||||
// 默认值 --一次平均分发
|
||||
// topic: T0{P0,P1,P2,P3},T1{P0,P1,P2,P3},两个消费者C1,C2
|
||||
// T1分区总数6 / 消费者数2 = 3 ,即该会话的分区每个消费者分3个
|
||||
// T2分区总数4 / 消费者数2 = 2 ,即该会话的分区每个消费者分2个
|
||||
// C1: T0{P0, P1, P2} T1{P0, P1}
|
||||
// C2: T0{P3, P4, P5} T1{P2, P3}
|
||||
config.Consumer.Group.Rebalance.GroupStrategies = []sarama.BalanceStrategy{sarama.NewBalanceStrategyRange()}
|
||||
default:
|
||||
return nil, fmt.Errorf("Unrecognized consumer group partition assignor: %s", assignor)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return config, nil
|
||||
}
|
||||
|
||||
type IMsgReceiver interface {
|
||||
Receiver(msgs []*sarama.ConsumerMessage) bool
|
||||
}
|
||||
|
||||
func (c *ConsumerGroup) Setup(addr []string, topics []string, groupId string, config *sarama.Config, receiverInterval time.Duration, maxReceiverNum int, msgReceiver IMsgReceiver) error {
|
||||
var err error
|
||||
c.ConsumerGroup, err = sarama.NewConsumerGroup(addr, groupId, config)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
c.groupId = groupId
|
||||
c.chanExit = make(chan error, 1)
|
||||
|
||||
var handler ConsumerGroupHandler
|
||||
handler.receiver = msgReceiver
|
||||
handler.maxReceiverNum = maxReceiverNum
|
||||
handler.receiverInterval = receiverInterval
|
||||
handler.chanExit = c.chanExit
|
||||
|
||||
var ctx context.Context
|
||||
ctx, c.cancel = context.WithCancel(context.Background())
|
||||
|
||||
c.waitGroup.Add(1)
|
||||
go func() {
|
||||
defer c.waitGroup.Done()
|
||||
|
||||
for {
|
||||
if err = c.Consume(ctx, topics, &handler); err != nil {
|
||||
// 当setup失败的时候,error会返回到这里
|
||||
log.Error("Error from consumer", log.Any("err", err))
|
||||
return
|
||||
}
|
||||
|
||||
// check if context was cancelled, signaling that the consumer should stop
|
||||
if ctx.Err() != nil {
|
||||
log.Info("consumer stop", log.Any("info", ctx.Err()))
|
||||
}
|
||||
|
||||
c.chanExit <- err
|
||||
}
|
||||
}()
|
||||
|
||||
err = <-c.chanExit
|
||||
//已经准备好了
|
||||
return err
|
||||
}
|
||||
|
||||
func (c *ConsumerGroup) Close() {
|
||||
log.Info("close consumerGroup")
|
||||
//1.cancel掉
|
||||
c.cancel()
|
||||
|
||||
//2.关闭连接
|
||||
err := c.ConsumerGroup.Close()
|
||||
if err != nil {
|
||||
log.Error("close consumerGroup fail", log.Any("err", err.Error()))
|
||||
}
|
||||
|
||||
//3.等待退出
|
||||
c.waitGroup.Wait()
|
||||
}
|
||||
|
||||
type ConsumerGroupHandler struct {
|
||||
receiver IMsgReceiver
|
||||
|
||||
receiverInterval time.Duration
|
||||
maxReceiverNum int
|
||||
|
||||
//mapTopicOffset map[string]map[int32]int //map[topic]map[partitionId]offsetInfo
|
||||
mapTopicData map[string]*MsgData
|
||||
mx sync.Mutex
|
||||
|
||||
chanExit chan error
|
||||
isRebalance bool //是否为再平衡
|
||||
//stopSig *int32
|
||||
}
|
||||
|
||||
type MsgData struct {
|
||||
sync.Mutex
|
||||
msg []*sarama.ConsumerMessage
|
||||
|
||||
mapPartitionOffset map[int32]int64
|
||||
}
|
||||
|
||||
func (ch *ConsumerGroupHandler) Flush(session sarama.ConsumerGroupSession, topic string) {
|
||||
if topic != "" {
|
||||
msgData := ch.GetMsgData(topic)
|
||||
msgData.flush(session, ch.receiver, topic)
|
||||
return
|
||||
}
|
||||
|
||||
for tp, msgData := range ch.mapTopicData {
|
||||
msgData.flush(session, ch.receiver, tp)
|
||||
}
|
||||
}
|
||||
|
||||
func (ch *ConsumerGroupHandler) GetMsgData(topic string) *MsgData {
|
||||
ch.mx.Lock()
|
||||
defer ch.mx.Unlock()
|
||||
|
||||
msgData := ch.mapTopicData[topic]
|
||||
if msgData == nil {
|
||||
msgData = &MsgData{}
|
||||
msgData.msg = make([]*sarama.ConsumerMessage, 0, ch.maxReceiverNum)
|
||||
ch.mapTopicData[topic] = msgData
|
||||
}
|
||||
|
||||
return msgData
|
||||
}
|
||||
|
||||
func (md *MsgData) flush(session sarama.ConsumerGroupSession, receiver IMsgReceiver, topic string) {
|
||||
if len(md.msg) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
//发送给接收者
|
||||
for {
|
||||
ok := receiver.Receiver(md.msg)
|
||||
if ok == true {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
for pId, offset := range md.mapPartitionOffset {
|
||||
|
||||
session.MarkOffset(topic, pId, offset+1, "")
|
||||
log.Info(fmt.Sprintf("topic %s,pid %d,offset %d", topic, pId, offset+1))
|
||||
}
|
||||
session.Commit()
|
||||
//log.Info("commit")
|
||||
//time.Sleep(1000 * time.Second)
|
||||
//置空
|
||||
md.msg = md.msg[:0]
|
||||
clear(md.mapPartitionOffset)
|
||||
}
|
||||
|
||||
func (md *MsgData) appendMsg(session sarama.ConsumerGroupSession, msg *sarama.ConsumerMessage, receiver IMsgReceiver, maxReceiverNum int) {
|
||||
md.Lock()
|
||||
defer md.Unlock()
|
||||
|
||||
//收到的offset只会越来越大在
|
||||
if md.mapPartitionOffset == nil {
|
||||
md.mapPartitionOffset = make(map[int32]int64, 10)
|
||||
}
|
||||
|
||||
md.mapPartitionOffset[msg.Partition] = msg.Offset
|
||||
|
||||
md.msg = append(md.msg, msg)
|
||||
if len(md.msg) < maxReceiverNum {
|
||||
return
|
||||
}
|
||||
|
||||
md.flush(session, receiver, msg.Topic)
|
||||
}
|
||||
|
||||
func (ch *ConsumerGroupHandler) AppendMsg(session sarama.ConsumerGroupSession, msg *sarama.ConsumerMessage) {
|
||||
dataMsg := ch.GetMsgData(msg.Topic)
|
||||
dataMsg.appendMsg(session, msg, ch.receiver, ch.maxReceiverNum)
|
||||
}
|
||||
|
||||
func (ch *ConsumerGroupHandler) Setup(session sarama.ConsumerGroupSession) error {
|
||||
ch.mapTopicData = make(map[string]*MsgData, 128)
|
||||
|
||||
if ch.isRebalance == false {
|
||||
ch.chanExit <- nil
|
||||
}
|
||||
|
||||
ch.isRebalance = true
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ch *ConsumerGroupHandler) Cleanup(session sarama.ConsumerGroupSession) error {
|
||||
ch.Flush(session, "")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ch *ConsumerGroupHandler) ConsumeClaim(session sarama.ConsumerGroupSession, claim sarama.ConsumerGroupClaim) error {
|
||||
|
||||
ticker := time.NewTicker(ch.receiverInterval)
|
||||
|
||||
for {
|
||||
select {
|
||||
case msg := <-claim.Messages():
|
||||
if msg == nil {
|
||||
log.SWarning("claim will exit", log.Any("topic", claim.Topic()), log.Any("Partition", claim.Partition()))
|
||||
return nil
|
||||
}
|
||||
ch.AppendMsg(session, msg)
|
||||
case <-ticker.C:
|
||||
ch.Flush(session, claim.Topic())
|
||||
case <-session.Context().Done():
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
阿里云参数说明:https://sls.aliyun.com/doc/oscompatibledemo/sarama_go_kafka_consume.html
|
||||
conf.Net.TLS.Enable = true
|
||||
conf.Net.SASL.Enable = true
|
||||
conf.Net.SASL.User = project
|
||||
conf.Net.SASL.Password = fmt.Sprintf("%s#%s", accessId, accessKey)
|
||||
conf.Net.SASL.Mechanism = "PLAIN"
|
||||
|
||||
|
||||
|
||||
conf.Net.TLS.Enable = true
|
||||
conf.Net.SASL.Enable = true
|
||||
conf.Net.SASL.User = project
|
||||
conf.Net.SASL.Password = fmt.Sprintf("%s#%s", accessId, accessKey)
|
||||
conf.Net.SASL.Mechanism = "PLAIN"
|
||||
|
||||
conf.Consumer.Fetch.Min = 1
|
||||
conf.Consumer.Fetch.Default = 1024 * 1024
|
||||
conf.Consumer.Retry.Backoff = 2 * time.Second
|
||||
conf.Consumer.MaxWaitTime = 250 * time.Millisecond
|
||||
conf.Consumer.MaxProcessingTime = 100 * time.Millisecond
|
||||
conf.Consumer.Return.Errors = false
|
||||
conf.Consumer.Offsets.AutoCommit.Enable = true
|
||||
conf.Consumer.Offsets.AutoCommit.Interval = 1 * time.Second
|
||||
conf.Consumer.Offsets.Initial = sarama.OffsetOldest
|
||||
conf.Consumer.Offsets.Retry.Max = 3
|
||||
*/
|
||||
146
sysmodule/kafkamodule/Producer.go
Normal file
146
sysmodule/kafkamodule/Producer.go
Normal file
@@ -0,0 +1,146 @@
|
||||
package kafkamodule
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/IBM/sarama"
|
||||
"github.com/duanhf2012/origin/v2/log"
|
||||
"github.com/duanhf2012/origin/v2/service"
|
||||
"time"
|
||||
)
|
||||
|
||||
type IProducer interface {
|
||||
}
|
||||
|
||||
type SyncProducer struct {
|
||||
}
|
||||
|
||||
type AsyncProducer struct {
|
||||
}
|
||||
|
||||
type Producer struct {
|
||||
service.Module
|
||||
|
||||
sarama.SyncProducer
|
||||
|
||||
sarama.AsyncProducer
|
||||
}
|
||||
|
||||
// NewProducerConfig 新建producerConfig
|
||||
// kafkaVersion kafka版本
|
||||
// returnErr,returnSucc 是否返回错误与成功
|
||||
// requiredAcks -1 #全量同步确认,强可靠性保证(当所有的 leader 和 follower 都接收成功时)#WaitForAll 1 #leader 确认收到, 默认(仅 leader 反馈)#WaitForLocal 0 #不确认,但是吞吐量大(不 care 结果) #NoResponse
|
||||
// Idempotent(幂等) 确保信息都准确写入一份副本,用于幂等生产者,当这一项设置为true的时候,生产者将保证生产的消息一定是有序且精确一次的
|
||||
// partitioner 生成分区器,用于选择向哪个分区发送信息,默认情况下对消息密钥进行散列
|
||||
func NewProducerConfig(kafkaVersion string, returnErr bool, returnSucc bool, requiredAcks sarama.RequiredAcks, Idempotent bool,
|
||||
partitioner sarama.PartitionerConstructor) (*sarama.Config, error) {
|
||||
config := sarama.NewConfig()
|
||||
var err error
|
||||
config.Version, err = sarama.ParseKafkaVersion(kafkaVersion)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
config.Producer.Return.Errors = returnErr
|
||||
config.Producer.Return.Successes = returnSucc
|
||||
config.Producer.RequiredAcks = requiredAcks
|
||||
config.Producer.Partitioner = partitioner
|
||||
config.Producer.Timeout = 10 * time.Second
|
||||
|
||||
config.Producer.Idempotent = Idempotent
|
||||
if Idempotent == true {
|
||||
config.Net.MaxOpenRequests = 1
|
||||
}
|
||||
return config, nil
|
||||
}
|
||||
|
||||
func (p *Producer) SyncSetup(addr []string, config *sarama.Config) error {
|
||||
var err error
|
||||
p.SyncProducer, err = sarama.NewSyncProducer(addr, config)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *Producer) ASyncSetup(addr []string, config *sarama.Config) error {
|
||||
var err error
|
||||
p.AsyncProducer, err = sarama.NewAsyncProducer(addr, config)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
go func() {
|
||||
p.asyncRun()
|
||||
}()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *Producer) asyncRun() {
|
||||
for {
|
||||
select {
|
||||
case sm := <-p.Successes():
|
||||
if sm.Metadata == nil {
|
||||
break
|
||||
}
|
||||
asyncReturn := sm.Metadata.(*AsyncReturn)
|
||||
asyncReturn.chanReturn <- asyncReturn
|
||||
case em := <-p.Errors():
|
||||
log.Error("async kafkamodule error", log.ErrorAttr("err", em.Err))
|
||||
if em.Msg.Metadata == nil {
|
||||
break
|
||||
}
|
||||
asyncReturn := em.Msg.Metadata.(*AsyncReturn)
|
||||
asyncReturn.Err = em.Err
|
||||
asyncReturn.chanReturn <- asyncReturn
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type AsyncReturn struct {
|
||||
Msg *sarama.ProducerMessage
|
||||
Err error
|
||||
chanReturn chan *AsyncReturn
|
||||
}
|
||||
|
||||
func (ar *AsyncReturn) WaitOk(ctx context.Context) (*sarama.ProducerMessage, error) {
|
||||
asyncReturn := ar.Msg.Metadata.(*AsyncReturn)
|
||||
select {
|
||||
case <-asyncReturn.chanReturn:
|
||||
return asyncReturn.Msg, asyncReturn.Err
|
||||
case <-ctx.Done():
|
||||
return nil, ctx.Err()
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Producer) AsyncSendMessage(msg *sarama.ProducerMessage) *AsyncReturn {
|
||||
asyncReturn := AsyncReturn{Msg: msg, chanReturn: make(chan *AsyncReturn, 1)}
|
||||
msg.Metadata = &asyncReturn
|
||||
p.AsyncProducer.Input() <- msg
|
||||
|
||||
return &asyncReturn
|
||||
}
|
||||
|
||||
func (p *Producer) AsyncPushMessage(msg *sarama.ProducerMessage) {
|
||||
p.AsyncProducer.Input() <- msg
|
||||
}
|
||||
|
||||
func (p *Producer) Close() {
|
||||
if p.SyncProducer != nil {
|
||||
p.SyncProducer.Close()
|
||||
p.SyncProducer = nil
|
||||
}
|
||||
|
||||
if p.AsyncProducer != nil {
|
||||
p.AsyncProducer.Close()
|
||||
p.AsyncProducer = nil
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Producer) SendMessage(msg *sarama.ProducerMessage) (partition int32, offset int64, err error) {
|
||||
return p.SyncProducer.SendMessage(msg)
|
||||
}
|
||||
|
||||
func (p *Producer) SendMessages(msgs []*sarama.ProducerMessage) error {
|
||||
return p.SyncProducer.SendMessages(msgs)
|
||||
}
|
||||
151
sysmodule/kafkamodule/ProducerAndConsumer_test.go
Normal file
151
sysmodule/kafkamodule/ProducerAndConsumer_test.go
Normal file
@@ -0,0 +1,151 @@
|
||||
package kafkamodule
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/IBM/sarama"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// 对各参数和机制名称的说明:https://blog.csdn.net/u013311345/article/details/129217728
|
||||
type MsgReceiver struct {
|
||||
t *testing.T
|
||||
}
|
||||
|
||||
func (mr *MsgReceiver) Receiver(msgs []*sarama.ConsumerMessage) bool {
|
||||
for _, m := range msgs {
|
||||
mr.t.Logf("time:%s, topic:%s, partition:%d, offset:%d, key:%s, value:%s", time.Now().Format("2006-01-02 15:04:05.000"), m.Topic, m.Partition, m.Offset, m.Key, string(m.Value))
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
var addr = []string{"192.168.13.24:9092", "192.168.13.24:9093", "192.168.13.24:9094", "192.168.13.24:9095"}
|
||||
var topicName = []string{"test_topic_1", "test_topic_2"}
|
||||
var kafkaVersion = "3.3.1"
|
||||
|
||||
func producer(t *testing.T) {
|
||||
var admin KafkaAdmin
|
||||
err := admin.Setup(kafkaVersion, addr)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
for _, tName := range topicName {
|
||||
if admin.HasTopic(tName) == false {
|
||||
err = admin.CreateTopic(tName, 2, 2, false)
|
||||
t.Log(err)
|
||||
}
|
||||
}
|
||||
|
||||
var pd Producer
|
||||
cfg, err := NewProducerConfig(kafkaVersion, true, true, sarama.WaitForAll, false, sarama.NewHashPartitioner)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err = pd.SyncSetup(addr, cfg)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
now := time.Now()
|
||||
|
||||
//msgs := make([]*sarama.ProducerMessage, 0, 20000)
|
||||
for i := 0; i < 20000; i++ {
|
||||
var msg sarama.ProducerMessage
|
||||
msg.Key = sarama.StringEncoder(fmt.Sprintf("%d", i))
|
||||
msg.Topic = topicName[0]
|
||||
msg.Value = sarama.StringEncoder(fmt.Sprintf("i'm %d", i))
|
||||
pd.SendMessage(&msg)
|
||||
//msgs = append(msgs, &msg)
|
||||
}
|
||||
//err = pd.SendMessages(msgs)
|
||||
//t.Log(err)
|
||||
t.Log(time.Now().Sub(now).Milliseconds())
|
||||
pd.Close()
|
||||
}
|
||||
|
||||
func producer_async(t *testing.T) {
|
||||
var admin KafkaAdmin
|
||||
err := admin.Setup(kafkaVersion, addr)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
for _, tName := range topicName {
|
||||
if admin.HasTopic(tName) == false {
|
||||
err = admin.CreateTopic(tName, 10, 2, false)
|
||||
t.Log(err)
|
||||
}
|
||||
}
|
||||
|
||||
var pd Producer
|
||||
cfg, err := NewProducerConfig(kafkaVersion, true, true, sarama.WaitForAll, false, sarama.NewHashPartitioner)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err = pd.ASyncSetup(addr, cfg)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
now := time.Now()
|
||||
|
||||
msgs := make([]*AsyncReturn, 0, 20000)
|
||||
for i := 0; i < 200000; i++ {
|
||||
var msg sarama.ProducerMessage
|
||||
msg.Key = sarama.StringEncoder(fmt.Sprintf("%d", i))
|
||||
msg.Topic = topicName[0]
|
||||
msg.Value = sarama.StringEncoder(fmt.Sprintf("i'm %d", i))
|
||||
|
||||
r := pd.AsyncSendMessage(&msg)
|
||||
msgs = append(msgs, r)
|
||||
}
|
||||
//err = pd.SendMessages(msgs)
|
||||
//t.Log(err)
|
||||
|
||||
for _, r := range msgs {
|
||||
r.WaitOk(context.Background())
|
||||
//t.Log(m, e)
|
||||
}
|
||||
t.Log(time.Now().Sub(now).Milliseconds())
|
||||
|
||||
time.Sleep(1000 * time.Second)
|
||||
pd.Close()
|
||||
}
|
||||
|
||||
func consumer(t *testing.T) {
|
||||
var admin KafkaAdmin
|
||||
err := admin.Setup(kafkaVersion, addr)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
for _, tName := range topicName {
|
||||
if admin.HasTopic(tName) == false {
|
||||
err = admin.CreateTopic(tName, 10, 2, false)
|
||||
t.Log(err)
|
||||
}
|
||||
}
|
||||
|
||||
var cg ConsumerGroup
|
||||
cfg, err := NewConsumerConfig(kafkaVersion, "sticky", sarama.OffsetOldest)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err = cg.Setup(addr, topicName, "test_groupId", cfg, 50*time.Second, 10, &MsgReceiver{t: t})
|
||||
t.Log(err)
|
||||
time.Sleep(10000 * time.Second)
|
||||
cg.Close()
|
||||
}
|
||||
|
||||
func TestConsumerAndProducer(t *testing.T) {
|
||||
producer_async(t)
|
||||
//go producer(t)
|
||||
//producer(t)
|
||||
//consumer(t)
|
||||
}
|
||||
7
sysmodule/kafkamodule/Sasl.go
Normal file
7
sysmodule/kafkamodule/Sasl.go
Normal file
@@ -0,0 +1,7 @@
|
||||
package kafkamodule
|
||||
|
||||
type Sasl struct {
|
||||
UserName string `json:"UserName"`
|
||||
Passwd string `json:"Passwd"`
|
||||
InstanceId string `json:"InstanceId"`
|
||||
}
|
||||
@@ -5,7 +5,6 @@ import (
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
"go.mongodb.org/mongo-driver/x/bsonx"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -48,6 +47,10 @@ func (mm *MongoModule) Start() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (mm *MongoModule) Stop() error {
|
||||
return mm.client.Disconnect(context.Background())
|
||||
}
|
||||
|
||||
func (mm *MongoModule) TakeSession() Session {
|
||||
return Session{Client: mm.client, maxOperatorTimeOut: mm.maxOperatorTimeOut}
|
||||
}
|
||||
@@ -72,12 +75,12 @@ func (s *Session) NextSeq(db string, collection string, id interface{}) (int, er
|
||||
return res.Seq, err
|
||||
}
|
||||
|
||||
// indexKeys[索引][每个索引key字段]
|
||||
// EnsureIndex indexKeys[索引][每个索引key字段]
|
||||
func (s *Session) EnsureIndex(db string, collection string, indexKeys [][]string, bBackground bool, sparse bool, asc bool) error {
|
||||
return s.ensureIndex(db, collection, indexKeys, bBackground, false, sparse, asc)
|
||||
}
|
||||
|
||||
// indexKeys[索引][每个索引key字段]
|
||||
// EnsureUniqueIndex indexKeys[索引][每个索引key字段]
|
||||
func (s *Session) EnsureUniqueIndex(db string, collection string, indexKeys [][]string, bBackground bool, sparse bool, asc bool) error {
|
||||
return s.ensureIndex(db, collection, indexKeys, bBackground, true, sparse, asc)
|
||||
}
|
||||
@@ -86,12 +89,12 @@ func (s *Session) EnsureUniqueIndex(db string, collection string, indexKeys [][]
|
||||
func (s *Session) ensureIndex(db string, collection string, indexKeys [][]string, bBackground bool, unique bool, sparse bool, asc bool) error {
|
||||
var indexes []mongo.IndexModel
|
||||
for _, keys := range indexKeys {
|
||||
keysDoc := bsonx.Doc{}
|
||||
keysDoc := bson.D{}
|
||||
for _, key := range keys {
|
||||
if asc {
|
||||
keysDoc = keysDoc.Append(key, bsonx.Int32(1))
|
||||
keysDoc = append(keysDoc, bson.E{Key: key, Value: 1})
|
||||
} else {
|
||||
keysDoc = keysDoc.Append(key, bsonx.Int32(-1))
|
||||
keysDoc = append(keysDoc, bson.E{Key: key, Value: -1})
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -28,7 +28,6 @@ type PingExecute struct {
|
||||
pintExit chan bool
|
||||
}
|
||||
|
||||
// DBModule ...
|
||||
type MySQLModule struct {
|
||||
service.Module
|
||||
db *sql.DB
|
||||
@@ -53,7 +52,7 @@ type DBResult struct {
|
||||
RowsAffected int64
|
||||
|
||||
rowNum int
|
||||
RowInfo map[string][]interface{} //map[fieldname][row]sql.NullString
|
||||
RowInfo map[string][]interface{} //map[fieldName][row]sql.NullString
|
||||
}
|
||||
|
||||
type DataSetList struct {
|
||||
@@ -63,13 +62,11 @@ type DataSetList struct {
|
||||
blur bool
|
||||
}
|
||||
|
||||
|
||||
type dbControl interface {
|
||||
Exec(query string, args ...interface{}) (sql.Result, error)
|
||||
Query(query string, args ...interface{}) (*sql.Rows, error)
|
||||
}
|
||||
|
||||
|
||||
func (m *MySQLModule) Init(url string, userName string, password string, dbname string, maxConn int) error {
|
||||
m.url = url
|
||||
m.username = userName
|
||||
@@ -116,7 +113,7 @@ func (slf *Tx) Commit() error {
|
||||
return slf.tx.Commit()
|
||||
}
|
||||
|
||||
// QueryEx executes a query that return rows.
|
||||
// Query executes a query that return rows.
|
||||
func (slf *Tx) Query(strQuery string, args ...interface{}) (*DataSetList, error) {
|
||||
return query(slf.slowDuration, slf.tx, strQuery, args...)
|
||||
}
|
||||
@@ -225,12 +222,12 @@ func query(slowDuration time.Duration,db dbControl,strQuery string, args ...inte
|
||||
|
||||
if checkArgs(args) != nil {
|
||||
log.Error("CheckArgs is error :%s", strQuery)
|
||||
return &datasetList, fmt.Errorf("CheckArgs is error!")
|
||||
return &datasetList, fmt.Errorf("checkArgs is error")
|
||||
}
|
||||
|
||||
if db == nil {
|
||||
log.Error("cannot connect database:%s", strQuery)
|
||||
return &datasetList, fmt.Errorf("cannot connect database!")
|
||||
return &datasetList, fmt.Errorf("cannot connect database")
|
||||
}
|
||||
|
||||
TimeFuncStart := time.Now()
|
||||
@@ -256,7 +253,7 @@ func query(slowDuration time.Duration,db dbControl,strQuery string, args ...inte
|
||||
if dbResult.RowInfo == nil {
|
||||
dbResult.RowInfo = make(map[string][]interface{})
|
||||
}
|
||||
//RowInfo map[string][][]sql.NullString //map[fieldname][row][column]sql.NullString
|
||||
|
||||
colField, err := rows.Columns()
|
||||
if err != nil {
|
||||
return &datasetList, err
|
||||
@@ -268,10 +265,10 @@ func query(slowDuration time.Duration,db dbControl,strQuery string, args ...inte
|
||||
}
|
||||
rows.Scan(valuePtrs...)
|
||||
|
||||
for idx, fieldname := range colField {
|
||||
fieldRowData := dbResult.RowInfo[strings.ToLower(fieldname)]
|
||||
for idx, fieldName := range colField {
|
||||
fieldRowData := dbResult.RowInfo[strings.ToLower(fieldName)]
|
||||
fieldRowData = append(fieldRowData, valuePtrs[idx])
|
||||
dbResult.RowInfo[strings.ToLower(fieldname)] = fieldRowData
|
||||
dbResult.RowInfo[strings.ToLower(fieldName)] = fieldRowData
|
||||
}
|
||||
dbResult.rowNum += 1
|
||||
}
|
||||
@@ -295,12 +292,12 @@ func exec(slowDuration time.Duration,db dbControl,strSql string, args ...interfa
|
||||
ret := &DBResult{}
|
||||
if db == nil {
|
||||
log.Error("cannot connect database:%s", strSql)
|
||||
return ret, fmt.Errorf("cannot connect database!")
|
||||
return ret, fmt.Errorf("cannot connect database")
|
||||
}
|
||||
|
||||
if checkArgs(args) != nil {
|
||||
log.Error("CheckArgs is error :%s", strSql)
|
||||
return ret, fmt.Errorf("CheckArgs is error!")
|
||||
return ret, fmt.Errorf("checkArgs is error")
|
||||
}
|
||||
|
||||
TimeFuncStart := time.Now()
|
||||
@@ -361,21 +358,21 @@ func (ds *DataSetList) slice2interface(in interface{}) error {
|
||||
}
|
||||
|
||||
v := reflect.ValueOf(in).Elem()
|
||||
newv := reflect.MakeSlice(v.Type(), 0, length)
|
||||
v.Set(newv)
|
||||
newV := reflect.MakeSlice(v.Type(), 0, length)
|
||||
v.Set(newV)
|
||||
v.SetLen(length)
|
||||
|
||||
for i := 0; i < length; i++ {
|
||||
idxv := v.Index(i)
|
||||
if idxv.Kind() == reflect.Ptr {
|
||||
newObj := reflect.New(idxv.Type().Elem())
|
||||
idxV := v.Index(i)
|
||||
if idxV.Kind() == reflect.Ptr {
|
||||
newObj := reflect.New(idxV.Type().Elem())
|
||||
v.Index(i).Set(newObj)
|
||||
idxv = newObj
|
||||
idxV = newObj
|
||||
} else {
|
||||
idxv = idxv.Addr()
|
||||
idxV = idxV.Addr()
|
||||
}
|
||||
|
||||
err := ds.rowData2interface(i, ds.dataSetList[ds.currentDataSetIdx].RowInfo, idxv)
|
||||
err := ds.rowData2interface(i, ds.dataSetList[ds.currentDataSetIdx].RowInfo, idxV)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -390,7 +387,7 @@ func (ds *DataSetList) rowData2interface(rowIdx int, m map[string][]interface{},
|
||||
typ := t.Elem()
|
||||
|
||||
if !val.IsValid() {
|
||||
return errors.New("Incorrect data type!")
|
||||
return errors.New("incorrect data type")
|
||||
}
|
||||
|
||||
for i := 0; i < val.NumField(); i++ {
|
||||
@@ -402,11 +399,11 @@ func (ds *DataSetList) rowData2interface(rowIdx int, m map[string][]interface{},
|
||||
}
|
||||
|
||||
if tag != "" && tag != "-" {
|
||||
vtag := strings.ToLower(tag)
|
||||
columnData, ok := m[vtag]
|
||||
vTag := strings.ToLower(tag)
|
||||
columnData, ok := m[vTag]
|
||||
if ok == false {
|
||||
if !ds.blur {
|
||||
return fmt.Errorf("Cannot find filed name %s!", vtag)
|
||||
return fmt.Errorf("cannot find filed name %s", vTag)
|
||||
}
|
||||
continue
|
||||
}
|
||||
@@ -416,12 +413,12 @@ func (ds *DataSetList) rowData2interface(rowIdx int, m map[string][]interface{},
|
||||
meta := columnData[rowIdx].(*sql.NullString)
|
||||
if !ok {
|
||||
if !ds.blur {
|
||||
return fmt.Errorf("No corresponding field was found in the result set %s!", tag)
|
||||
return fmt.Errorf("no corresponding field was found in the result set %s", tag)
|
||||
}
|
||||
continue
|
||||
}
|
||||
if !value.CanSet() {
|
||||
return errors.New("Struct fields do not have read or write permissions!")
|
||||
return errors.New("struct fields do not have read or write permissions")
|
||||
}
|
||||
|
||||
if meta.Valid == false {
|
||||
@@ -460,7 +457,7 @@ func (ds *DataSetList) rowData2interface(rowIdx int, m map[string][]interface{},
|
||||
}
|
||||
value.SetBool(b)
|
||||
default:
|
||||
return errors.New("The database map has unrecognized data types!")
|
||||
return errors.New("the database map has unrecognized data types")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
314
sysmodule/netmodule/ginmodule/GinModule.go
Normal file
314
sysmodule/netmodule/ginmodule/GinModule.go
Normal file
@@ -0,0 +1,314 @@
|
||||
package ginmodule
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/duanhf2012/origin/v2/event"
|
||||
"github.com/duanhf2012/origin/v2/log"
|
||||
"github.com/duanhf2012/origin/v2/service"
|
||||
"github.com/gin-gonic/gin"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type IGinProcessor interface {
|
||||
Process(data *gin.Context) (*gin.Context, error)
|
||||
}
|
||||
|
||||
type GinModule struct {
|
||||
service.Module
|
||||
|
||||
*gin.Engine
|
||||
srv *http.Server
|
||||
|
||||
listenAddr string
|
||||
handleTimeout time.Duration
|
||||
processor []IGinProcessor
|
||||
}
|
||||
|
||||
func (gm *GinModule) Init(addr string, handleTimeout time.Duration, engine *gin.Engine) {
|
||||
gm.listenAddr = addr
|
||||
gm.handleTimeout = handleTimeout
|
||||
gm.Engine = engine
|
||||
}
|
||||
|
||||
func (gm *GinModule) SetupDataProcessor(processor ...IGinProcessor) {
|
||||
gm.processor = processor
|
||||
}
|
||||
|
||||
func (gm *GinModule) AppendDataProcessor(processor ...IGinProcessor) {
|
||||
gm.processor = append(gm.processor, processor...)
|
||||
}
|
||||
|
||||
func (gm *GinModule) OnInit() error {
|
||||
if gm.Engine == nil {
|
||||
gm.Engine = gin.Default()
|
||||
}
|
||||
|
||||
gm.srv = &http.Server{
|
||||
Addr: gm.listenAddr,
|
||||
Handler: gm.Engine,
|
||||
}
|
||||
|
||||
gm.Engine.Use(Logger())
|
||||
gm.Engine.Use(gin.Recovery())
|
||||
gm.GetEventProcessor().RegEventReceiverFunc(event.Sys_Event_Gin_Event, gm.GetEventHandler(), gm.eventHandler)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (gm *GinModule) eventHandler(ev event.IEvent) {
|
||||
ginEvent := ev.(*GinEvent)
|
||||
for _, handler := range ginEvent.handlersChain {
|
||||
handler(&ginEvent.c)
|
||||
}
|
||||
|
||||
//ginEvent.chanWait <- struct{}{}
|
||||
}
|
||||
|
||||
func (gm *GinModule) Start() {
|
||||
gm.srv.Addr = gm.listenAddr
|
||||
log.Info("http start listen", slog.Any("addr", gm.listenAddr))
|
||||
go func() {
|
||||
err := gm.srv.ListenAndServe()
|
||||
if err != nil {
|
||||
log.Error("ListenAndServe error", slog.Any("error", err.Error()))
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func (gm *GinModule) StartTLS(certFile, keyFile string) {
|
||||
log.Info("http start listen", slog.Any("addr", gm.listenAddr))
|
||||
go func() {
|
||||
err := gm.srv.ListenAndServeTLS(certFile, keyFile)
|
||||
if err != nil {
|
||||
log.Fatal("ListenAndServeTLS error", slog.Any("error", err.Error()))
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func (gm *GinModule) Stop(ctx context.Context) {
|
||||
if err := gm.srv.Shutdown(ctx); err != nil {
|
||||
log.Error("Server Shutdown", slog.Any("error", err))
|
||||
}
|
||||
}
|
||||
|
||||
type SafeContext struct {
|
||||
*gin.Context
|
||||
chanWait chan struct{}
|
||||
}
|
||||
|
||||
func (c *SafeContext) JSONAndDone(code int, obj any) {
|
||||
c.Context.JSON(code, obj)
|
||||
c.Done()
|
||||
}
|
||||
|
||||
func (c *SafeContext) AsciiJSONAndDone(code int, obj any) {
|
||||
c.Context.AsciiJSON(code, obj)
|
||||
c.Done()
|
||||
}
|
||||
|
||||
func (c *SafeContext) PureJSONAndDone(code int, obj any) {
|
||||
c.Context.PureJSON(code, obj)
|
||||
c.Done()
|
||||
}
|
||||
|
||||
func (c *SafeContext) XMLAndDone(code int, obj any) {
|
||||
c.Context.XML(code, obj)
|
||||
c.Done()
|
||||
}
|
||||
|
||||
func (c *SafeContext) YAMLAndDone(code int, obj any) {
|
||||
c.Context.YAML(code, obj)
|
||||
c.Done()
|
||||
}
|
||||
|
||||
func (c *SafeContext) TOMLAndDone(code int, obj any) {
|
||||
c.Context.TOML(code, obj)
|
||||
c.Done()
|
||||
}
|
||||
|
||||
func (c *SafeContext) ProtoBufAndDone(code int, obj any) {
|
||||
c.Context.ProtoBuf(code, obj)
|
||||
c.Done()
|
||||
}
|
||||
|
||||
func (c *SafeContext) StringAndDone(code int, format string, values ...any) {
|
||||
c.Context.String(code, format, values...)
|
||||
c.Done()
|
||||
}
|
||||
|
||||
func (c *SafeContext) RedirectAndDone(code int, location string) {
|
||||
c.Context.Redirect(code, location)
|
||||
c.Done()
|
||||
}
|
||||
|
||||
func (c *SafeContext) DataAndDone(code int, contentType string, data []byte) {
|
||||
c.Context.Data(code, contentType, data)
|
||||
c.Done()
|
||||
}
|
||||
|
||||
func (c *SafeContext) DataFromReaderAndDone(code int, contentLength int64, contentType string, reader io.Reader, extraHeaders map[string]string) {
|
||||
c.DataFromReader(code, contentLength, contentType, reader, extraHeaders)
|
||||
c.Done()
|
||||
}
|
||||
|
||||
func (c *SafeContext) HTMLAndDone(code int, name string, obj any) {
|
||||
c.Context.HTML(code, name, obj)
|
||||
c.Done()
|
||||
}
|
||||
|
||||
func (c *SafeContext) IndentedJSONAndDone(code int, obj any) {
|
||||
c.Context.IndentedJSON(code, obj)
|
||||
c.Done()
|
||||
}
|
||||
|
||||
func (c *SafeContext) SecureJSONAndDone(code int, obj any) {
|
||||
c.Context.SecureJSON(code, obj)
|
||||
c.Done()
|
||||
}
|
||||
|
||||
func (c *SafeContext) JSONPAndDone(code int, obj any) {
|
||||
c.Context.JSONP(code, obj)
|
||||
c.Done()
|
||||
}
|
||||
|
||||
func (c *SafeContext) Done() {
|
||||
c.chanWait <- struct{}{}
|
||||
}
|
||||
|
||||
type GinEvent struct {
|
||||
handlersChain []SafeHandlerFunc
|
||||
c SafeContext
|
||||
}
|
||||
|
||||
type SafeHandlerFunc func(*SafeContext)
|
||||
|
||||
func (ge *GinEvent) GetEventType() event.EventType {
|
||||
return event.Sys_Event_Gin_Event
|
||||
}
|
||||
|
||||
func (gm *GinModule) handleMethod(httpMethod, relativePath string, handlers ...SafeHandlerFunc) gin.IRoutes {
|
||||
return gm.Engine.Handle(httpMethod, relativePath, func(c *gin.Context) {
|
||||
for _, p := range gm.processor {
|
||||
_, err := p.Process(c)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
var ev GinEvent
|
||||
chanWait := make(chan struct{}, 2)
|
||||
ev.c.chanWait = chanWait
|
||||
ev.handlersChain = handlers
|
||||
ev.c.Context = c
|
||||
gm.NotifyEvent(&ev)
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), gm.handleTimeout)
|
||||
defer cancel()
|
||||
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
log.Error("GinModule process timeout", slog.Any("path", c.Request.URL.Path))
|
||||
c.AbortWithStatus(http.StatusRequestTimeout)
|
||||
case <-chanWait:
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// GET 回调处理是在gin协程中
|
||||
func (gm *GinModule) GET(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {
|
||||
return gm.Engine.GET(relativePath, handlers...)
|
||||
}
|
||||
|
||||
// POST 回调处理是在gin协程中
|
||||
func (gm *GinModule) POST(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {
|
||||
return gm.Engine.POST(relativePath, handlers...)
|
||||
}
|
||||
|
||||
// DELETE 回调处理是在gin协程中
|
||||
func (gm *GinModule) DELETE(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {
|
||||
return gm.Engine.DELETE(relativePath, handlers...)
|
||||
}
|
||||
|
||||
// PATCH 回调处理是在gin协程中
|
||||
func (gm *GinModule) PATCH(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {
|
||||
return gm.Engine.PATCH(relativePath, handlers...)
|
||||
}
|
||||
|
||||
// Put 回调处理是在gin协程中
|
||||
func (gm *GinModule) Put(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {
|
||||
return gm.Engine.PUT(relativePath, handlers...)
|
||||
}
|
||||
|
||||
// SafeGET 回调处理是在service协程中
|
||||
func (gm *GinModule) SafeGET(relativePath string, handlers ...SafeHandlerFunc) gin.IRoutes {
|
||||
return gm.handleMethod(http.MethodGet, relativePath, handlers...)
|
||||
}
|
||||
|
||||
// SafePOST 回调处理是在service协程中
|
||||
func (gm *GinModule) SafePOST(relativePath string, handlers ...SafeHandlerFunc) gin.IRoutes {
|
||||
return gm.handleMethod(http.MethodPost, relativePath, handlers...)
|
||||
}
|
||||
|
||||
// SafeDELETE 回调处理是在service协程中
|
||||
func (gm *GinModule) SafeDELETE(relativePath string, handlers ...SafeHandlerFunc) gin.IRoutes {
|
||||
return gm.handleMethod(http.MethodDelete, relativePath, handlers...)
|
||||
}
|
||||
|
||||
// SafePATCH 回调处理是在service协程中
|
||||
func (gm *GinModule) SafePATCH(relativePath string, handlers ...SafeHandlerFunc) gin.IRoutes {
|
||||
return gm.handleMethod(http.MethodPatch, relativePath, handlers...)
|
||||
}
|
||||
|
||||
// SafePut 回调处理是在service协程中
|
||||
func (gm *GinModule) SafePut(relativePath string, handlers ...SafeHandlerFunc) gin.IRoutes {
|
||||
return gm.handleMethod(http.MethodPut, relativePath, handlers...)
|
||||
}
|
||||
|
||||
func GetIPWithProxyHeaders(c *gin.Context) string {
|
||||
// 尝试从 X-Real-IP 头部获取真实 IP
|
||||
ip := c.GetHeader("X-Real-IP")
|
||||
|
||||
// 如果 X-Real-IP 头部不存在,则尝试从 X-Forwarded-For 头部获取
|
||||
if ip == "" {
|
||||
ip = c.GetHeader("X-Forwarded-For")
|
||||
}
|
||||
|
||||
// 如果两者都不存在,则使用默认的 ClientIP 方法获取 IP
|
||||
if ip == "" {
|
||||
ip = c.ClientIP()
|
||||
}
|
||||
|
||||
return ip
|
||||
}
|
||||
|
||||
func GetIPWithValidatedProxyHeaders(c *gin.Context) string {
|
||||
// 获取代理头部
|
||||
proxyHeaders := c.Request.Header.Get("X-Real-IP,X-Forwarded-For")
|
||||
|
||||
// 分割代理头部,取第一个 IP 作为真实 IP
|
||||
ips := strings.Split(proxyHeaders, ",")
|
||||
ip := strings.TrimSpace(ips[0])
|
||||
|
||||
// 如果 IP 格式合法,则使用获取到的 IP,否则使用默认的 ClientIP 方法获取
|
||||
if isValidIP(ip) {
|
||||
return ip
|
||||
} else {
|
||||
ip = c.ClientIP()
|
||||
return ip
|
||||
}
|
||||
}
|
||||
|
||||
// isValidIP 判断 IP 格式是否合法
|
||||
func isValidIP(ip string) bool {
|
||||
// 此处添加自定义的 IP 格式验证逻辑
|
||||
// 例如,使用正则表达式验证 IP 格式
|
||||
// ...
|
||||
if ip == "" {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
@@ -2,7 +2,7 @@ package ginmodule
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duanhf2012/origin/log"
|
||||
"github.com/duanhf2012/origin/v2/log"
|
||||
"github.com/gin-gonic/gin"
|
||||
"time"
|
||||
)
|
||||
@@ -44,7 +44,7 @@ func Logger() gin.HandlerFunc {
|
||||
// 响应状态码
|
||||
statusCode := c.Writer.Status()
|
||||
|
||||
log.SDebug(fmt.Sprintf(
|
||||
log.Debug(fmt.Sprintf(
|
||||
"%s | %3d | %s %10s | \033[44;37m%-6s\033[0m %s %s | %10v | \"%s\" \"%s\"",
|
||||
colorForStatus(statusCode),
|
||||
statusCode,
|
||||
214
sysmodule/netmodule/kcpmodule/KcpModule.go
Normal file
214
sysmodule/netmodule/kcpmodule/KcpModule.go
Normal file
@@ -0,0 +1,214 @@
|
||||
package kcpmodule
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duanhf2012/origin/v2/event"
|
||||
"github.com/duanhf2012/origin/v2/log"
|
||||
"github.com/duanhf2012/origin/v2/network"
|
||||
"github.com/duanhf2012/origin/v2/network/processor"
|
||||
"github.com/duanhf2012/origin/v2/service"
|
||||
"github.com/xtaci/kcp-go/v5"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"runtime"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type KcpModule struct {
|
||||
service.Module
|
||||
blockCrypt kcp.BlockCrypt
|
||||
|
||||
mapClientLocker sync.RWMutex
|
||||
mapClient map[string]*Client
|
||||
process processor.IRawProcessor
|
||||
|
||||
kcpServer network.KCPServer
|
||||
kcpCfg *network.KcpCfg
|
||||
}
|
||||
|
||||
type Client struct {
|
||||
id string
|
||||
kcpConn *network.NetConn
|
||||
kcpModule *KcpModule
|
||||
}
|
||||
|
||||
type KcpPackType int8
|
||||
|
||||
const (
|
||||
KPTConnected KcpPackType = 0
|
||||
KPTDisConnected KcpPackType = 1
|
||||
KPTPack KcpPackType = 2
|
||||
KPTUnknownPack KcpPackType = 3
|
||||
)
|
||||
|
||||
type KcpPack struct {
|
||||
Type KcpPackType //0表示连接 1表示断开 2表示数据
|
||||
ClientId string
|
||||
Data interface{}
|
||||
RecyclerReaderBytes func(data []byte)
|
||||
}
|
||||
|
||||
func (km *KcpModule) OnInit() error {
|
||||
if km.kcpCfg == nil || km.process == nil {
|
||||
return fmt.Errorf("please call the Init function correctly")
|
||||
}
|
||||
|
||||
km.mapClient = make(map[string]*Client, km.kcpCfg.MaxConnNum)
|
||||
km.GetEventProcessor().RegEventReceiverFunc(event.Sys_Event_Kcp, km.GetEventHandler(), km.kcpEventHandler)
|
||||
km.process.SetByteOrder(km.kcpCfg.LittleEndian)
|
||||
km.kcpServer.Init(km.kcpCfg)
|
||||
km.kcpServer.NewAgent = km.NewAgent
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (km *KcpModule) Init(kcpCfg *network.KcpCfg, process processor.IRawProcessor) {
|
||||
km.kcpCfg = kcpCfg
|
||||
km.process = process
|
||||
}
|
||||
|
||||
func (km *KcpModule) Start() error {
|
||||
return km.kcpServer.Start()
|
||||
}
|
||||
|
||||
func (km *KcpModule) kcpEventHandler(ev event.IEvent) {
|
||||
e := ev.(*event.Event)
|
||||
switch KcpPackType(e.IntExt[0]) {
|
||||
case KPTConnected:
|
||||
km.process.ConnectedRoute(e.StringExt[0])
|
||||
case KPTDisConnected:
|
||||
km.process.DisConnectedRoute(e.StringExt[0])
|
||||
case KPTUnknownPack:
|
||||
km.process.UnknownMsgRoute(e.StringExt[0], e.Data, e.AnyExt[0].(func(data []byte)))
|
||||
case KPTPack:
|
||||
km.process.MsgRoute(e.StringExt[0], e.Data, e.AnyExt[0].(func(data []byte)))
|
||||
}
|
||||
|
||||
event.DeleteEvent(ev)
|
||||
}
|
||||
|
||||
func (km *KcpModule) SetBlob(blockCrypt kcp.BlockCrypt) {
|
||||
km.blockCrypt = blockCrypt
|
||||
}
|
||||
|
||||
func (km *KcpModule) OnConnected(c *Client) {
|
||||
ev := event.NewEvent()
|
||||
ev.Type = event.Sys_Event_Kcp
|
||||
ev.IntExt[0] = int64(KPTConnected)
|
||||
ev.StringExt[0] = c.id
|
||||
|
||||
km.NotifyEvent(ev)
|
||||
}
|
||||
|
||||
func (km *KcpModule) OnClose(c *Client) {
|
||||
ev := event.NewEvent()
|
||||
ev.Type = event.Sys_Event_Kcp
|
||||
ev.IntExt[0] = int64(KPTDisConnected)
|
||||
ev.StringExt[0] = c.id
|
||||
|
||||
km.NotifyEvent(ev)
|
||||
}
|
||||
|
||||
func (km *KcpModule) newClient(conn network.Conn) *Client {
|
||||
km.mapClientLocker.Lock()
|
||||
defer km.mapClientLocker.Unlock()
|
||||
|
||||
pClient := &Client{kcpConn: conn.(*network.NetConn), id: primitive.NewObjectID().Hex()}
|
||||
pClient.kcpModule = km
|
||||
km.mapClient[pClient.id] = pClient
|
||||
|
||||
return pClient
|
||||
}
|
||||
|
||||
func (km *KcpModule) GetProcessor() processor.IRawProcessor {
|
||||
return km.process
|
||||
}
|
||||
|
||||
func (km *KcpModule) SendRawMsg(clientId string, data []byte) error {
|
||||
km.mapClientLocker.Lock()
|
||||
client, ok := km.mapClient[clientId]
|
||||
if ok == false {
|
||||
km.mapClientLocker.Unlock()
|
||||
return fmt.Errorf("client %s is disconnect", clientId)
|
||||
}
|
||||
km.mapClientLocker.Unlock()
|
||||
return client.kcpConn.WriteMsg(data)
|
||||
}
|
||||
|
||||
func (km *KcpModule) Close(clientId string) {
|
||||
km.mapClientLocker.Lock()
|
||||
client, ok := km.mapClient[clientId]
|
||||
if ok == false {
|
||||
km.mapClientLocker.Unlock()
|
||||
return
|
||||
}
|
||||
km.mapClientLocker.Unlock()
|
||||
client.kcpConn.Close()
|
||||
}
|
||||
|
||||
func (km *KcpModule) GetClientIp(clientId string) string {
|
||||
km.mapClientLocker.Lock()
|
||||
defer km.mapClientLocker.Unlock()
|
||||
client, ok := km.mapClient[clientId]
|
||||
if ok == false {
|
||||
return ""
|
||||
}
|
||||
removeAddr := client.kcpConn.RemoteAddr()
|
||||
if removeAddr != nil {
|
||||
return removeAddr.String()
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (km *KcpModule) NewAgent(conn network.Conn) network.Agent {
|
||||
c := km.newClient(conn)
|
||||
return c
|
||||
}
|
||||
|
||||
func (c *Client) Run() {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
buf := make([]byte, 4096)
|
||||
l := runtime.Stack(buf, false)
|
||||
errString := fmt.Sprint(r)
|
||||
log.Dump(string(buf[:l]), log.String("error", errString))
|
||||
}
|
||||
}()
|
||||
|
||||
c.kcpModule.OnConnected(c)
|
||||
for c.kcpConn != nil {
|
||||
c.kcpConn.SetReadDeadline(*c.kcpModule.kcpCfg.ReadDeadlineMill)
|
||||
msgBuff, err := c.kcpConn.ReadMsg()
|
||||
if err != nil {
|
||||
log.Debug("read client failed", log.ErrorAttr("error", err), log.String("clientId", c.id))
|
||||
break
|
||||
}
|
||||
|
||||
data, err := c.kcpModule.process.Unmarshal(c.id, msgBuff)
|
||||
if err != nil {
|
||||
ev := event.NewEvent()
|
||||
ev.Type = event.Sys_Event_Kcp
|
||||
ev.IntExt[0] = int64(KPTUnknownPack)
|
||||
ev.StringExt[0] = c.id
|
||||
ev.Data = msgBuff
|
||||
ev.AnyExt[0] = c.kcpConn.GetRecyclerReaderBytes()
|
||||
c.kcpModule.NotifyEvent(ev)
|
||||
continue
|
||||
}
|
||||
|
||||
ev := event.NewEvent()
|
||||
ev.Type = event.Sys_Event_Kcp
|
||||
ev.IntExt[0] = int64(KPTPack)
|
||||
ev.StringExt[0] = c.id
|
||||
ev.Data = data
|
||||
ev.AnyExt[0] = c.kcpConn.GetRecyclerReaderBytes()
|
||||
c.kcpModule.NotifyEvent(ev)
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client) OnClose() {
|
||||
c.kcpModule.OnClose(c)
|
||||
|
||||
c.kcpModule.mapClientLocker.Lock()
|
||||
delete(c.kcpModule.mapClient, c.id)
|
||||
c.kcpModule.mapClientLocker.Unlock()
|
||||
}
|
||||
223
sysmodule/netmodule/tcpmodule/TcpModule.go
Normal file
223
sysmodule/netmodule/tcpmodule/TcpModule.go
Normal file
@@ -0,0 +1,223 @@
|
||||
package tcpmodule
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duanhf2012/origin/v2/event"
|
||||
"github.com/duanhf2012/origin/v2/log"
|
||||
"github.com/duanhf2012/origin/v2/network"
|
||||
"github.com/duanhf2012/origin/v2/network/processor"
|
||||
"github.com/duanhf2012/origin/v2/service"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"runtime"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
type TcpModule struct {
|
||||
tcpServer network.TCPServer
|
||||
service.Module
|
||||
|
||||
mapClientLocker sync.RWMutex
|
||||
mapClient map[string]*Client
|
||||
process processor.IRawProcessor
|
||||
tcpCfg *TcpCfg
|
||||
}
|
||||
|
||||
type TcpPackType int8
|
||||
|
||||
const (
|
||||
TPTConnected TcpPackType = 0
|
||||
TPTDisConnected TcpPackType = 1
|
||||
TPTPack TcpPackType = 2
|
||||
TPTUnknownPack TcpPackType = 3
|
||||
)
|
||||
|
||||
type TcpPack struct {
|
||||
Type TcpPackType //0表示连接 1表示断开 2表示数据
|
||||
ClientId string
|
||||
Data interface{}
|
||||
RecyclerReaderBytes func(data []byte)
|
||||
}
|
||||
|
||||
type Client struct {
|
||||
id string
|
||||
tcpConn *network.NetConn
|
||||
tcpModule *TcpModule
|
||||
}
|
||||
|
||||
type TcpCfg struct {
|
||||
ListenAddr string //监听地址
|
||||
MaxConnNum int //最大连接数
|
||||
PendingWriteNum int //写channel最大消息数量
|
||||
LittleEndian bool //是否小端序
|
||||
LenMsgLen int //消息头占用byte数量,只能是1byte,2byte,4byte。如果是4byte,意味着消息最大可以是math.MaxUint32(4GB)
|
||||
MinMsgLen uint32 //最小消息长度
|
||||
MaxMsgLen uint32 //最大消息长度,超过判定不合法,断开连接
|
||||
ReadDeadlineSecond time.Duration //读超时
|
||||
WriteDeadlineSecond time.Duration //写超时
|
||||
}
|
||||
|
||||
func (tm *TcpModule) OnInit() error {
|
||||
if tm.tcpCfg == nil || tm.process == nil {
|
||||
return fmt.Errorf("please call the Init function correctly")
|
||||
}
|
||||
|
||||
//2.初始化网络模块
|
||||
tm.tcpServer.Addr = tm.tcpCfg.ListenAddr
|
||||
tm.tcpServer.MaxConnNum = tm.tcpCfg.MaxConnNum
|
||||
tm.tcpServer.PendingWriteNum = tm.tcpCfg.PendingWriteNum
|
||||
tm.tcpServer.LittleEndian = tm.tcpCfg.LittleEndian
|
||||
tm.tcpServer.LenMsgLen = tm.tcpCfg.LenMsgLen
|
||||
tm.tcpServer.MinMsgLen = tm.tcpCfg.MinMsgLen
|
||||
tm.tcpServer.MaxMsgLen = tm.tcpCfg.MaxMsgLen
|
||||
tm.tcpServer.ReadDeadline = tm.tcpCfg.ReadDeadlineSecond * time.Second
|
||||
tm.tcpServer.WriteDeadline = tm.tcpCfg.WriteDeadlineSecond * time.Second
|
||||
tm.mapClient = make(map[string]*Client, tm.tcpServer.MaxConnNum)
|
||||
tm.tcpServer.NewAgent = tm.NewClient
|
||||
|
||||
//3.设置解析处理器
|
||||
tm.process.SetByteOrder(tm.tcpCfg.LittleEndian)
|
||||
|
||||
//4.设置网络事件处理
|
||||
tm.GetEventProcessor().RegEventReceiverFunc(event.Sys_Event_Tcp, tm.GetEventHandler(), tm.tcpEventHandler)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (tm *TcpModule) Init(tcpCfg *TcpCfg, process processor.IRawProcessor) {
|
||||
tm.tcpCfg = tcpCfg
|
||||
tm.process = process
|
||||
}
|
||||
|
||||
func (tm *TcpModule) Start() error {
|
||||
return tm.tcpServer.Start()
|
||||
}
|
||||
|
||||
func (tm *TcpModule) tcpEventHandler(ev event.IEvent) {
|
||||
pack := ev.(*event.Event).Data.(TcpPack)
|
||||
switch pack.Type {
|
||||
case TPTConnected:
|
||||
tm.process.ConnectedRoute(pack.ClientId)
|
||||
case TPTDisConnected:
|
||||
tm.process.DisConnectedRoute(pack.ClientId)
|
||||
case TPTUnknownPack:
|
||||
tm.process.UnknownMsgRoute(pack.ClientId, pack.Data, pack.RecyclerReaderBytes)
|
||||
case TPTPack:
|
||||
tm.process.MsgRoute(pack.ClientId, pack.Data, pack.RecyclerReaderBytes)
|
||||
}
|
||||
}
|
||||
|
||||
func (tm *TcpModule) NewClient(conn network.Conn) network.Agent {
|
||||
tm.mapClientLocker.Lock()
|
||||
defer tm.mapClientLocker.Unlock()
|
||||
|
||||
clientId := primitive.NewObjectID().Hex()
|
||||
pClient := &Client{tcpConn: conn.(*network.NetConn), id: clientId}
|
||||
pClient.tcpModule = tm
|
||||
tm.mapClient[clientId] = pClient
|
||||
|
||||
return pClient
|
||||
}
|
||||
|
||||
func (slf *Client) GetId() string {
|
||||
return slf.id
|
||||
}
|
||||
|
||||
func (slf *Client) Run() {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
buf := make([]byte, 4096)
|
||||
l := runtime.Stack(buf, false)
|
||||
errString := fmt.Sprint(r)
|
||||
log.Dump(string(buf[:l]), log.String("error", errString))
|
||||
}
|
||||
}()
|
||||
|
||||
slf.tcpModule.NotifyEvent(&event.Event{Type: event.Sys_Event_Tcp, Data: TcpPack{ClientId: slf.id, Type: TPTConnected}})
|
||||
for slf.tcpConn != nil {
|
||||
slf.tcpConn.SetReadDeadline(slf.tcpModule.tcpServer.ReadDeadline)
|
||||
bytes, err := slf.tcpConn.ReadMsg()
|
||||
if err != nil {
|
||||
log.Debug("read client failed", log.ErrorAttr("error", err), log.String("clientId", slf.id))
|
||||
break
|
||||
}
|
||||
data, err := slf.tcpModule.process.Unmarshal(slf.id, bytes)
|
||||
if err != nil {
|
||||
slf.tcpModule.NotifyEvent(&event.Event{Type: event.Sys_Event_Tcp, Data: TcpPack{ClientId: slf.id, Type: TPTUnknownPack, Data: bytes, RecyclerReaderBytes: slf.tcpConn.GetRecyclerReaderBytes()}})
|
||||
continue
|
||||
}
|
||||
slf.tcpModule.NotifyEvent(&event.Event{Type: event.Sys_Event_Tcp, Data: TcpPack{ClientId: slf.id, Type: TPTPack, Data: data, RecyclerReaderBytes: slf.tcpConn.GetRecyclerReaderBytes()}})
|
||||
}
|
||||
}
|
||||
|
||||
func (slf *Client) OnClose() {
|
||||
slf.tcpModule.NotifyEvent(&event.Event{Type: event.Sys_Event_Tcp, Data: TcpPack{ClientId: slf.id, Type: TPTDisConnected}})
|
||||
slf.tcpModule.mapClientLocker.Lock()
|
||||
defer slf.tcpModule.mapClientLocker.Unlock()
|
||||
delete(slf.tcpModule.mapClient, slf.GetId())
|
||||
}
|
||||
|
||||
func (tm *TcpModule) SendMsg(clientId string, msg interface{}) error {
|
||||
tm.mapClientLocker.Lock()
|
||||
client, ok := tm.mapClient[clientId]
|
||||
if ok == false {
|
||||
tm.mapClientLocker.Unlock()
|
||||
return fmt.Errorf("client %s is disconnect", clientId)
|
||||
}
|
||||
|
||||
tm.mapClientLocker.Unlock()
|
||||
bytes, err := tm.process.Marshal(clientId, msg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return client.tcpConn.WriteMsg(bytes)
|
||||
}
|
||||
|
||||
func (tm *TcpModule) Close(clientId string) {
|
||||
tm.mapClientLocker.Lock()
|
||||
defer tm.mapClientLocker.Unlock()
|
||||
|
||||
client, ok := tm.mapClient[clientId]
|
||||
if ok == false {
|
||||
return
|
||||
}
|
||||
|
||||
if client.tcpConn != nil {
|
||||
client.tcpConn.Close()
|
||||
}
|
||||
|
||||
log.SWarning("close client:", clientId)
|
||||
return
|
||||
}
|
||||
|
||||
func (tm *TcpModule) GetClientIp(clientId string) string {
|
||||
tm.mapClientLocker.Lock()
|
||||
defer tm.mapClientLocker.Unlock()
|
||||
pClient, ok := tm.mapClient[clientId]
|
||||
if ok == false {
|
||||
return ""
|
||||
}
|
||||
|
||||
return pClient.tcpConn.GetRemoteIp()
|
||||
}
|
||||
|
||||
func (tm *TcpModule) SendRawMsg(clientId string, msg []byte) error {
|
||||
tm.mapClientLocker.Lock()
|
||||
client, ok := tm.mapClient[clientId]
|
||||
if ok == false {
|
||||
tm.mapClientLocker.Unlock()
|
||||
return fmt.Errorf("client %s is disconnect", clientId)
|
||||
}
|
||||
tm.mapClientLocker.Unlock()
|
||||
return client.tcpConn.WriteMsg(msg)
|
||||
}
|
||||
|
||||
func (tm *TcpModule) GetConnNum() int {
|
||||
tm.mapClientLocker.Lock()
|
||||
connNum := len(tm.mapClient)
|
||||
tm.mapClientLocker.Unlock()
|
||||
return connNum
|
||||
}
|
||||
|
||||
func (tm *TcpModule) GetProcessor() processor.IRawProcessor {
|
||||
return tm.process
|
||||
}
|
||||
199
sysmodule/netmodule/wsmodule/WSModule.go
Normal file
199
sysmodule/netmodule/wsmodule/WSModule.go
Normal file
@@ -0,0 +1,199 @@
|
||||
package wsmodule
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duanhf2012/origin/v2/event"
|
||||
"github.com/duanhf2012/origin/v2/log"
|
||||
"github.com/duanhf2012/origin/v2/network"
|
||||
"github.com/duanhf2012/origin/v2/network/processor"
|
||||
"github.com/duanhf2012/origin/v2/service"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type WSModule struct {
|
||||
service.Module
|
||||
|
||||
wsServer network.WSServer
|
||||
|
||||
mapClientLocker sync.RWMutex
|
||||
mapClient map[string]*WSClient
|
||||
process processor.IRawProcessor
|
||||
wsCfg *WSCfg
|
||||
}
|
||||
|
||||
type WSClient struct {
|
||||
id string
|
||||
wsConn *network.WSConn
|
||||
wsModule *WSModule
|
||||
}
|
||||
|
||||
type WSCfg struct {
|
||||
ListenAddr string
|
||||
MaxConnNum int
|
||||
PendingWriteNum int
|
||||
MaxMsgLen uint32
|
||||
LittleEndian bool //是否小端序
|
||||
}
|
||||
|
||||
type WSPackType int8
|
||||
|
||||
const (
|
||||
WPTConnected WSPackType = 0
|
||||
WPTDisConnected WSPackType = 1
|
||||
WPTPack WSPackType = 2
|
||||
WPTUnknownPack WSPackType = 3
|
||||
)
|
||||
|
||||
type WSPack struct {
|
||||
Type WSPackType //0表示连接 1表示断开 2表示数据
|
||||
MsgProcessor processor.IRawProcessor
|
||||
ClientId string
|
||||
Data any
|
||||
}
|
||||
|
||||
func (ws *WSModule) OnInit() error {
|
||||
if ws.wsCfg == nil || ws.process == nil {
|
||||
return fmt.Errorf("please call the Init function correctly")
|
||||
}
|
||||
|
||||
ws.wsServer.MaxConnNum = ws.wsCfg.MaxConnNum
|
||||
ws.wsServer.PendingWriteNum = ws.wsCfg.PendingWriteNum
|
||||
ws.wsServer.MaxMsgLen = ws.wsCfg.MaxMsgLen
|
||||
ws.wsServer.Addr = ws.wsCfg.ListenAddr
|
||||
|
||||
//3.设置解析处理器
|
||||
ws.process.SetByteOrder(ws.wsCfg.LittleEndian)
|
||||
|
||||
ws.mapClient = make(map[string]*WSClient, ws.wsServer.MaxConnNum)
|
||||
ws.wsServer.NewAgent = ws.NewWSClient
|
||||
|
||||
//4.设置网络事件处理
|
||||
ws.GetEventProcessor().RegEventReceiverFunc(event.Sys_Event_WebSocket, ws.GetEventHandler(), ws.wsEventHandler)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ws *WSModule) Init(wsCfg *WSCfg, process processor.IRawProcessor) {
|
||||
ws.wsCfg = wsCfg
|
||||
ws.process = process
|
||||
}
|
||||
|
||||
func (ws *WSModule) Start() error {
|
||||
return ws.wsServer.Start()
|
||||
}
|
||||
|
||||
func (ws *WSModule) wsEventHandler(ev event.IEvent) {
|
||||
pack := ev.(*event.Event).Data.(*WSPack)
|
||||
switch pack.Type {
|
||||
case WPTConnected:
|
||||
ws.process.ConnectedRoute(pack.ClientId)
|
||||
case WPTDisConnected:
|
||||
ws.process.DisConnectedRoute(pack.ClientId)
|
||||
case WPTUnknownPack:
|
||||
ws.process.UnknownMsgRoute(pack.ClientId, pack.Data, ws.recyclerReaderBytes)
|
||||
case WPTPack:
|
||||
ws.process.MsgRoute(pack.ClientId, pack.Data, ws.recyclerReaderBytes)
|
||||
}
|
||||
}
|
||||
|
||||
func (ws *WSModule) recyclerReaderBytes([]byte) {
|
||||
}
|
||||
|
||||
func (ws *WSModule) NewWSClient(conn *network.WSConn) network.Agent {
|
||||
ws.mapClientLocker.Lock()
|
||||
defer ws.mapClientLocker.Unlock()
|
||||
|
||||
pClient := &WSClient{wsConn: conn, id: primitive.NewObjectID().Hex()}
|
||||
pClient.wsModule = ws
|
||||
ws.mapClient[pClient.id] = pClient
|
||||
|
||||
return pClient
|
||||
}
|
||||
|
||||
func (wc *WSClient) GetId() string {
|
||||
return wc.id
|
||||
}
|
||||
|
||||
func (wc *WSClient) Run() {
|
||||
wc.wsModule.NotifyEvent(&event.Event{Type: event.Sys_Event_WebSocket, Data: &WSPack{ClientId: wc.id, Type: WPTConnected}})
|
||||
for {
|
||||
bytes, err := wc.wsConn.ReadMsg()
|
||||
if err != nil {
|
||||
log.Debug("read client is error", log.String("clientId", wc.id), log.ErrorAttr("err", err))
|
||||
break
|
||||
}
|
||||
data, err := wc.wsModule.process.Unmarshal(wc.id, bytes)
|
||||
if err != nil {
|
||||
wc.wsModule.NotifyEvent(&event.Event{Type: event.Sys_Event_WebSocket, Data: &WSPack{ClientId: wc.id, Type: WPTUnknownPack, Data: bytes}})
|
||||
continue
|
||||
}
|
||||
wc.wsModule.NotifyEvent(&event.Event{Type: event.Sys_Event_WebSocket, Data: &WSPack{ClientId: wc.id, Type: WPTPack, Data: data}})
|
||||
}
|
||||
}
|
||||
|
||||
func (wc *WSClient) OnClose() {
|
||||
wc.wsModule.NotifyEvent(&event.Event{Type: event.Sys_Event_WebSocket, Data: &WSPack{ClientId: wc.id, Type: WPTDisConnected}})
|
||||
wc.wsModule.mapClientLocker.Lock()
|
||||
defer wc.wsModule.mapClientLocker.Unlock()
|
||||
delete(wc.wsModule.mapClient, wc.GetId())
|
||||
}
|
||||
|
||||
func (ws *WSModule) GetProcessor() processor.IRawProcessor {
|
||||
return ws.process
|
||||
}
|
||||
|
||||
func (ws *WSModule) GetClientIp(clientId string) string {
|
||||
ws.mapClientLocker.Lock()
|
||||
defer ws.mapClientLocker.Unlock()
|
||||
|
||||
pClient, ok := ws.mapClient[clientId]
|
||||
if ok == false {
|
||||
return ""
|
||||
}
|
||||
|
||||
return pClient.wsConn.RemoteAddr().String()
|
||||
}
|
||||
|
||||
func (ws *WSModule) Close(clientId string) {
|
||||
ws.mapClientLocker.Lock()
|
||||
defer ws.mapClientLocker.Unlock()
|
||||
|
||||
client, ok := ws.mapClient[clientId]
|
||||
if ok == false {
|
||||
return
|
||||
}
|
||||
|
||||
if client.wsConn != nil {
|
||||
client.wsConn.Close()
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (ws *WSModule) SendMsg(clientId string, msg interface{}) error {
|
||||
ws.mapClientLocker.Lock()
|
||||
client, ok := ws.mapClient[clientId]
|
||||
if ok == false {
|
||||
ws.mapClientLocker.Unlock()
|
||||
return fmt.Errorf("client %s is disconnect", clientId)
|
||||
}
|
||||
|
||||
ws.mapClientLocker.Unlock()
|
||||
bytes, err := ws.process.Marshal(clientId, msg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return client.wsConn.WriteMsg(bytes)
|
||||
}
|
||||
|
||||
func (ws *WSModule) SendRawMsg(clientId string, msg []byte) error {
|
||||
ws.mapClientLocker.Lock()
|
||||
client, ok := ws.mapClient[clientId]
|
||||
if ok == false {
|
||||
ws.mapClientLocker.Unlock()
|
||||
return fmt.Errorf("client %s is disconnect", clientId)
|
||||
}
|
||||
ws.mapClientLocker.Unlock()
|
||||
return client.wsConn.WriteMsg(msg)
|
||||
}
|
||||
@@ -90,18 +90,18 @@ func (m *RedisModule) Init(redisCfg *ConfigRedis) {
|
||||
func (m *RedisModule) getConn() (redis.Conn, error) {
|
||||
if m.redisPool == nil {
|
||||
log.Error("Not Init RedisModule")
|
||||
return nil, fmt.Errorf("Not Init RedisModule")
|
||||
return nil, fmt.Errorf("not Init RedisModule")
|
||||
}
|
||||
conn := m.redisPool.Get()
|
||||
if conn == nil {
|
||||
log.Error("Cannot get connection")
|
||||
return nil, fmt.Errorf("Cannot get connection")
|
||||
return nil, fmt.Errorf("cannot get connection")
|
||||
}
|
||||
|
||||
if conn.Err() != nil {
|
||||
err := conn.Err()
|
||||
if err != nil {
|
||||
log.Error("Get Conn have error,reason:%v", err)
|
||||
log.Error("get Conn have error,reason:%v", err)
|
||||
}
|
||||
conn.Close()
|
||||
return nil, err
|
||||
@@ -153,7 +153,7 @@ func (m *RedisModule) SetStringJSONExpire(key interface{}, val interface{}, expi
|
||||
|
||||
func (m *RedisModule) setStringByExpire(key, value, expire interface{}) error {
|
||||
if key == "" {
|
||||
return errors.New("Key Is Empty")
|
||||
return errors.New("key is empty")
|
||||
}
|
||||
|
||||
conn, err := m.getConn()
|
||||
@@ -474,7 +474,7 @@ func (m *RedisModule) DelStringKeyList(keys []interface{}) (map[interface{}]bool
|
||||
|
||||
func (m *RedisModule) SetHash(redisKey, hashKey, value interface{}) error {
|
||||
if redisKey == "" || hashKey == "" {
|
||||
return errors.New("Key Is Empty")
|
||||
return errors.New("key is empty")
|
||||
}
|
||||
conn, err := m.getConn()
|
||||
if err != nil {
|
||||
@@ -490,10 +490,9 @@ func (m *RedisModule) SetHash(redisKey, hashKey, value interface{}) error {
|
||||
return retErr
|
||||
}
|
||||
|
||||
// GetRedisAllHashJSON ...
|
||||
func (m *RedisModule) GetAllHashJSON(redisKey string) (map[string]string, error) {
|
||||
if redisKey == "" {
|
||||
return nil, errors.New("Key Is Empty")
|
||||
return nil, errors.New("key is empty")
|
||||
}
|
||||
conn, err := m.getConn()
|
||||
if err != nil {
|
||||
@@ -513,7 +512,7 @@ func (m *RedisModule) GetAllHashJSON(redisKey string) (map[string]string, error)
|
||||
func (m *RedisModule) GetHash(redisKey interface{}, fieldKey interface{}) (string, error) {
|
||||
if redisKey == "" || fieldKey == "" {
|
||||
log.Error("GetHashValueByKey key is empty!")
|
||||
return "", errors.New("Key Is Empty")
|
||||
return "", errors.New("key is empty")
|
||||
}
|
||||
conn, err := m.getConn()
|
||||
if err != nil {
|
||||
@@ -527,7 +526,7 @@ func (m *RedisModule) GetHash(redisKey interface{}, fieldKey interface{}) (strin
|
||||
return "", err
|
||||
}
|
||||
if value == nil {
|
||||
return "", errors.New("Reids Get Hash nil")
|
||||
return "", errors.New("redis get hash nil")
|
||||
}
|
||||
|
||||
return redis.String(value, nil)
|
||||
@@ -536,7 +535,7 @@ func (m *RedisModule) GetHash(redisKey interface{}, fieldKey interface{}) (strin
|
||||
func (m *RedisModule) GetMuchHash(args ...interface{}) ([]string, error) {
|
||||
if len(args) < 2 {
|
||||
log.Error("GetHashValueByHashKeyList key len less than two!")
|
||||
return nil, errors.New("Key Is Empty")
|
||||
return nil, errors.New("key is empty")
|
||||
}
|
||||
conn, err := m.getConn()
|
||||
if err != nil {
|
||||
@@ -550,11 +549,11 @@ func (m *RedisModule) GetMuchHash(args ...interface{}) ([]string, error) {
|
||||
return nil, err
|
||||
}
|
||||
if value == nil {
|
||||
return nil, errors.New("Reids Get Hash nil")
|
||||
return nil, errors.New("redis get hash nil")
|
||||
}
|
||||
|
||||
valueList := value.([]interface{})
|
||||
retList := []string{}
|
||||
var retList []string
|
||||
for _, valueItem := range valueList {
|
||||
valueByte, ok := valueItem.([]byte)
|
||||
if !ok {
|
||||
@@ -572,7 +571,7 @@ func (m *RedisModule) ScanMatchKeys(cursorValue int, redisKey string, count int)
|
||||
nextCursorValue := 0
|
||||
if redisKey == "" {
|
||||
log.Error("ScanMatchKeys key is empty!")
|
||||
return nextCursorValue, nil, errors.New("Key Is Empty")
|
||||
return nextCursorValue, nil, errors.New("key is empty")
|
||||
}
|
||||
|
||||
conn, err := m.getConn()
|
||||
@@ -587,7 +586,7 @@ func (m *RedisModule) ScanMatchKeys(cursorValue int, redisKey string, count int)
|
||||
return nextCursorValue, nil, err
|
||||
}
|
||||
if value == nil {
|
||||
return nextCursorValue, nil, errors.New("Reids Get Hash nil")
|
||||
return nextCursorValue, nil, errors.New("redis get hash nil")
|
||||
}
|
||||
|
||||
valueList := value.([]interface{})
|
||||
@@ -669,7 +668,7 @@ func (m *RedisModule) RPushListJSON(key interface{}, value ...interface{}) error
|
||||
// LPUSH和RPUSH
|
||||
func (m *RedisModule) setListPush(setType string, args ...interface{}) error {
|
||||
if setType != "LPUSH" && setType != "RPUSH" {
|
||||
return errors.New("Redis List Push Type Error,Must Be LPUSH or RPUSH")
|
||||
return errors.New("redis list push type error,must be LPUSH or RPUSH")
|
||||
}
|
||||
conn, err := m.getConn()
|
||||
if err != nil {
|
||||
@@ -697,7 +696,6 @@ func (m *RedisModule) setListJSONPush(setType string, key interface{}, value ...
|
||||
return m.setListPush(setType, args...)
|
||||
}
|
||||
|
||||
// Lrange ...
|
||||
func (m *RedisModule) LRangeList(key string, start, end int) ([]string, error) {
|
||||
conn, err := m.getConn()
|
||||
if err != nil {
|
||||
@@ -714,7 +712,7 @@ func (m *RedisModule) LRangeList(key string, start, end int) ([]string, error) {
|
||||
return redis.Strings(reply, err)
|
||||
}
|
||||
|
||||
// 获取List的长度
|
||||
// GetListLen 获取List的长度
|
||||
func (m *RedisModule) GetListLen(key string) (int, error) {
|
||||
conn, err := m.getConn()
|
||||
if err != nil {
|
||||
@@ -730,7 +728,7 @@ func (m *RedisModule) GetListLen(key string) (int, error) {
|
||||
return redis.Int(reply, err)
|
||||
}
|
||||
|
||||
// 弹出List最后条记录
|
||||
// RPOPListValue 弹出List最后条记录
|
||||
func (m *RedisModule) RPOPListValue(key string) (string, error) {
|
||||
conn, err := m.getConn()
|
||||
if err != nil {
|
||||
@@ -782,7 +780,7 @@ func (m *RedisModule) LRange(key string, start, stop int) ([]byte, error) {
|
||||
return makeListJson(reply.([]interface{}), false), nil
|
||||
}
|
||||
|
||||
// 弹出list(消息队列)数据,数据放入out fromLeft表示是否从左侧弹出 block表示是否阻塞 timeout表示阻塞超时
|
||||
// ListPopJson 弹出list(消息队列)数据,数据放入out fromLeft表示是否从左侧弹出 block表示是否阻塞 timeout表示阻塞超时
|
||||
func (m *RedisModule) ListPopJson(key string, fromLeft, block bool, timeout int, out interface{}) error {
|
||||
b, err := m.ListPop(key, fromLeft, block, timeout)
|
||||
if err != nil {
|
||||
@@ -795,7 +793,7 @@ func (m *RedisModule) ListPopJson(key string, fromLeft, block bool, timeout int,
|
||||
return nil
|
||||
}
|
||||
|
||||
// 弹出list(消息队列)数据 fromLeft表示是否从左侧弹出 block表示是否阻塞 timeout表示阻塞超时
|
||||
// ListPop 弹出list(消息队列)数据 fromLeft表示是否从左侧弹出 block表示是否阻塞 timeout表示阻塞超时
|
||||
func (m *RedisModule) ListPop(key string, fromLeft, block bool, timeout int) ([]byte, error) {
|
||||
cmd := ""
|
||||
if fromLeft {
|
||||
@@ -837,7 +835,7 @@ func (m *RedisModule) ListPop(key string, fromLeft, block bool, timeout int) ([]
|
||||
return b, nil
|
||||
}
|
||||
|
||||
// 有序集合插入Json
|
||||
// ZADDInsertJson 有序集合插入Json
|
||||
func (m *RedisModule) ZADDInsertJson(key string, score float64, value interface{}) error {
|
||||
|
||||
conn, err := m.getConn()
|
||||
@@ -857,7 +855,7 @@ func (m *RedisModule) ZADDInsertJson(key string, score float64, value interface{
|
||||
return nil
|
||||
}
|
||||
|
||||
// 有序集合插入
|
||||
// ZADDInsert 有序集合插入
|
||||
func (m *RedisModule) ZADDInsert(key string, score float64, Data interface{}) error {
|
||||
conn, err := m.getConn()
|
||||
if err != nil {
|
||||
@@ -897,7 +895,7 @@ func (m *RedisModule) ZRangeJSON(key string, start, stop int, ascend bool, withS
|
||||
return nil
|
||||
}
|
||||
|
||||
// 取有序set指定排名 ascend=true表示按升序遍历 否则按降序遍历
|
||||
// ZRange 取有序set指定排名 ascend=true表示按升序遍历 否则按降序遍历
|
||||
func (m *RedisModule) ZRange(key string, start, stop int, ascend bool, withScores bool) ([]byte, error) {
|
||||
conn, err := m.getConn()
|
||||
if err != nil {
|
||||
@@ -921,7 +919,7 @@ func (m *RedisModule) ZRange(key string, start, stop int, ascend bool, withScore
|
||||
return makeListJson(reply.([]interface{}), withScores), nil
|
||||
}
|
||||
|
||||
// 获取有序集合长度
|
||||
// Zcard 获取有序集合长度
|
||||
func (m *RedisModule) Zcard(key string) (int, error) {
|
||||
conn, err := m.getConn()
|
||||
if err != nil {
|
||||
@@ -1005,7 +1003,7 @@ func (m *RedisModule) ZRangeByScore(key string, start, stop float64, ascend bool
|
||||
return makeListJson(reply.([]interface{}), withScores), nil
|
||||
}
|
||||
|
||||
// 获取指定member的排名
|
||||
// ZScore 获取指定member的排名
|
||||
func (m *RedisModule) ZScore(key string, member interface{}) (float64, error) {
|
||||
conn, err := m.getConn()
|
||||
if err != nil {
|
||||
@@ -1021,7 +1019,7 @@ func (m *RedisModule) ZScore(key string, member interface{}) (float64, error) {
|
||||
return redis.Float64(reply, err)
|
||||
}
|
||||
|
||||
// 获取指定member的排名
|
||||
// ZRank 获取指定member的排名
|
||||
func (m *RedisModule) ZRank(key string, member interface{}, ascend bool) (int, error) {
|
||||
conn, err := m.getConn()
|
||||
if err != nil {
|
||||
@@ -1080,7 +1078,7 @@ func (m *RedisModule) ZREMMulti(key string, member ...interface{}) (int, error)
|
||||
|
||||
func (m *RedisModule) HincrbyHashInt(redisKey, hashKey string, value int) error {
|
||||
if redisKey == "" || hashKey == "" {
|
||||
return errors.New("Key Is Empty")
|
||||
return errors.New("key is empty")
|
||||
}
|
||||
conn, err := m.getConn()
|
||||
if err != nil {
|
||||
@@ -1140,7 +1138,7 @@ func (m *RedisModule) Keys(key string) ([]string, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
strs := []string{}
|
||||
var strs []string
|
||||
for _, val := range retList {
|
||||
strVal, ok := val.([]byte)
|
||||
if !ok {
|
||||
|
||||
@@ -20,7 +20,6 @@ var DefaultReadTimeout time.Duration = time.Second * 10
|
||||
var DefaultWriteTimeout time.Duration = time.Second * 10
|
||||
var DefaultProcessTimeout time.Duration = time.Second * 10
|
||||
|
||||
//http redirect
|
||||
type HttpRedirectData struct {
|
||||
Url string
|
||||
CookieList []*http.Cookie
|
||||
@@ -53,7 +52,7 @@ type IHttpRouter interface {
|
||||
POST(url string, handle HttpHandle) bool
|
||||
Router(session *HttpSession)
|
||||
|
||||
SetServeFile(method HTTP_METHOD, urlpath string, dirname string) error
|
||||
SetServeFile(method HTTP_METHOD, urlPath string, dirname string) error
|
||||
SetFormFileKey(formFileKey string)
|
||||
GetFormFileKey() string
|
||||
AddHttpFiltrate(FiltrateFun HttpFiltrate) bool
|
||||
@@ -215,7 +214,7 @@ func (slf *HttpRouter) analysisRouterUrl(url string) (string, error) {
|
||||
//替换所有空格
|
||||
url = strings.ReplaceAll(url, " ", "")
|
||||
if len(url) <= 1 || url[0] != '/' {
|
||||
return "", fmt.Errorf("url %s format is error!", url)
|
||||
return "", fmt.Errorf("url %s format is error", url)
|
||||
}
|
||||
|
||||
//去掉尾部的/
|
||||
@@ -300,12 +299,12 @@ func (httpService *HttpService) SetHttpRouter(httpRouter IHttpRouter, eventHandl
|
||||
httpService.RegEventReceiverFunc(event.Sys_Event_Http_Event, eventHandler, httpService.HttpEventHandler)
|
||||
}
|
||||
|
||||
func (slf *HttpRouter) SetServeFile(method HTTP_METHOD, urlpath string, dirname string) error {
|
||||
func (slf *HttpRouter) SetServeFile(method HTTP_METHOD, urlPath string, dirname string) error {
|
||||
_, err := os.Stat(dirname)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
matchURL, aErr := slf.analysisRouterUrl(urlpath)
|
||||
matchURL, aErr := slf.analysisRouterUrl(urlPath)
|
||||
if aErr != nil {
|
||||
return aErr
|
||||
}
|
||||
@@ -350,15 +349,15 @@ func (slf *HttpSession) redirects() {
|
||||
func (httpService *HttpService) OnInit() error {
|
||||
iConfig := httpService.GetServiceCfg()
|
||||
if iConfig == nil {
|
||||
return fmt.Errorf("%s service config is error!", httpService.GetName())
|
||||
return fmt.Errorf("%s service config is error", httpService.GetName())
|
||||
}
|
||||
httpCfg := iConfig.(map[string]interface{})
|
||||
addr, ok := httpCfg["ListenAddr"]
|
||||
if ok == false {
|
||||
return fmt.Errorf("%s service config is error!", httpService.GetName())
|
||||
return fmt.Errorf("%s service config is error", httpService.GetName())
|
||||
}
|
||||
var readTimeout time.Duration = DefaultReadTimeout
|
||||
var writeTimeout time.Duration = DefaultWriteTimeout
|
||||
var readTimeout = DefaultReadTimeout
|
||||
var writeTimeout = DefaultWriteTimeout
|
||||
|
||||
if cfgRead, ok := httpCfg["ReadTimeout"]; ok == true {
|
||||
readTimeout = time.Duration(cfgRead.(float64)) * time.Millisecond
|
||||
|
||||
@@ -16,7 +16,7 @@ type CustomerSubscriber struct {
|
||||
rpc.IRpcHandler
|
||||
topic string
|
||||
subscriber *Subscriber
|
||||
fromNodeId int
|
||||
fromNodeId string
|
||||
callBackRpcMethod string
|
||||
serviceName string
|
||||
StartIndex uint64
|
||||
@@ -37,7 +37,7 @@ const (
|
||||
MethodLast SubscribeMethod = 1 //Last模式,以该消费者上次记录的位置开始订阅
|
||||
)
|
||||
|
||||
func (cs *CustomerSubscriber) trySetSubscriberBaseInfo(rpcHandler rpc.IRpcHandler, ss *Subscriber, topic string, subscribeMethod SubscribeMethod, customerId string, fromNodeId int, callBackRpcMethod string, startIndex uint64, oneBatchQuantity int32) error {
|
||||
func (cs *CustomerSubscriber) trySetSubscriberBaseInfo(rpcHandler rpc.IRpcHandler, ss *Subscriber, topic string, subscribeMethod SubscribeMethod, customerId string, fromNodeId string, callBackRpcMethod string, startIndex uint64, oneBatchQuantity int32) error {
|
||||
cs.subscriber = ss
|
||||
cs.fromNodeId = fromNodeId
|
||||
cs.callBackRpcMethod = callBackRpcMethod
|
||||
@@ -62,13 +62,13 @@ func (cs *CustomerSubscriber) trySetSubscriberBaseInfo(rpcHandler rpc.IRpcHandle
|
||||
cs.serviceName = strRpcMethod[0]
|
||||
|
||||
if cluster.HasService(fromNodeId, cs.serviceName) == false {
|
||||
err := fmt.Errorf("nodeId %d cannot found %s", fromNodeId, cs.serviceName)
|
||||
err := fmt.Errorf("nodeId %s cannot found %s", fromNodeId, cs.serviceName)
|
||||
log.SError(err.Error())
|
||||
return err
|
||||
}
|
||||
|
||||
if cluster.GetCluster().IsNodeConnected(fromNodeId) == false {
|
||||
err := fmt.Errorf("nodeId %d is disconnect", fromNodeId)
|
||||
err := fmt.Errorf("nodeId %s is disconnect", fromNodeId)
|
||||
log.SError(err.Error())
|
||||
return err
|
||||
}
|
||||
@@ -84,8 +84,8 @@ func (cs *CustomerSubscriber) trySetSubscriberBaseInfo(rpcHandler rpc.IRpcHandle
|
||||
return nil
|
||||
}
|
||||
|
||||
// 开始订阅
|
||||
func (cs *CustomerSubscriber) Subscribe(rpcHandler rpc.IRpcHandler, ss *Subscriber, topic string, subscribeMethod SubscribeMethod, customerId string, fromNodeId int, callBackRpcMethod string, startIndex uint64, oneBatchQuantity int32) error {
|
||||
// Subscribe 开始订阅
|
||||
func (cs *CustomerSubscriber) Subscribe(rpcHandler rpc.IRpcHandler, ss *Subscriber, topic string, subscribeMethod SubscribeMethod, customerId string, fromNodeId string, callBackRpcMethod string, startIndex uint64, oneBatchQuantity int32) error {
|
||||
err := cs.trySetSubscriberBaseInfo(rpcHandler, ss, topic, subscribeMethod, customerId, fromNodeId, callBackRpcMethod, startIndex, oneBatchQuantity)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -96,7 +96,7 @@ func (cs *CustomerSubscriber) Subscribe(rpcHandler rpc.IRpcHandler, ss *Subscrib
|
||||
return nil
|
||||
}
|
||||
|
||||
// 取消订阅
|
||||
// UnSubscribe 取消订阅
|
||||
func (cs *CustomerSubscriber) UnSubscribe() {
|
||||
atomic.StoreInt32(&cs.isStop, 1)
|
||||
}
|
||||
@@ -170,7 +170,7 @@ func (cs *CustomerSubscriber) subscribe() bool {
|
||||
}
|
||||
|
||||
func (cs *CustomerSubscriber) checkCustomerIsValid() bool {
|
||||
//1.检查nodeid是否在线,不在线,直接取消订阅
|
||||
//1.检查nodeId是否在线,不在线,直接取消订阅
|
||||
if cluster.GetCluster().IsNodeConnected(cs.fromNodeId) == false {
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ func (mq *MemoryQueue) Init(cap int32) {
|
||||
mq.topicQueue = make([]TopicData, cap+1)
|
||||
}
|
||||
|
||||
// 从队尾Push数据
|
||||
// Push 从队尾Push数据
|
||||
func (mq *MemoryQueue) Push(topicData *TopicData) bool {
|
||||
mq.locker.Lock()
|
||||
defer mq.locker.Unlock()
|
||||
|
||||
@@ -122,5 +122,5 @@ func (ms *MessageQueueService) RPC_Publish(inParam *rpc.DBQueuePublishReq, outPa
|
||||
|
||||
func (ms *MessageQueueService) RPC_Subscribe(req *rpc.DBQueueSubscribeReq, res *rpc.DBQueueSubscribeRes) error {
|
||||
topicRoom := ms.GetTopicRoom(req.TopicName)
|
||||
return topicRoom.TopicSubscribe(ms.GetRpcHandler(), req.SubType, int32(req.Method), int(req.FromNodeId), req.RpcMethod, req.TopicName, req.CustomerId, req.StartIndex, req.OneBatchQuantity)
|
||||
return topicRoom.TopicSubscribe(ms.GetRpcHandler(), req.SubType, int32(req.Method), req.FromNodeId, req.RpcMethod, req.TopicName, req.CustomerId, req.StartIndex, req.OneBatchQuantity)
|
||||
}
|
||||
|
||||
@@ -201,14 +201,13 @@ func (mp *MongoPersist) PersistTopicData(topic string, topicData []TopicData, re
|
||||
}
|
||||
|
||||
//如果成功
|
||||
return topicData[findPos:len(topicData)], topicData[0:findPos], true
|
||||
return topicData[findPos:], topicData[0:findPos], true
|
||||
}
|
||||
|
||||
// FindTopicData 查找数据
|
||||
func (mp *MongoPersist) findTopicData(topic string, startIndex uint64, limit int64, topicBuff []TopicData) ([]TopicData, bool) {
|
||||
s := mp.mongo.TakeSession()
|
||||
|
||||
|
||||
condition := bson.D{{Key: "_id", Value: bson.D{{Key: "$gt", Value: startIndex}}}}
|
||||
|
||||
var findOption options.FindOptions
|
||||
@@ -238,11 +237,7 @@ func (mp *MongoPersist) findTopicData(topic string, startIndex uint64, limit int
|
||||
defer cancelAll()
|
||||
err = cursor.All(ctxAll, &res)
|
||||
if err != nil {
|
||||
if err != nil {
|
||||
log.SError("find collect name ", topic, " is error:", err.Error())
|
||||
return nil, false
|
||||
}
|
||||
|
||||
log.Error("find collect name ", topic, " is error", log.ErrorAttr("err", err))
|
||||
return nil, false
|
||||
}
|
||||
|
||||
@@ -251,7 +246,7 @@ func (mp *MongoPersist) findTopicData(topic string, startIndex uint64, limit int
|
||||
rawData, errM := bson.Marshal(res[i])
|
||||
if errM != nil {
|
||||
if errM != nil {
|
||||
log.SError("collect name ", topic, " Marshal is error:", err.Error())
|
||||
log.Error("collect name ", topic, " Marshal is error", log.ErrorAttr("err", err))
|
||||
return nil, false
|
||||
}
|
||||
continue
|
||||
@@ -305,9 +300,9 @@ func (mp *MongoPersist) FindTopicData(topic string, startIndex uint64, limit int
|
||||
}
|
||||
|
||||
//从startIndex开始一直往后查
|
||||
topicData, isSucc := mp.findTopicData(topic, startIndex, limit,topicBuff)
|
||||
topicData, ok := mp.findTopicData(topic, startIndex, limit, topicBuff)
|
||||
//有数据或者数据库出错时返回,返回后,会进行下一轮的查询遍历
|
||||
if len(topicData) > 0 || isSucc == false {
|
||||
if len(topicData) > 0 || ok == false {
|
||||
return topicData
|
||||
}
|
||||
|
||||
@@ -411,14 +406,14 @@ func (mp *MongoPersist) PersistIndex(topic string, customerId string, index uint
|
||||
|
||||
condition := bson.D{{Key: "Customer", Value: customerId}, {Key: "Topic", Value: topic}}
|
||||
upsert := bson.M{"Customer": customerId, "Topic": topic, "Index": index}
|
||||
updata := bson.M{"$set": upsert}
|
||||
update := bson.M{"$set": upsert}
|
||||
|
||||
var UpdateOptionsOpts []*options.UpdateOptions
|
||||
UpdateOptionsOpts = append(UpdateOptionsOpts, options.Update().SetUpsert(true))
|
||||
|
||||
ctx, cancel := s.GetDefaultContext()
|
||||
defer cancel()
|
||||
_, err := s.Collection(mp.dbName, CustomerCollectName).UpdateOne(ctx, condition, updata, UpdateOptionsOpts...)
|
||||
_, err := s.Collection(mp.dbName, CustomerCollectName).UpdateOne(ctx, condition, update, UpdateOptionsOpts...)
|
||||
if err != nil {
|
||||
log.SError("PersistIndex fail :", err.Error())
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ import (
|
||||
"sync"
|
||||
)
|
||||
|
||||
// 订阅器
|
||||
// Subscriber 订阅器
|
||||
type Subscriber struct {
|
||||
customerLocker sync.RWMutex
|
||||
mapCustomer map[string]*CustomerSubscriber
|
||||
@@ -31,7 +31,7 @@ func (ss *Subscriber) PersistTopicData(topic string, topics []TopicData, retryCo
|
||||
return ss.dataPersist.PersistTopicData(topic, topics, retryCount)
|
||||
}
|
||||
|
||||
func (ss *Subscriber) TopicSubscribe(rpcHandler rpc.IRpcHandler, subScribeType rpc.SubscribeType, subscribeMethod SubscribeMethod, fromNodeId int, callBackRpcMethod string, topic string, customerId string, StartIndex uint64, oneBatchQuantity int32) error {
|
||||
func (ss *Subscriber) TopicSubscribe(rpcHandler rpc.IRpcHandler, subScribeType rpc.SubscribeType, subscribeMethod SubscribeMethod, fromNodeId string, callBackRpcMethod string, topic string, customerId string, StartIndex uint64, oneBatchQuantity int32) error {
|
||||
//取消订阅时
|
||||
if subScribeType == rpc.SubscribeType_Unsubscribe {
|
||||
ss.UnSubscribe(customerId)
|
||||
|
||||
@@ -43,7 +43,7 @@ type TopicRoom struct {
|
||||
isStop int32
|
||||
}
|
||||
|
||||
// maxProcessTopicBacklogNum:主题最大积压数量
|
||||
// Init maxProcessTopicBacklogNum:主题最大积压数量
|
||||
func (tr *TopicRoom) Init(maxTopicBacklogNum int32, memoryQueueLen int32, topic string, queueWait *sync.WaitGroup, dataPersist QueueDataPersist) {
|
||||
if maxTopicBacklogNum == 0 {
|
||||
maxTopicBacklogNum = DefaultMaxTopicBacklogNum
|
||||
@@ -124,7 +124,7 @@ func (tr *TopicRoom) topicRoomRun() {
|
||||
}
|
||||
|
||||
// 2. 如果存档成功,并且有后续批次,则继续存档
|
||||
if ret == true && len(stagingBuff) > 0 {
|
||||
if len(stagingBuff) > 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
|
||||
@@ -128,7 +128,7 @@ func (mp *MongoPersist) ReadCfg() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
//启服从数据库加载
|
||||
// OnStart 启服从数据库加载
|
||||
func (mp *MongoPersist) OnStart() {
|
||||
}
|
||||
|
||||
@@ -171,7 +171,7 @@ func (mp *MongoPersist) loadFromDB(rankId uint64,rankCollectName string) error{
|
||||
|
||||
rankSkip := mp.mapRankSkip[rankId]
|
||||
if rankSkip == nil {
|
||||
err = fmt.Errorf("rank ", rankCollectName, " is not setup:")
|
||||
err = fmt.Errorf("rank %s is not setup", rankCollectName)
|
||||
log.SError(err.Error())
|
||||
return err
|
||||
}
|
||||
@@ -222,7 +222,6 @@ func (mp *MongoPersist) OnEnterRank(rankSkip IRankSkip, enterData *RankData){
|
||||
mp.mapUpsertRankData[rankSkip.GetRankID()][enterData.Key] = *enterData
|
||||
}
|
||||
|
||||
|
||||
func (mp *MongoPersist) OnLeaveRank(rankSkip IRankSkip, leaveData *RankData) {
|
||||
mp.Lock()
|
||||
defer mp.Unlock()
|
||||
@@ -245,13 +244,13 @@ func (mp *MongoPersist) OnChangeRankData(rankSkip IRankSkip, changeData *RankDat
|
||||
mp.mapUpsertRankData[rankSkip.GetRankID()][changeData.Key] = *changeData
|
||||
}
|
||||
|
||||
//停存持久化到DB
|
||||
// OnStop 停存持久化到DB
|
||||
func (mp *MongoPersist) OnStop(mapRankSkip map[uint64]*RankSkip) {
|
||||
atomic.StoreInt32(&mp.stop, 1)
|
||||
mp.waitGroup.Wait()
|
||||
}
|
||||
|
||||
func (mp *MongoPersist) JugeTimeoutSave() bool{
|
||||
func (mp *MongoPersist) JudgeTimeoutSave() bool {
|
||||
timeout := time.Now()
|
||||
isTimeOut := timeout.Sub(mp.lastSaveTime) >= mp.SaveInterval
|
||||
if isTimeOut == true {
|
||||
@@ -263,7 +262,7 @@ func (mp *MongoPersist) JugeTimeoutSave() bool{
|
||||
|
||||
func (mp *MongoPersist) persistCoroutine() {
|
||||
defer mp.waitGroup.Done()
|
||||
for atomic.LoadInt32(&mp.stop)==0 || mp.hasPersistData(){
|
||||
for atomic.LoadInt32(&mp.stop) == 0 {
|
||||
//间隔时间sleep
|
||||
time.Sleep(time.Second * 1)
|
||||
|
||||
@@ -272,7 +271,7 @@ func (mp *MongoPersist) persistCoroutine(){
|
||||
continue
|
||||
}
|
||||
|
||||
if mp.JugeTimeoutSave() == false{
|
||||
if mp.JudgeTimeoutSave() == false {
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -420,7 +419,6 @@ func (mp *MongoPersist) removeRankDataToDB(mapRemoveRankData map[uint64]map[uint
|
||||
|
||||
mp.removeRankData(rankId, keyList)
|
||||
|
||||
//如果删完,删掉rankid下所有
|
||||
if len(mapRemoveKey) == 0 {
|
||||
delete(mapRemoveRankData, rankId)
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package rankservice
|
||||
|
||||
import (
|
||||
"github.com/duanhf2012/origin/v2/service"
|
||||
"github.com/duanhf2012/origin/v2/rpc"
|
||||
"github.com/duanhf2012/origin/v2/service"
|
||||
)
|
||||
|
||||
type RankDataChangeType int8
|
||||
@@ -17,7 +17,6 @@ type IRankSkip interface {
|
||||
type IRankModule interface {
|
||||
service.IModule
|
||||
|
||||
|
||||
OnSetupRank(manual bool, rankSkip *RankSkip) error //当完成安装排行榜对象时
|
||||
OnStart() //服务开启时回调
|
||||
OnEnterRank(rankSkip IRankSkip, enterData *RankData) //进入排行
|
||||
|
||||
@@ -45,7 +45,7 @@ func (rs *RankService) OnRelease() {
|
||||
rs.rankModule.OnStop(rs.mapRankSkip)
|
||||
}
|
||||
|
||||
// 安装排行模块
|
||||
// SetupRankModule 安装排行模块
|
||||
func (rs *RankService) SetupRankModule(rankModule IRankModule) {
|
||||
rs.rankModule = rankModule
|
||||
}
|
||||
|
||||
@@ -342,7 +342,7 @@ func (rs *RankSkip) GetRankNodeData(findKey uint64) (*RankData, uint64) {
|
||||
return rankNode, index + 1
|
||||
}
|
||||
|
||||
// GetRankNodeDataByPos 获取,返回排名节点与名次
|
||||
// GetRankNodeDataByRank 获取,返回排名节点与名次
|
||||
func (rs *RankSkip) GetRankNodeDataByRank(rank uint64) (*RankData, uint64) {
|
||||
rs.pickExpireKey()
|
||||
rankNode := rs.skipList.ByPosition(rank - 1)
|
||||
@@ -382,7 +382,7 @@ func (rs *RankSkip) GetRankKeyPrevToLimit(findKey, count uint64, result *rpc.Ran
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetRankKeyPrevToLimit 获取key前count名的数据
|
||||
// GetRankKeyNextToLimit 获取key前count名的数据
|
||||
func (rs *RankSkip) GetRankKeyNextToLimit(findKey, count uint64, result *rpc.RankDataList) error {
|
||||
if rs.GetRankLen() <= 0 {
|
||||
return fmt.Errorf("rank[%d] no data", rs.rankId)
|
||||
@@ -411,7 +411,7 @@ func (rs *RankSkip) GetRankKeyNextToLimit(findKey, count uint64, result *rpc.Ran
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetRankList 获取排行榜数据,startPos开始的count个数据
|
||||
// GetRankDataFromToLimit 获取排行榜数据,startPos开始的count个数据
|
||||
func (rs *RankSkip) GetRankDataFromToLimit(startPos, count uint64, result *rpc.RankDataList) error {
|
||||
if rs.GetRankLen() <= 0 {
|
||||
//初始排行榜可能没有数据
|
||||
|
||||
@@ -8,11 +8,11 @@ import (
|
||||
"github.com/duanhf2012/origin/v2/network/processor"
|
||||
"github.com/duanhf2012/origin/v2/service"
|
||||
"github.com/duanhf2012/origin/v2/util/bytespool"
|
||||
"runtime"
|
||||
"sync"
|
||||
"github.com/google/uuid"
|
||||
"time"
|
||||
"runtime"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
type TcpService struct {
|
||||
@@ -25,6 +25,7 @@ type TcpService struct {
|
||||
}
|
||||
|
||||
type TcpPackType int8
|
||||
|
||||
const (
|
||||
TPT_Connected TcpPackType = 0
|
||||
TPT_DisConnected TcpPackType = 1
|
||||
@@ -40,19 +41,19 @@ type TcpPack struct {
|
||||
|
||||
type Client struct {
|
||||
id string
|
||||
tcpConn *network.TCPConn
|
||||
tcpConn *network.NetConn
|
||||
tcpService *TcpService
|
||||
}
|
||||
|
||||
func (tcpService *TcpService) OnInit() error {
|
||||
iConfig := tcpService.GetServiceCfg()
|
||||
if iConfig == nil {
|
||||
return fmt.Errorf("%s service config is error!", tcpService.GetName())
|
||||
return fmt.Errorf("%s service config is error", tcpService.GetName())
|
||||
}
|
||||
tcpCfg := iConfig.(map[string]interface{})
|
||||
addr, ok := tcpCfg["ListenAddr"]
|
||||
if ok == false {
|
||||
return fmt.Errorf("%s service config is error!", tcpService.GetName())
|
||||
return fmt.Errorf("%s service config is error", tcpService.GetName())
|
||||
}
|
||||
|
||||
tcpService.tcpServer.Addr = addr.(string)
|
||||
@@ -94,9 +95,7 @@ func (tcpService *TcpService) OnInit() error{
|
||||
|
||||
tcpService.mapClient = make(map[string]*Client, tcpService.tcpServer.MaxConnNum)
|
||||
tcpService.tcpServer.NewAgent = tcpService.NewClient
|
||||
tcpService.tcpServer.Start()
|
||||
|
||||
return nil
|
||||
return tcpService.tcpServer.Start()
|
||||
}
|
||||
|
||||
func (tcpService *TcpService) TcpEventHandler(ev event.IEvent) {
|
||||
@@ -107,24 +106,27 @@ func (tcpService *TcpService) TcpEventHandler(ev event.IEvent) {
|
||||
case TPT_DisConnected:
|
||||
tcpService.process.DisConnectedRoute(pack.ClientId)
|
||||
case TPT_UnknownPack:
|
||||
tcpService.process.UnknownMsgRoute(pack.ClientId,pack.Data)
|
||||
tcpService.process.UnknownMsgRoute(pack.ClientId, pack.Data, tcpService.recyclerReaderBytes)
|
||||
case TPT_Pack:
|
||||
tcpService.process.MsgRoute(pack.ClientId,pack.Data)
|
||||
tcpService.process.MsgRoute(pack.ClientId, pack.Data, tcpService.recyclerReaderBytes)
|
||||
}
|
||||
}
|
||||
|
||||
func (tcpService *TcpService) recyclerReaderBytes(data []byte) {
|
||||
}
|
||||
|
||||
func (tcpService *TcpService) SetProcessor(process processor.IProcessor, handler event.IEventHandler) {
|
||||
tcpService.process = process
|
||||
tcpService.RegEventReceiverFunc(event.Sys_Event_Tcp, handler, tcpService.TcpEventHandler)
|
||||
}
|
||||
|
||||
func (tcpService *TcpService) NewClient(conn *network.TCPConn) network.Agent {
|
||||
func (tcpService *TcpService) NewClient(conn network.Conn) network.Agent {
|
||||
tcpService.mapClientLocker.Lock()
|
||||
defer tcpService.mapClientLocker.Unlock()
|
||||
|
||||
uuId, _ := uuid.NewUUID()
|
||||
clientId := strings.ReplaceAll(uuId.String(), "-", "")
|
||||
pClient := &Client{tcpConn: conn, id: clientId}
|
||||
pClient := &Client{tcpConn: conn.(*network.NetConn), id: clientId}
|
||||
pClient.tcpService = tcpService
|
||||
tcpService.mapClient[clientId] = pClient
|
||||
|
||||
@@ -179,7 +181,7 @@ func (tcpService *TcpService) SendMsg(clientId string,msg interface{}) error{
|
||||
client, ok := tcpService.mapClient[clientId]
|
||||
if ok == false {
|
||||
tcpService.mapClientLocker.Unlock()
|
||||
return fmt.Errorf("client %d is disconnect!",clientId)
|
||||
return fmt.Errorf("client %s is disconnect", clientId)
|
||||
}
|
||||
|
||||
tcpService.mapClientLocker.Unlock()
|
||||
@@ -206,10 +208,10 @@ func (tcpService *TcpService) Close(clientId string) {
|
||||
return
|
||||
}
|
||||
|
||||
func (tcpService *TcpService) GetClientIp(clientid string) string{
|
||||
func (tcpService *TcpService) GetClientIp(clientId string) string {
|
||||
tcpService.mapClientLocker.Lock()
|
||||
defer tcpService.mapClientLocker.Unlock()
|
||||
pClient,ok := tcpService.mapClient[clientid]
|
||||
pClient, ok := tcpService.mapClient[clientId]
|
||||
if ok == false {
|
||||
return ""
|
||||
}
|
||||
@@ -217,13 +219,12 @@ func (tcpService *TcpService) GetClientIp(clientid string) string{
|
||||
return pClient.tcpConn.GetRemoteIp()
|
||||
}
|
||||
|
||||
|
||||
func (tcpService *TcpService) SendRawMsg(clientId string, msg []byte) error {
|
||||
tcpService.mapClientLocker.Lock()
|
||||
client, ok := tcpService.mapClient[clientId]
|
||||
if ok == false {
|
||||
tcpService.mapClientLocker.Unlock()
|
||||
return fmt.Errorf("client %d is disconnect!",clientId)
|
||||
return fmt.Errorf("client %s is disconnect", clientId)
|
||||
}
|
||||
tcpService.mapClientLocker.Unlock()
|
||||
return client.tcpConn.WriteMsg(msg)
|
||||
@@ -234,13 +235,12 @@ func (tcpService *TcpService) SendRawData(clientId string,data []byte) error{
|
||||
client, ok := tcpService.mapClient[clientId]
|
||||
if ok == false {
|
||||
tcpService.mapClientLocker.Unlock()
|
||||
return fmt.Errorf("client %d is disconnect!",clientId)
|
||||
return fmt.Errorf("client %s is disconnect", clientId)
|
||||
}
|
||||
tcpService.mapClientLocker.Unlock()
|
||||
return client.tcpConn.WriteRawMsg(data)
|
||||
}
|
||||
|
||||
|
||||
func (tcpService *TcpService) GetConnNum() int {
|
||||
tcpService.mapClientLocker.Lock()
|
||||
connNum := len(tcpService.mapClient)
|
||||
@@ -248,14 +248,14 @@ func (tcpService *TcpService) GetConnNum() int {
|
||||
return connNum
|
||||
}
|
||||
|
||||
func (server *TcpService) SetNetMempool(mempool bytespool.IBytesMempool){
|
||||
server.tcpServer.SetNetMempool(mempool)
|
||||
func (tcpService *TcpService) SetNetMemPool(memPool bytespool.IBytesMemPool) {
|
||||
tcpService.tcpServer.SetNetMemPool(memPool)
|
||||
}
|
||||
|
||||
func (server *TcpService) GetNetMempool() bytespool.IBytesMempool {
|
||||
return server.tcpServer.GetNetMempool()
|
||||
func (tcpService *TcpService) GetNetMemPool() bytespool.IBytesMemPool {
|
||||
return tcpService.tcpServer.GetNetMemPool()
|
||||
}
|
||||
|
||||
func (server *TcpService) ReleaseNetMem(byteBuff []byte) {
|
||||
server.tcpServer.GetNetMempool().ReleaseBytes(byteBuff)
|
||||
func (tcpService *TcpService) ReleaseNetMem(byteBuff []byte) {
|
||||
tcpService.tcpServer.GetNetMemPool().ReleaseBytes(byteBuff)
|
||||
}
|
||||
|
||||
@@ -7,9 +7,9 @@ import (
|
||||
"github.com/duanhf2012/origin/v2/network"
|
||||
"github.com/duanhf2012/origin/v2/network/processor"
|
||||
"github.com/duanhf2012/origin/v2/service"
|
||||
"sync"
|
||||
"github.com/google/uuid"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type WSService struct {
|
||||
@@ -22,6 +22,7 @@ type WSService struct {
|
||||
}
|
||||
|
||||
type WSPackType int8
|
||||
|
||||
const (
|
||||
WPT_Connected WSPackType = 0
|
||||
WPT_DisConnected WSPackType = 1
|
||||
@@ -50,12 +51,12 @@ func (ws *WSService) OnInit() error{
|
||||
|
||||
iConfig := ws.GetServiceCfg()
|
||||
if iConfig == nil {
|
||||
return fmt.Errorf("%s service config is error!", ws.GetName())
|
||||
return fmt.Errorf("%s service config is error", ws.GetName())
|
||||
}
|
||||
wsCfg := iConfig.(map[string]interface{})
|
||||
addr, ok := wsCfg["ListenAddr"]
|
||||
if ok == false {
|
||||
return fmt.Errorf("%s service config is error!", ws.GetName())
|
||||
return fmt.Errorf("%s service config is error", ws.GetName())
|
||||
}
|
||||
|
||||
ws.wsServer.Addr = addr.(string)
|
||||
@@ -79,8 +80,7 @@ func (ws *WSService) OnInit() error{
|
||||
|
||||
ws.mapClient = make(map[string]*WSClient, ws.wsServer.MaxConnNum)
|
||||
ws.wsServer.NewAgent = ws.NewWSClient
|
||||
ws.wsServer.Start()
|
||||
return nil
|
||||
return ws.wsServer.Start()
|
||||
}
|
||||
|
||||
func (ws *WSService) SetMessageType(messageType int) {
|
||||
@@ -95,9 +95,9 @@ func (ws *WSService) WSEventHandler(ev event.IEvent) {
|
||||
case WPT_DisConnected:
|
||||
pack.MsgProcessor.DisConnectedRoute(pack.ClientId)
|
||||
case WPT_UnknownPack:
|
||||
pack.MsgProcessor.UnknownMsgRoute(pack.ClientId,pack.Data)
|
||||
pack.MsgProcessor.UnknownMsgRoute(pack.ClientId, pack.Data, ws.recyclerReaderBytes)
|
||||
case WPT_Pack:
|
||||
pack.MsgProcessor.MsgRoute(pack.ClientId,pack.Data)
|
||||
pack.MsgProcessor.MsgRoute(pack.ClientId, pack.Data, ws.recyclerReaderBytes)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -129,7 +129,7 @@ func (slf *WSClient) Run() {
|
||||
for {
|
||||
bytes, err := slf.wsConn.ReadMsg()
|
||||
if err != nil {
|
||||
log.Debug("read client id %d is error:%+v",slf.id,err)
|
||||
log.Debug("read client id %s is error:%+v", slf.id, err)
|
||||
break
|
||||
}
|
||||
data, err := slf.wsService.process.Unmarshal(slf.id, bytes)
|
||||
@@ -148,27 +148,27 @@ func (slf *WSClient) OnClose(){
|
||||
delete(slf.wsService.mapClient, slf.GetId())
|
||||
}
|
||||
|
||||
func (ws *WSService) SendMsg(clientid string,msg interface{}) error{
|
||||
func (ws *WSService) SendMsg(clientId string, msg interface{}) error {
|
||||
ws.mapClientLocker.Lock()
|
||||
client,ok := ws.mapClient[clientid]
|
||||
client, ok := ws.mapClient[clientId]
|
||||
if ok == false {
|
||||
ws.mapClientLocker.Unlock()
|
||||
return fmt.Errorf("client %d is disconnect!",clientid)
|
||||
return fmt.Errorf("client %s is disconnect", clientId)
|
||||
}
|
||||
|
||||
ws.mapClientLocker.Unlock()
|
||||
bytes,err := ws.process.Marshal(clientid,msg)
|
||||
bytes, err := ws.process.Marshal(clientId, msg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return client.wsConn.WriteMsg(bytes)
|
||||
}
|
||||
|
||||
func (ws *WSService) Close(clientid string) {
|
||||
func (ws *WSService) Close(clientId string) {
|
||||
ws.mapClientLocker.Lock()
|
||||
defer ws.mapClientLocker.Unlock()
|
||||
|
||||
client,ok := ws.mapClient[clientid]
|
||||
client, ok := ws.mapClient[clientId]
|
||||
if ok == false {
|
||||
return
|
||||
}
|
||||
@@ -180,3 +180,5 @@ func (ws *WSService) Close(clientid string) {
|
||||
return
|
||||
}
|
||||
|
||||
func (ws *WSService) recyclerReaderBytes(data []byte) {
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ import (
|
||||
func NewAesEncrypt(key string) (aes *AesEncrypt, err error) {
|
||||
keyLen := len(key)
|
||||
if keyLen < 16 {
|
||||
err = fmt.Errorf("The length of res key shall not be less than 16")
|
||||
err = fmt.Errorf("the length of res key shall not be less than 16")
|
||||
return
|
||||
}
|
||||
aes = &AesEncrypt{
|
||||
@@ -22,12 +22,12 @@ type AesEncrypt struct {
|
||||
StrKey string
|
||||
}
|
||||
|
||||
func (this *AesEncrypt) getKey() []byte {
|
||||
keyLen := len(this.StrKey)
|
||||
func (ae *AesEncrypt) getKey() []byte {
|
||||
keyLen := len(ae.StrKey)
|
||||
if keyLen < 16 {
|
||||
panic("The length of res key shall not be less than 16")
|
||||
}
|
||||
arrKey := []byte(this.StrKey)
|
||||
arrKey := []byte(ae.StrKey)
|
||||
if keyLen >= 32 {
|
||||
//取前32个字节
|
||||
return arrKey[:32]
|
||||
@@ -40,33 +40,33 @@ func (this *AesEncrypt) getKey() []byte {
|
||||
return arrKey[:16]
|
||||
}
|
||||
|
||||
//加密字符串
|
||||
func (this *AesEncrypt) Encrypt(strMesg string) ([]byte, error) {
|
||||
key := this.getKey()
|
||||
var iv = []byte(key)[:aes.BlockSize]
|
||||
encrypted := make([]byte, len(strMesg))
|
||||
// Encrypt 加密字符串
|
||||
func (ae *AesEncrypt) Encrypt(str string) ([]byte, error) {
|
||||
key := ae.getKey()
|
||||
var iv = key[:aes.BlockSize]
|
||||
encrypted := make([]byte, len(str))
|
||||
aesBlockEncrypter, err := aes.NewCipher(key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
aesEncrypter := cipher.NewCFBEncrypter(aesBlockEncrypter, iv)
|
||||
aesEncrypter.XORKeyStream(encrypted, []byte(strMesg))
|
||||
aesEncrypter.XORKeyStream(encrypted, []byte(str))
|
||||
return encrypted, nil
|
||||
}
|
||||
|
||||
//解密字符串
|
||||
func (this *AesEncrypt) Decrypt(src []byte) (strDesc string, err error) {
|
||||
// Decrypt 解密字符串
|
||||
func (ae *AesEncrypt) Decrypt(src []byte) (strDesc string, err error) {
|
||||
defer func() {
|
||||
//错误处理
|
||||
if e := recover(); e != nil {
|
||||
err = e.(error)
|
||||
}
|
||||
}()
|
||||
key := this.getKey()
|
||||
var iv = []byte(key)[:aes.BlockSize]
|
||||
key := ae.getKey()
|
||||
var iv = key[:aes.BlockSize]
|
||||
decrypted := make([]byte, len(src))
|
||||
var aesBlockDecrypter cipher.Block
|
||||
aesBlockDecrypter, err = aes.NewCipher([]byte(key))
|
||||
aesBlockDecrypter, err = aes.NewCipher(key)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
@@ -42,7 +42,7 @@ func setBitTagByIndex[Number BitNumber, UNumber UnsignedNumber](bitBuff []Number
|
||||
func GetBitwiseTag[Number BitNumber, UNumber UnsignedNumber](bitBuff []Number, bitPositionIndex UNumber) (bool, error) {
|
||||
sliceIndex, sliceBitIndex, ret := getBitTagIndex(bitBuff, bitPositionIndex)
|
||||
if ret == false {
|
||||
return false, errors.New("Invalid parameter")
|
||||
return false, errors.New("invalid parameter")
|
||||
}
|
||||
|
||||
return (bitBuff[sliceIndex] & (1 << sliceBitIndex)) > 0, nil
|
||||
|
||||
@@ -4,7 +4,7 @@ import (
|
||||
"sync"
|
||||
)
|
||||
|
||||
type IBytesMempool interface {
|
||||
type IBytesMemPool interface {
|
||||
MakeBytes(size int) []byte
|
||||
ReleaseBytes(byteBuff []byte) bool
|
||||
}
|
||||
|
||||
@@ -39,7 +39,7 @@ func Go(callback interface{}, args ...interface{}) {
|
||||
go F(callback, 0, args...)
|
||||
}
|
||||
|
||||
//-1表示一直恢复
|
||||
// GoRecover -1表示一直恢复
|
||||
func GoRecover(callback interface{}, recoverNum int, args ...interface{}) {
|
||||
go F(callback, recoverNum, args...)
|
||||
}
|
||||
@@ -1,129 +0,0 @@
|
||||
package math
|
||||
|
||||
import (
|
||||
"github.com/duanhf2012/origin/v2/log"
|
||||
)
|
||||
|
||||
type NumberType interface {
|
||||
int | int8 | int16 | int32 | int64 | float32 | float64 | uint | uint8 | uint16 | uint32 | uint64
|
||||
}
|
||||
|
||||
type SignedNumberType interface {
|
||||
int | int8 | int16 | int32 | int64 | float32 | float64
|
||||
}
|
||||
|
||||
type FloatType interface {
|
||||
float32 | float64
|
||||
}
|
||||
|
||||
func Max[NumType NumberType](number1 NumType, number2 NumType) NumType {
|
||||
if number1 > number2 {
|
||||
return number1
|
||||
}
|
||||
|
||||
return number2
|
||||
}
|
||||
|
||||
func Min[NumType NumberType](number1 NumType, number2 NumType) NumType {
|
||||
if number1 < number2 {
|
||||
return number1
|
||||
}
|
||||
|
||||
return number2
|
||||
}
|
||||
|
||||
func Abs[NumType SignedNumberType](Num NumType) NumType {
|
||||
if Num < 0 {
|
||||
return -1 * Num
|
||||
}
|
||||
|
||||
return Num
|
||||
}
|
||||
|
||||
func AddSafe[NumType NumberType](number1 NumType, number2 NumType) (NumType, bool) {
|
||||
ret := number1 + number2
|
||||
if number2 > 0 && ret < number1 {
|
||||
log.Stack("Calculation overflow", log.Any("number1", number1), log.Any("number2", number2))
|
||||
return ret, false
|
||||
} else if number2 < 0 && ret > number1 {
|
||||
log.Stack("Calculation overflow", log.Any("number1", number1), log.Any("number2", number2))
|
||||
return ret, false
|
||||
}
|
||||
|
||||
return ret, true
|
||||
}
|
||||
|
||||
func SubSafe[NumType NumberType](number1 NumType, number2 NumType) (NumType, bool) {
|
||||
ret := number1 - number2
|
||||
if number2 > 0 && ret > number1 {
|
||||
log.Stack("Calculation overflow", log.Any("number1", number1), log.Any("number2", number2))
|
||||
return ret, false
|
||||
} else if number2 < 0 && ret < number1 {
|
||||
log.Stack("Calculation overflow", log.Any("number1", number1), log.Any("number2", number2))
|
||||
return ret, false
|
||||
}
|
||||
|
||||
return ret, true
|
||||
}
|
||||
|
||||
func MulSafe[NumType NumberType](number1 NumType, number2 NumType) (NumType, bool) {
|
||||
ret := number1 * number2
|
||||
if number1 == 0 || number2 == 0 {
|
||||
return ret, true
|
||||
}
|
||||
|
||||
if ret/number2 == number1 {
|
||||
return ret, true
|
||||
}
|
||||
|
||||
log.Stack("Calculation overflow", log.Any("number1", number1), log.Any("number2", number2))
|
||||
return ret, true
|
||||
}
|
||||
|
||||
func Add[NumType NumberType](number1 NumType, number2 NumType) NumType {
|
||||
ret, _ := AddSafe(number1, number2)
|
||||
return ret
|
||||
}
|
||||
|
||||
func Sub[NumType NumberType](number1 NumType, number2 NumType) NumType {
|
||||
ret, _ := SubSafe(number1, number2)
|
||||
return ret
|
||||
}
|
||||
|
||||
func Mul[NumType NumberType](number1 NumType, number2 NumType) NumType {
|
||||
ret, _ := MulSafe(number1, number2)
|
||||
return ret
|
||||
}
|
||||
|
||||
// 安全的求比例
|
||||
func PercentRateSafe[NumType NumberType, OutNumType NumberType](maxValue int64, rate NumType, numbers ...NumType) (OutNumType, bool) {
|
||||
// 比例不能为负数
|
||||
if rate < 0 {
|
||||
log.Stack("rate must not positive")
|
||||
return 0, false
|
||||
}
|
||||
|
||||
if rate == 0 {
|
||||
// 比例为0
|
||||
return 0, true
|
||||
}
|
||||
|
||||
ret := int64(rate)
|
||||
for _, number := range numbers {
|
||||
number64 := int64(number)
|
||||
result, success := MulSafe(number64, ret)
|
||||
if !success {
|
||||
// 基数*比例越界了,int64都越界了,没办法了
|
||||
return 0, false
|
||||
}
|
||||
|
||||
ret = result
|
||||
}
|
||||
|
||||
ret = ret / 10000
|
||||
if ret > maxValue {
|
||||
return 0, false
|
||||
}
|
||||
|
||||
return OutNumType(ret), true
|
||||
}
|
||||
173
util/pattern/pubsub/pubsub.go
Normal file
173
util/pattern/pubsub/pubsub.go
Normal file
@@ -0,0 +1,173 @@
|
||||
package pubsub
|
||||
|
||||
import (
|
||||
"container/list"
|
||||
"sync/atomic"
|
||||
)
|
||||
|
||||
type TopicType int
|
||||
type Key uint64
|
||||
|
||||
type IBaseSubscriber interface {
|
||||
OnSubscribe(key Key)
|
||||
GetKey() Key
|
||||
}
|
||||
|
||||
type ISubscriber interface {
|
||||
IBaseSubscriber
|
||||
OnEvent(ctx ...any)
|
||||
}
|
||||
|
||||
type IPublisher interface {
|
||||
Publish(topic TopicType, ctx ...any)
|
||||
Subscribe(topic TopicType, sub ISubscriber)
|
||||
UnSubscribe(topic TopicType)
|
||||
UnSubscribeKey(key Key)
|
||||
}
|
||||
|
||||
var keyID uint64
|
||||
|
||||
func genKeyID() Key {
|
||||
return Key(atomic.AddUint64(&keyID, 1))
|
||||
}
|
||||
|
||||
type KeyData struct {
|
||||
subscriber ISubscriber
|
||||
topicType TopicType
|
||||
keyElement *list.Element
|
||||
}
|
||||
|
||||
type SubscriberSet map[Key]KeyData
|
||||
type TopicSet map[TopicType]*list.List
|
||||
|
||||
type Publisher struct {
|
||||
subscriberSet SubscriberSet
|
||||
topicSet TopicSet
|
||||
}
|
||||
|
||||
func (set *SubscriberSet) init() {
|
||||
*set = make(SubscriberSet, 64)
|
||||
}
|
||||
|
||||
func (set *SubscriberSet) add(keyElement *list.Element, topicType TopicType, subscriber ISubscriber) {
|
||||
(*set)[keyElement.Value.(Key)] = KeyData{subscriber: subscriber, topicType: topicType, keyElement: keyElement}
|
||||
}
|
||||
|
||||
func (set *SubscriberSet) del(key Key) {
|
||||
delete(*set, key)
|
||||
}
|
||||
|
||||
func (set *SubscriberSet) get(key Key) (KeyData, bool) {
|
||||
keyData, ok := (*set)[key]
|
||||
if !ok {
|
||||
return keyData, false
|
||||
}
|
||||
|
||||
return keyData, true
|
||||
}
|
||||
|
||||
func (set *TopicSet) init() {
|
||||
*set = make(TopicSet, 64)
|
||||
}
|
||||
|
||||
func (set *TopicSet) add(topic TopicType, key Key) *list.Element {
|
||||
keyList := (*set)[topic]
|
||||
if keyList == nil {
|
||||
keyList = list.New()
|
||||
(*set)[topic] = keyList
|
||||
}
|
||||
|
||||
return keyList.PushBack(key)
|
||||
}
|
||||
|
||||
func (set *TopicSet) del(topic TopicType, keyElement *list.Element) {
|
||||
keyList := (*set)[topic]
|
||||
if keyList == nil {
|
||||
return
|
||||
}
|
||||
|
||||
keyList.Remove(keyElement)
|
||||
}
|
||||
|
||||
func (set *TopicSet) foreach(topic TopicType, cb func(key Key)) {
|
||||
keyList := (*set)[topic]
|
||||
if keyList == nil {
|
||||
return
|
||||
}
|
||||
for e := keyList.Front(); e != nil; e = e.Next() {
|
||||
cb(e.Value.(Key))
|
||||
}
|
||||
}
|
||||
|
||||
type BaseSubscriber struct {
|
||||
key Key
|
||||
}
|
||||
|
||||
func (bs *BaseSubscriber) OnSubscribe(key Key) {
|
||||
bs.key = key
|
||||
}
|
||||
|
||||
func (bs *BaseSubscriber) GetKey() Key {
|
||||
return bs.key
|
||||
}
|
||||
|
||||
func (pub *Publisher) lazyInit() {
|
||||
if pub.subscriberSet == nil {
|
||||
pub.subscriberSet.init()
|
||||
}
|
||||
if pub.topicSet == nil {
|
||||
pub.topicSet.init()
|
||||
}
|
||||
}
|
||||
|
||||
func (pub *Publisher) add(topic TopicType, sub ISubscriber) Key {
|
||||
key := genKeyID()
|
||||
ele := pub.topicSet.add(topic, key)
|
||||
pub.subscriberSet.add(ele, topic, sub)
|
||||
|
||||
return key
|
||||
}
|
||||
|
||||
func (pub *Publisher) Publish(topic TopicType, ctx ...any) {
|
||||
pub.lazyInit()
|
||||
pub.topicSet.foreach(topic, func(key Key) {
|
||||
keyData, ok := pub.subscriberSet.get(key)
|
||||
if ok == false {
|
||||
return
|
||||
}
|
||||
keyData.subscriber.OnEvent(ctx...)
|
||||
})
|
||||
}
|
||||
|
||||
func (pub *Publisher) Subscribe(topic TopicType, sub ISubscriber) bool {
|
||||
if topic == 0 {
|
||||
return false
|
||||
}
|
||||
|
||||
pub.lazyInit()
|
||||
sub.OnSubscribe(pub.add(topic, sub))
|
||||
return true
|
||||
}
|
||||
|
||||
func (pub *Publisher) UnSubscribe(topic TopicType) {
|
||||
keyList := pub.topicSet[topic]
|
||||
if keyList == nil {
|
||||
return
|
||||
}
|
||||
|
||||
for e := keyList.Front(); e != nil; e = e.Next() {
|
||||
pub.subscriberSet.del(e.Value.(Key))
|
||||
}
|
||||
|
||||
delete(pub.topicSet, topic)
|
||||
}
|
||||
|
||||
func (pub *Publisher) UnSubscribeKey(key Key) {
|
||||
keyData, ok := pub.subscriberSet.get(key)
|
||||
if ok == false {
|
||||
return
|
||||
}
|
||||
|
||||
pub.topicSet.del(keyData.topicType, keyData.keyElement)
|
||||
pub.subscriberSet.del(key)
|
||||
}
|
||||
54
util/pattern/pubsub/pubsub_test.go
Normal file
54
util/pattern/pubsub/pubsub_test.go
Normal file
@@ -0,0 +1,54 @@
|
||||
package pubsub
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
const (
|
||||
Invalid TopicType = iota
|
||||
Topic1
|
||||
Topic2
|
||||
)
|
||||
|
||||
var test *testing.T
|
||||
|
||||
type Subscriber1 struct {
|
||||
BaseSubscriber
|
||||
}
|
||||
|
||||
type Subscriber2 struct {
|
||||
BaseSubscriber
|
||||
}
|
||||
|
||||
func (sub *Subscriber1) OnEvent(ctx ...any) {
|
||||
test.Log("Subscriber1 OnEvent", " key ", sub.GetKey(), ctx)
|
||||
}
|
||||
|
||||
func (sub *Subscriber2) OnEvent(ctx ...any) {
|
||||
test.Log("Subscriber2 OnEvent", " key ", sub.GetKey(), ctx)
|
||||
}
|
||||
|
||||
func TestPubSub(t *testing.T) {
|
||||
test = t
|
||||
var publisher Publisher
|
||||
|
||||
// 创建3个订阅者
|
||||
var subscriber []ISubscriber
|
||||
subscriber = append(subscriber, &Subscriber1{}, &Subscriber1{}, &Subscriber2{})
|
||||
|
||||
// 分别注册进Publisher中
|
||||
publisher.Subscribe(Topic1, subscriber[0])
|
||||
publisher.Subscribe(Topic1, subscriber[1])
|
||||
publisher.Subscribe(Topic2, subscriber[2])
|
||||
|
||||
// 发布订阅,两个Subscriber1都会调用OnEvent
|
||||
publisher.Publish(Topic1, 1, 2, 3)
|
||||
|
||||
// 删除订阅,Publish后,只有Subscriber1的key2收到
|
||||
publisher.UnSubscribeKey(subscriber[0].GetKey())
|
||||
publisher.Publish(Topic1, 1, 2, 3)
|
||||
|
||||
// 删除Topic2,Publish将收不到
|
||||
publisher.UnSubscribe(Topic2)
|
||||
publisher.Publish(Topic2, 1)
|
||||
}
|
||||
@@ -1,91 +0,0 @@
|
||||
package rand
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"time"
|
||||
)
|
||||
|
||||
func init() {
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
}
|
||||
|
||||
func RandGroup(p ...uint32) int {
|
||||
if p == nil {
|
||||
panic("args not found")
|
||||
}
|
||||
|
||||
r := make([]uint32, len(p))
|
||||
for i := 0; i < len(p); i++ {
|
||||
if i == 0 {
|
||||
r[0] = p[0]
|
||||
} else {
|
||||
r[i] = r[i-1] + p[i]
|
||||
}
|
||||
}
|
||||
|
||||
rl := r[len(r)-1]
|
||||
if rl == 0 {
|
||||
return 0
|
||||
}
|
||||
|
||||
rn := uint32(rand.Int63n(int64(rl)))
|
||||
for i := 0; i < len(r); i++ {
|
||||
if rn < r[i] {
|
||||
return i
|
||||
}
|
||||
}
|
||||
|
||||
panic("bug")
|
||||
}
|
||||
|
||||
func RandInterval(b1, b2 int32) int32 {
|
||||
if b1 == b2 {
|
||||
return b1
|
||||
}
|
||||
|
||||
min, max := int64(b1), int64(b2)
|
||||
if min > max {
|
||||
min, max = max, min
|
||||
}
|
||||
return int32(rand.Int63n(max-min+1) + min)
|
||||
}
|
||||
|
||||
func RandIntervalN(b1, b2 int32, n uint32) []int32 {
|
||||
if b1 == b2 {
|
||||
return []int32{b1}
|
||||
}
|
||||
|
||||
min, max := int64(b1), int64(b2)
|
||||
if min > max {
|
||||
min, max = max, min
|
||||
}
|
||||
l := max - min + 1
|
||||
if int64(n) > l {
|
||||
n = uint32(l)
|
||||
}
|
||||
|
||||
r := make([]int32, n)
|
||||
m := make(map[int32]int32)
|
||||
for i := uint32(0); i < n; i++ {
|
||||
v := int32(rand.Int63n(l) + min)
|
||||
|
||||
if mv, ok := m[v]; ok {
|
||||
r[i] = mv
|
||||
} else {
|
||||
r[i] = v
|
||||
}
|
||||
|
||||
lv := int32(l - 1 + min)
|
||||
if v != lv {
|
||||
if mv, ok := m[lv]; ok {
|
||||
m[v] = mv
|
||||
} else {
|
||||
m[v] = lv
|
||||
}
|
||||
}
|
||||
|
||||
l--
|
||||
}
|
||||
|
||||
return r
|
||||
}
|
||||
85
util/smath/smath.go
Normal file
85
util/smath/smath.go
Normal file
@@ -0,0 +1,85 @@
|
||||
package smath
|
||||
|
||||
import (
|
||||
"github.com/duanhf2012/origin/v2/log"
|
||||
"github.com/duanhf2012/origin/v2/util/typ"
|
||||
)
|
||||
|
||||
func Max[NumType typ.Number](number1 NumType, number2 NumType) NumType {
|
||||
if number1 > number2 {
|
||||
return number1
|
||||
}
|
||||
|
||||
return number2
|
||||
}
|
||||
|
||||
func Min[NumType typ.Number](number1 NumType, number2 NumType) NumType {
|
||||
if number1 < number2 {
|
||||
return number1
|
||||
}
|
||||
|
||||
return number2
|
||||
}
|
||||
|
||||
func Abs[NumType typ.Signed | typ.Float](Num NumType) NumType {
|
||||
if Num < 0 {
|
||||
return -1 * Num
|
||||
}
|
||||
|
||||
return Num
|
||||
}
|
||||
|
||||
func AddSafe[NumType typ.Number](number1 NumType, number2 NumType) (NumType, bool) {
|
||||
ret := number1 + number2
|
||||
if number2 > 0 && ret < number1 {
|
||||
log.Stack("Calculation overflow", log.Any("number1", number1), log.Any("number2", number2))
|
||||
return ret, false
|
||||
} else if number2 < 0 && ret > number1 {
|
||||
log.Stack("Calculation overflow", log.Any("number1", number1), log.Any("number2", number2))
|
||||
return ret, false
|
||||
}
|
||||
|
||||
return ret, true
|
||||
}
|
||||
|
||||
func SubSafe[NumType typ.Number](number1 NumType, number2 NumType) (NumType, bool) {
|
||||
ret := number1 - number2
|
||||
if number2 > 0 && ret > number1 {
|
||||
log.Stack("Calculation overflow", log.Any("number1", number1), log.Any("number2", number2))
|
||||
return ret, false
|
||||
} else if number2 < 0 && ret < number1 {
|
||||
log.Stack("Calculation overflow", log.Any("number1", number1), log.Any("number2", number2))
|
||||
return ret, false
|
||||
}
|
||||
|
||||
return ret, true
|
||||
}
|
||||
|
||||
func MulSafe[NumType typ.Number](number1 NumType, number2 NumType) (NumType, bool) {
|
||||
ret := number1 * number2
|
||||
if number1 == 0 || number2 == 0 {
|
||||
return ret, true
|
||||
}
|
||||
|
||||
if ret/number2 == number1 {
|
||||
return ret, true
|
||||
}
|
||||
|
||||
log.Stack("Calculation overflow", log.Any("number1", number1), log.Any("number2", number2))
|
||||
return ret, true
|
||||
}
|
||||
|
||||
func Add[NumType typ.Number](number1 NumType, number2 NumType) NumType {
|
||||
ret, _ := AddSafe(number1, number2)
|
||||
return ret
|
||||
}
|
||||
|
||||
func Sub[NumType typ.Number](number1 NumType, number2 NumType) NumType {
|
||||
ret, _ := SubSafe(number1, number2)
|
||||
return ret
|
||||
}
|
||||
|
||||
func Mul[NumType typ.Number](number1 NumType, number2 NumType) NumType {
|
||||
ret, _ := MulSafe(number1, number2)
|
||||
return ret
|
||||
}
|
||||
108
util/srand/slice.go
Normal file
108
util/srand/slice.go
Normal file
@@ -0,0 +1,108 @@
|
||||
package srand
|
||||
|
||||
import (
|
||||
"github.com/duanhf2012/origin/v2/util/typ"
|
||||
"math/rand"
|
||||
"slices"
|
||||
)
|
||||
|
||||
func Sum[E ~[]T, T typ.Number](arr E) T {
|
||||
var sum T
|
||||
for i := range arr {
|
||||
sum += arr[i]
|
||||
}
|
||||
return sum
|
||||
}
|
||||
|
||||
func SumFunc[E ~[]V, V any, T typ.Number](arr E, getValue func(i int) T) T {
|
||||
var sum T
|
||||
for i := range arr {
|
||||
sum += getValue(i)
|
||||
}
|
||||
return sum
|
||||
}
|
||||
|
||||
func Shuffle[E ~[]T, T any](arr E) {
|
||||
rand.Shuffle(len(arr), func(i, j int) {
|
||||
arr[i], arr[j] = arr[j], arr[i]
|
||||
})
|
||||
}
|
||||
|
||||
func RandOne[E ~[]T, T any](arr E) T {
|
||||
return arr[rand.Intn(len(arr))]
|
||||
}
|
||||
|
||||
func RandN[E ~[]T, T any](arr E, num int) []T {
|
||||
index := make([]int, 0, len(arr))
|
||||
for i := range arr {
|
||||
index = append(index, i)
|
||||
}
|
||||
Shuffle(index)
|
||||
if len(index) > num {
|
||||
index = index[:num]
|
||||
}
|
||||
ret := make([]T, 0, len(index))
|
||||
for i := range index {
|
||||
ret = append(ret, arr[index[i]])
|
||||
}
|
||||
|
||||
return ret
|
||||
}
|
||||
|
||||
func RandWeight[E ~[]T, T typ.Integer](weights E) int {
|
||||
totalWeight := Sum(weights)
|
||||
if totalWeight <= 0 {
|
||||
return -1
|
||||
}
|
||||
|
||||
t := T(rand.Intn(int(totalWeight)))
|
||||
for i := range weights {
|
||||
if t < weights[i] {
|
||||
return i
|
||||
}
|
||||
t -= weights[i]
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
func RandWeightFunc[E ~[]U, U any, T typ.Integer](arr E, getWeight func(i int) T) int {
|
||||
weights := make([]T, 0, len(arr))
|
||||
for i := range arr {
|
||||
weights = append(weights, getWeight(i))
|
||||
}
|
||||
return RandWeight(weights)
|
||||
}
|
||||
|
||||
func Get[E ~[]T, T any, U typ.Integer](arr E, index U) (ret T, ok bool) {
|
||||
if index < 0 || int(index) >= len(arr) {
|
||||
return
|
||||
}
|
||||
ret = arr[index]
|
||||
ok = true
|
||||
return
|
||||
}
|
||||
|
||||
func GetPointer[E ~[]T, T any, U typ.Integer](arr E, index U) *T {
|
||||
if index < 0 || int(index) >= len(arr) {
|
||||
return nil
|
||||
}
|
||||
return &arr[index]
|
||||
}
|
||||
|
||||
func GetFunc[E ~[]T, T any](arr E, f func(T) bool) (ret T, ok bool) {
|
||||
index := slices.IndexFunc(arr, f)
|
||||
if index < 0 {
|
||||
return
|
||||
}
|
||||
|
||||
return arr[index], true
|
||||
}
|
||||
|
||||
func GetPointerFunc[E ~[]T, T any](arr E, f func(T) bool) *T {
|
||||
index := slices.IndexFunc(arr, f)
|
||||
if index < 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
return &arr[index]
|
||||
}
|
||||
@@ -28,8 +28,6 @@ func (pool *Pool) Get() interface{}{
|
||||
default:
|
||||
return pool.syncPool.Get()
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (pool *Pool) Put(data interface{}) {
|
||||
@@ -94,5 +92,3 @@ func (pool *PoolEx) Put(data IPoolData){
|
||||
pool.syncPool.Put(data)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -6,11 +6,10 @@ import (
|
||||
"github.com/duanhf2012/origin/v2/util/sync"
|
||||
"reflect"
|
||||
"runtime"
|
||||
"time"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
)
|
||||
|
||||
// ITimer
|
||||
type ITimer interface {
|
||||
GetId() uint64
|
||||
Cancel()
|
||||
@@ -27,7 +26,6 @@ type ITimer interface {
|
||||
type OnCloseTimer func(timer ITimer)
|
||||
type OnAddTimer func(timer ITimer)
|
||||
|
||||
// Timer
|
||||
type Timer struct {
|
||||
Id uint64
|
||||
cancelled int32 //是否关闭
|
||||
@@ -46,12 +44,10 @@ type Timer struct {
|
||||
ref bool
|
||||
}
|
||||
|
||||
// Ticker
|
||||
type Ticker struct {
|
||||
Timer
|
||||
}
|
||||
|
||||
// Cron
|
||||
type Cron struct {
|
||||
Timer
|
||||
}
|
||||
@@ -101,7 +97,6 @@ func releaseCron(cron *Cron) {
|
||||
cronPool.Put(cron)
|
||||
}
|
||||
|
||||
// one dispatcher per goroutine (goroutine not safe)
|
||||
type Dispatcher struct {
|
||||
ChanTimer chan ITimer
|
||||
}
|
||||
@@ -175,7 +170,7 @@ func (t *Timer) Cancel() {
|
||||
atomic.StoreInt32(&t.cancelled, 1)
|
||||
}
|
||||
|
||||
// 判断定时器是否已经取消
|
||||
// IsActive 判断定时器是否已经取消
|
||||
func (t *Timer) IsActive() bool {
|
||||
return atomic.LoadInt32(&t.cancelled) == 0
|
||||
}
|
||||
|
||||
54
util/typ/type.go
Normal file
54
util/typ/type.go
Normal file
@@ -0,0 +1,54 @@
|
||||
package typ
|
||||
|
||||
import "errors"
|
||||
|
||||
type Signed interface {
|
||||
~int | ~int8 | ~int16 | ~int32 | ~int64
|
||||
}
|
||||
|
||||
type Unsigned interface {
|
||||
~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64
|
||||
}
|
||||
|
||||
type Float interface {
|
||||
~float32 | ~float64
|
||||
}
|
||||
|
||||
type Integer interface {
|
||||
Signed | Unsigned
|
||||
}
|
||||
|
||||
type Number interface {
|
||||
Signed | Unsigned | Float
|
||||
}
|
||||
|
||||
type Ordered interface {
|
||||
Number | Float | ~string
|
||||
}
|
||||
|
||||
func ConvertToNumber[DType Number](val interface{}) (DType, error) {
|
||||
switch val.(type) {
|
||||
case int64:
|
||||
return DType(val.(int64)), nil
|
||||
case int:
|
||||
return DType(val.(int)), nil
|
||||
case uint:
|
||||
return DType(val.(uint)), nil
|
||||
case uint64:
|
||||
return DType(val.(uint64)), nil
|
||||
case float32:
|
||||
return DType(val.(float32)), nil
|
||||
case float64:
|
||||
return DType(val.(float64)), nil
|
||||
case int32:
|
||||
return DType(val.(int32)), nil
|
||||
case uint32:
|
||||
return DType(val.(uint32)), nil
|
||||
case int16:
|
||||
return DType(val.(int16)), nil
|
||||
case uint16:
|
||||
return DType(val.(uint16)), nil
|
||||
}
|
||||
|
||||
return 0, errors.New("unsupported type")
|
||||
}
|
||||
Reference in New Issue
Block a user