Skip to content

Commit

Permalink
Beta457 (#457)
Browse files Browse the repository at this point in the history
* beta447

* beta448

* beta449

* beta450

* beta451

* beta452

* beta453

* beta454

* beta455

* btea455

* beta456

* beta457
  • Loading branch information
Hoshinonyaruko authored Jul 9, 2024
1 parent b7ed658 commit 6e47571
Showing 1 changed file with 53 additions and 120 deletions.
173 changes: 53 additions & 120 deletions echo/echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,34 +13,19 @@ import (
)

type EchoMapping struct {
mu sync.Mutex
msgTypeMapping map[string]string
msgIDMapping map[string]string
eventIDMapping map[string]string
msgTypeMapping sync.Map
msgIDMapping sync.Map
eventIDMapping sync.Map
}

// Int64ToIntMapping 用于存储 int64 到 int 的映射(递归计数器)
type Int64ToIntMapping struct {
mu sync.Mutex
mapping map[int64]int
mapping sync.Map
}

// IntToStringMappingSeq 用于存储 string 到 int 的映射(seq对应)
// StringToIntMappingSeq 用于存储 string 到 int 的映射(seq对应)
type StringToIntMappingSeq struct {
mu sync.Mutex
mapping map[string]int
}

// StringToInt64MappingSeq 用于存储 string 到 int64 的映射(file接口频率限制)
type StringToInt64MappingSeq struct {
mu sync.Mutex
mapping map[string]int64
}

// Int64Stack 用于存储 int64 的栈
type Int64Stack struct {
mu sync.Mutex
stack []int64
mapping sync.Map
}

// MessageGroupPair 用于存储 group 和 groupMessage
Expand Down Expand Up @@ -68,27 +53,18 @@ var globalMessageGroupStack = &globalMessageGroup{
stack: make([]MessageGroupPair, 0),
}

// 定义一个全局的 Int64Stack 实例
var globalInt64Stack = &Int64Stack{
stack: make([]int64, 0),
}

var globalEchoMapping = &EchoMapping{
msgTypeMapping: make(map[string]string),
msgIDMapping: make(map[string]string),
eventIDMapping: make(map[string]string),
msgTypeMapping: sync.Map{},
msgIDMapping: sync.Map{},
eventIDMapping: sync.Map{},
}

var globalInt64ToIntMapping = &Int64ToIntMapping{
mapping: make(map[int64]int),
mapping: sync.Map{},
}

var globalStringToIntMappingSeq = &StringToIntMappingSeq{
mapping: make(map[string]int),
}

var globalStringToInt64MappingSeq = &StringToInt64MappingSeq{
mapping: make(map[string]int64),
mapping: sync.Map{},
}

func (e *EchoMapping) GenerateKey(appid string, s int64) string {
Expand All @@ -107,146 +83,103 @@ func (e *EchoMapping) GenerateKeyv3(appid string, s string) string {
return appid + "_" + s
}

// 添加echo对应的类型
// 添加 echo 对应的类型
func AddMsgType(appid string, s int64, msgType string) {
key := globalEchoMapping.GenerateKey(appid, s)
globalEchoMapping.mu.Lock()
defer globalEchoMapping.mu.Unlock()
globalEchoMapping.msgTypeMapping[key] = msgType
globalEchoMapping.msgTypeMapping.Store(key, msgType)
}

// 添加echo对应的messageid
func AddMsgIDv3(appid string, s string, msgID string) {
key := globalEchoMapping.GenerateKeyv3(appid, s)
globalEchoMapping.mu.Lock()
defer globalEchoMapping.mu.Unlock()
globalEchoMapping.msgIDMapping[key] = msgID
globalEchoMapping.msgIDMapping.Store(key, msgID)
}

// GetMsgIDv3 返回给定appid和s的msgID
func GetMsgIDv3(appid string, s string) string {
key := globalEchoMapping.GenerateKeyv3(appid, s)
globalEchoMapping.mu.Lock()
defer globalEchoMapping.mu.Unlock()

return globalEchoMapping.msgIDMapping[key]
value, ok := globalEchoMapping.msgIDMapping.Load(key)
if !ok {
return "" // 或者根据需要返回默认值或者错误处理
}
return value.(string)
}

// 添加group和userid对应的messageid
func AddMsgIDv2(appid string, groupid int64, userid int64, msgID string) {
key := globalEchoMapping.GenerateKeyv2(appid, groupid, userid)
globalEchoMapping.mu.Lock()
defer globalEchoMapping.mu.Unlock()
globalEchoMapping.msgIDMapping[key] = msgID
globalEchoMapping.msgIDMapping.Store(key, msgID)
}

// 添加group对应的eventid
func AddEvnetID(appid string, groupid int64, eventID string) {
key := globalEchoMapping.GenerateKeyEventID(appid, groupid)
globalEchoMapping.mu.Lock()
defer globalEchoMapping.mu.Unlock()
globalEchoMapping.eventIDMapping[key] = eventID
globalEchoMapping.eventIDMapping.Store(key, eventID)
}

// 添加echo对应的messageid
func AddMsgID(appid string, s int64, msgID string) {
key := globalEchoMapping.GenerateKey(appid, s)
globalEchoMapping.mu.Lock()
defer globalEchoMapping.mu.Unlock()
globalEchoMapping.msgIDMapping[key] = msgID
globalEchoMapping.msgIDMapping.Store(key, msgID)
}

// 根据给定的key获取消息类型
func GetMsgTypeByKey(key string) string {
globalEchoMapping.mu.Lock()
defer globalEchoMapping.mu.Unlock()
return globalEchoMapping.msgTypeMapping[key]
value, _ := globalEchoMapping.msgTypeMapping.Load(key)
if value == nil {
return "" // 根据需要返回默认值或者进行错误处理
}
return value.(string)
}

// 根据给定的key获取消息ID
func GetMsgIDByKey(key string) string {
globalEchoMapping.mu.Lock()
defer globalEchoMapping.mu.Unlock()
return globalEchoMapping.msgIDMapping[key]
value, _ := globalEchoMapping.msgIDMapping.Load(key)
if value == nil {
return "" // 根据需要返回默认值或者进行错误处理
}
return value.(string)
}

// 根据给定的key获取EventID
func GetEventIDByKey(key string) string {
globalEchoMapping.mu.Lock()
defer globalEchoMapping.mu.Unlock()
return globalEchoMapping.eventIDMapping[key]
value, _ := globalEchoMapping.eventIDMapping.Load(key)
if value == nil {
return "" // 根据需要返回默认值或者进行错误处理
}
return value.(string)
}

// AddMapping 添加一个新的映射
func AddMapping(key int64, value int) {
globalInt64ToIntMapping.mu.Lock()
defer globalInt64ToIntMapping.mu.Unlock()
globalInt64ToIntMapping.mapping[key] = value
globalInt64ToIntMapping.mapping.Store(key, value)
}

// GetMapping 根据给定的 int64 键获取映射值
func GetMapping(key int64) int {
globalInt64ToIntMapping.mu.Lock()
defer globalInt64ToIntMapping.mu.Unlock()
return globalInt64ToIntMapping.mapping[key]
value, _ := globalInt64ToIntMapping.mapping.Load(key)
if value == nil {
return 0 // 根据需要返回默认值或者进行错误处理
}
return value.(int)
}

// AddMapping 添加一个新的映射
// AddMappingSeq 添加一个新的映射
func AddMappingSeq(key string, value int) {
globalStringToIntMappingSeq.mu.Lock()
defer globalStringToIntMappingSeq.mu.Unlock()
globalStringToIntMappingSeq.mapping[key] = value
globalStringToIntMappingSeq.mapping.Store(key, value)
}

// GetMappingSeq 根据给定的 string 键获取映射值
func GetMappingSeq(key string) int {
if config.GetRamDomSeq() {
rng := rand.New(rand.NewSource(time.Now().UnixNano()))
return rng.Intn(10000) + 1 // 生成 1 到 10000 的随机数
} else {
globalStringToIntMappingSeq.mu.Lock()
defer globalStringToIntMappingSeq.mu.Unlock()
return globalStringToIntMappingSeq.mapping[key]
}
}

// AddMapping 添加一个新的映射
func AddMappingFileTimeLimit(key string, value int64) {
globalStringToInt64MappingSeq.mu.Lock()
defer globalStringToInt64MappingSeq.mu.Unlock()
globalStringToInt64MappingSeq.mapping[key] = value
}

// GetMapping 根据给定的 string 键获取映射值
func GetMappingFileTimeLimit(key string) int64 {
globalStringToInt64MappingSeq.mu.Lock()
defer globalStringToInt64MappingSeq.mu.Unlock()
return globalStringToInt64MappingSeq.mapping[key]
}

// Add 添加一个新的 int64 到全局栈中
func AddFileTimeLimit(value int64) {
globalInt64Stack.mu.Lock()
defer globalInt64Stack.mu.Unlock()

// 添加新元素到栈顶
globalInt64Stack.stack = append(globalInt64Stack.stack, value)

// 如果栈的大小超过 10,移除最早添加的元素
if len(globalInt64Stack.stack) > 10 {
globalInt64Stack.stack = globalInt64Stack.stack[1:]
}
}

// Get 获取全局栈顶的元素
func GetFileTimeLimit() int64 {
globalInt64Stack.mu.Lock()
defer globalInt64Stack.mu.Unlock()

if len(globalInt64Stack.stack) == 0 {
return 0 // 当栈为空时返回 0
value, ok := globalStringToIntMappingSeq.mapping.Load(key)
if !ok {
if config.GetRamDomSeq() {
rng := rand.New(rand.NewSource(time.Now().UnixNano()))
return rng.Intn(10000) + 1 // 生成 1 到 10000 的随机数
}
return 0 // 或者根据需要返回默认值或者进行错误处理
}
return globalInt64Stack.stack[len(globalInt64Stack.stack)-1]
return value.(int)
}

// PushGlobalStack 向全局栈中添加一个新的 MessageGroupPair
Expand Down

0 comments on commit 6e47571

Please sign in to comment.