Compare commits

...

5 Commits

Author SHA1 Message Date
ayflying
366ddb45ea 修改导表工具,修改websocket协议的钩子 2025-04-09 16:50:54 +08:00
ayflying
09a9f14a29 修改ExcelTime2Time方法 2025-04-09 11:47:07 +08:00
ayflying
e0afb55bb2 随机算法小修改 2025-04-08 18:43:37 +08:00
ayflying
a469692358 修改随机方法 2025-04-02 16:20:03 +08:00
ayflying
bce7131d9f 开启qps 2025-04-02 15:55:43 +08:00
8 changed files with 125 additions and 48 deletions

View File

@@ -38,13 +38,14 @@ func init() {
// @receiver s *sIp2region: sIp2region的实例。 // @receiver s *sIp2region: sIp2region的实例。
func (s *sIp2region) Load() { func (s *sIp2region) Load() {
var err error var err error
var url = "https://github.com/ayflying/resource/raw/refs/heads/main/attachment/ip2region.xdb"
var dbPath = "runtime/library/ip2region.xdb" var dbPath = "runtime/library/ip2region.xdb"
if gfile.IsEmpty(dbPath) { if gfile.IsEmpty(dbPath) {
g.Log().Debug(ctx, "等待下载ip库文件") g.Log().Debug(ctx, "等待下载ip库文件")
//下载文件 //下载文件
putData, err2 := g.Client().Discovery(nil). putData, err2 := g.Client().Discovery(nil).Get(ctx, url)
Get(ctx, "https://resource.luoe.cn/attachment/ip2region.xdb")
if err2 != nil { if err2 != nil {
return return
} }

View File

@@ -111,8 +111,12 @@ func (s *Excel) RemoveComments(list []interface{}, json []string) []interface{}
// 遍历当前元素的每个键值对 // 遍历当前元素的每个键值对
for _, v3 := range v2.(g.Map) { for _, v3 := range v2.(g.Map) {
// 如果字符串中存在//则跳过不写入temp // 如果字符串中存在//则跳过不写入temp
if gstr.Contains(gconv.String(v3), "//") { //if gstr.Contains(gconv.String(v3), "//") {
//delKey = append(delKey, k2) // //delKey = append(delKey, k2)
// add = false
// break
//}
if strings.HasPrefix(gconv.String(v3), "//") {
add = false add = false
break break
} }

View File

@@ -1,9 +1,15 @@
package aycache package aycache
import ( import (
v1 "github.com/ayflying/utility_go/api/system/v1"
"github.com/ayflying/utility_go/internal/boot"
"github.com/ayflying/utility_go/pkg/aycache/drive" "github.com/ayflying/utility_go/pkg/aycache/drive"
drive2 "github.com/ayflying/utility_go/pkg/aycache/drive" drive2 "github.com/ayflying/utility_go/pkg/aycache/drive"
"github.com/ayflying/utility_go/service"
"github.com/gogf/gf/v2/os/gcache" "github.com/gogf/gf/v2/os/gcache"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"math"
) )
type Mod struct { type Mod struct {
@@ -12,23 +18,23 @@ type Mod struct {
var ( var (
QPSCount int QPSCount int
//QPS = promauto.NewGauge( QPS = promauto.NewGauge(
// prometheus.GaugeOpts{ prometheus.GaugeOpts{
// Name: "Cache_QPS", Name: "Cache_QPS",
// Help: "当前缓存QPS数量", Help: "当前缓存QPS数量",
// }, },
//) )
) )
func init() { func init() {
//boot.AddFunc(func() { boot.AddFunc(func() {
// //初始化指标 //初始化指标
// service.SystemCron().AddCron(v1.CronType_MINUTE, func() error { service.SystemCron().AddCron(v1.CronType_MINUTE, func() error {
// QPS.Set(math.Round(float64(QPSCount) / 60)) QPS.Set(math.Round(float64(QPSCount) / 60))
// QPSCount = 0 QPSCount = 0
// return nil return nil
// }) })
//}) })
} }

View File

@@ -1,16 +1,26 @@
package websocket package websocket
import "context"
// 定义一个处理方法的类型 // 定义一个处理方法的类型
type Handler func(ctx context.Context, req any) (err error) type Handler func(conn *WebsocketData, req any) (err error)
type OnConnectHandler func(conn *WebsocketData)
// 路由器的处理映射 // 路由器的处理映射
var ( var (
handlers = make(map[int]Handler) handlers = make(map[int]Handler)
OnConnectHandlers = make([]OnConnectHandler, 0)
OnCloseHandlers = make([]OnConnectHandler, 0)
) )
// 注册方法将某个消息路由器ID和对应的处理方法关联起来 // 注册方法将某个消息路由器ID和对应的处理方法关联起来
func (s *SocketV1) RegisterRouter(cmd int, handler Handler) { func (s *SocketV1) RegisterRouter(cmd int, handler Handler) {
handlers[cmd] = handler handlers[cmd] = handler
} }
//注册方法,讲长连接登陆方法进行注册
func (s *SocketV1) RegisterOnConnect(_func OnConnectHandler) {
OnConnectHandlers = append(OnConnectHandlers, _func)
}
func (s *SocketV1) RegisterOnClose(_func OnConnectHandler) {
OnCloseHandlers = append(OnCloseHandlers, _func)
}

View File

@@ -4,16 +4,18 @@ import (
"context" "context"
"github.com/gogf/gf/v2/container/gmap" "github.com/gogf/gf/v2/container/gmap"
"github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gctx" "github.com/gogf/gf/v2/net/ghttp"
"github.com/gogf/gf/v2/os/glog" "github.com/gogf/gf/v2/os/glog"
"github.com/gogf/gf/v2/util/gconv" "github.com/gogf/gf/v2/util/gconv"
"github.com/google/uuid" "github.com/google/uuid"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
"strconv"
"sync" "sync"
"github.com/gogf/gf/v2/net/ghttp"
) )
type SocketV1 struct{} type SocketV1 struct {
Type int `json:"type"`
}
var ( var (
//ctx = gctx.New() //ctx = gctx.New()
@@ -31,7 +33,9 @@ type WebsocketData struct {
} }
func NewV1() *SocketV1 { func NewV1() *SocketV1 {
return &SocketV1{} return &SocketV1{
Type: 1,
}
} }
type SocketInterface interface { type SocketInterface interface {
@@ -79,7 +83,6 @@ func (s *SocketV1) OnConnect(ctx context.Context, conn *websocket.Conn) {
defer conn.Close() defer conn.Close()
id, _ := uuid.NewUUID() id, _ := uuid.NewUUID()
ip := conn.RemoteAddr().String() ip := conn.RemoteAddr().String()
data := &WebsocketData{ data := &WebsocketData{
Uuid: id, Uuid: id,
Ws: conn, Ws: conn,
@@ -92,6 +95,11 @@ func (s *SocketV1) OnConnect(ctx context.Context, conn *websocket.Conn) {
//to := fmt.Sprintf("创建连接:%v,ip=%v", id, ip) //to := fmt.Sprintf("创建连接:%v,ip=%v", id, ip)
//s.Send(id, []byte(to)) //s.Send(id, []byte(to))
//用户登录钩子执行
for _, connect := range OnConnectHandlers {
connect(data)
}
for { for {
//进入当前连接线程拥堵 //进入当前连接线程拥堵
msgType, msg, err := conn.ReadMessage() msgType, msg, err := conn.ReadMessage()
@@ -102,7 +110,7 @@ func (s *SocketV1) OnConnect(ctx context.Context, conn *websocket.Conn) {
s.OnMessage(m.Get(id).(*WebsocketData), msg, msgType) s.OnMessage(m.Get(id).(*WebsocketData), msg, msgType)
} }
//关闭连接触发 //关闭连接触发
s.OnClose(id, conn) s.OnClose(data)
g.Log().Debugf(ctx, "断开连接:uuid=%v,ip=%v", id, ip) g.Log().Debugf(ctx, "断开连接:uuid=%v,ip=%v", id, ip)
} }
@@ -113,21 +121,23 @@ func (s *SocketV1) OnConnect(ctx context.Context, conn *websocket.Conn) {
// @param msg // @param msg
// @param msgType // @param msgType
func (s *SocketV1) OnMessage(conn *WebsocketData, req []byte, msgType int) { func (s *SocketV1) OnMessage(conn *WebsocketData, req []byte, msgType int) {
s.Type = msgType
//g.Log().Debugf(ctx, "收到消息:%v,type=%v,conn=%v", string(req), msgType, conn) //g.Log().Debugf(ctx, "收到消息:%v,type=%v,conn=%v", string(req), msgType, conn)
//s.Send(conn.Uuid, msg) //s.Send(conn.Uuid, msg)
//s.SendAll(msg) //s.SendAll(msg)
msgStr := string(req) //msgStr := string(req)
msg := msgStr[8:] msg := req[8:]
cmd := gconv.Int(msgStr[:8]) cmd := gconv.Int(req[:8])
//GetRouter(cmd, conn.Uid, msg) //GetRouter(cmd, conn.Uid, msg)
handler, exist := handlers[cmd] handler, exist := handlers[cmd]
if exist { if exist {
//匹配上路由器 //匹配上路由器
handler(conn.Ctx, msg) err := handler(conn, msg)
g.Log().Error(conn.Ctx, err)
} else { } else {
//fmt.Println("未注册的路由器ID:", cmd) //fmt.Println("未注册的路由器ID:", cmd)
s.Send(conn.Uuid, []byte("未注册的协议号:"+msgStr[:8])) s.Send(conn.Uuid, []byte("未注册的协议号:"+strconv.Itoa(cmd)))
s.OnClose(conn.Uuid, conn.Ws) s.OnClose(conn)
return return
} }
@@ -146,7 +156,7 @@ func (s *SocketV1) Send(id uuid.UUID, data []byte) (err error) {
} }
conn := m.Get(id).(*WebsocketData) conn := m.Get(id).(*WebsocketData)
conn.Ws.WriteMessage(1, data) conn.Ws.WriteMessage(s.Type, data)
return return
} }
@@ -156,7 +166,8 @@ func (s *SocketV1) SendAll(data []byte) {
m.Iterator(func(k interface{}, v interface{}) bool { m.Iterator(func(k interface{}, v interface{}) bool {
//fmt.Printf("%v:%v ", k, v) //fmt.Printf("%v:%v ", k, v)
conn := v.(*WebsocketData) conn := v.(*WebsocketData)
conn.Ws.WriteMessage(1, data) conn.Ws.WriteMessage(s.Type, data)
return true return true
}) })
} }
@@ -166,14 +177,19 @@ func (s *SocketV1) SendAll(data []byte) {
// @Description: // @Description:
// @receiver s // @receiver s
// @param conn // @param conn
func (s *SocketV1) OnClose(id uuid.UUID, conn *websocket.Conn) { func (s *SocketV1) OnClose(conn *WebsocketData) {
// 在此处编写断开连接后的处理逻辑 // 在此处编写断开连接后的处理逻辑
g.Log().Debugf(gctx.New(), "WebSocket connection from %s has been closed.", conn.RemoteAddr()) //g.Log().Debugf(gctx.New(), "WebSocket connection from %s has been closed.", conn.RemoteAddr())
//用户登录钩子执行
for _, connect := range OnCloseHandlers {
connect(conn)
}
// 可能的后续操作: // 可能的后续操作:
// 1. 更新连接状态或从连接池移除 // 1. 更新连接状态或从连接池移除
// 2. 发送通知或清理关联资源 // 2. 发送通知或清理关联资源
// 3. 执行特定于业务的断开处理 // 3. 执行特定于业务的断开处理
m.Remove(id) m.Remove(conn.Uuid)
conn.Close() conn.Ws.Close()
} }

View File

@@ -53,12 +53,52 @@ func (m *randMod) RandomAll(data map[int]int, n int) []int {
return result return result
} }
func RandomAll[t Any](data map[t]int, n int) []t {
if n > len(data) {
n = len(data)
}
rand.Seed(time.Now().UnixNano())
// 复制权重映射,避免修改原始数据
remainingWeights := make(map[t]int)
for k, v := range data {
remainingWeights[k] = v
}
result := make([]t, 0, n)
for i := 0; i < n; i++ {
totalWeight := 0
// 计算剩余元素的总权重
for _, weight := range remainingWeights {
totalWeight += weight
}
if totalWeight == 0 {
break
}
// 生成一个 0 到总权重之间的随机数
randomNum := rand.Intn(totalWeight)
currentWeight := 0
for key, weight := range remainingWeights {
currentWeight += weight
if randomNum < currentWeight {
// 将选中的元素添加到结果切片中
result = append(result, key)
// 从剩余权重映射中移除选中的元素
delete(remainingWeights, key)
break
}
}
}
return result
}
// RandByArrInt 根据传入的 interface 切片中的整数值按权重随机返回一个索引 // RandByArrInt 根据传入的 interface 切片中的整数值按权重随机返回一个索引
// 参数 s: 一个包含整数的 interface 切片,切片中的每个元素代表一个权重 // 参数 s: 一个包含整数的 interface 切片,切片中的每个元素代表一个权重
// 返回值: 随机选中的元素的索引 // 返回值: 随机选中的元素的索引
func RandByArrInt(s []interface{}) int { func (m *randMod) RandByArrInt(_s interface{}) int {
// 初始化总权重为 0 // 初始化总权重为 0
sv := 0 sv := 0
s := gconv.Ints(_s)
// 遍历切片,累加每个元素的权重 // 遍历切片,累加每个元素的权重
for i := range s { for i := range s {
sv += gconv.Int(s[i]) sv += gconv.Int(s[i])
@@ -73,7 +113,7 @@ func RandByArrInt(s []interface{}) int {
var all int var all int
// 再次遍历切片,累加权重 // 再次遍历切片,累加权重
for i := range s { for i := range s {
all += gconv.Int(s[i]) all += s[i]
// 如果当前累加的权重大于随机数,则返回当前索引 // 如果当前累加的权重大于随机数,则返回当前索引
if all > r { if all > r {
return i return i

View File

@@ -124,7 +124,7 @@ func (m *timeMod) GetDailyTimeList(time1 time.Time, time2 time.Time) (timeList [
// ExcelTime2Time excel时间转时间 (12/10/24 02:03转为时间) // ExcelTime2Time excel时间转时间 (12/10/24 02:03转为时间)
func (m *timeMod) ExcelTime2Time(excelTime string) time.Time { func (m *timeMod) ExcelTime2Time(excelTime string) time.Time {
layout := "01/02/06 15:04" // 月/日/年(最后两位) 小时:分钟 (24小时制) layout := "1/2/06 15:04" // 月/日/年(最后两位) 小时:分钟 (24小时制)
timeNew, _ := time.ParseInLocation(layout, excelTime, time.Local) timeNew, _ := time.ParseInLocation(layout, excelTime, time.Local)
return timeNew return timeNew
} }

View File

@@ -20,9 +20,9 @@ type Number interface {
int | int64 | int32 | int16 | uint64 | uint32 | uint16 | float32 | float64 int | int64 | int32 | int16 | uint64 | uint32 | uint16 | float32 | float64
} }
//type Any interface { type Any interface {
// interface{} | string | int | int64 | int32 | int16 | uint64 | uint32 | uint16 | float32 | float64 string | int | int64 | int32 | int16 | uint64 | uint32 | uint16 | float32 | float64
//} }
type toolsInterface interface { type toolsInterface interface {
Load() Load()
@@ -132,7 +132,7 @@ func RemoveSlice[t Number](slice []t, value ...t) []t {
// 从后向前遍历切片 // 从后向前遍历切片
for i := len(slice) - 1; i >= 0; i-- { for i := len(slice) - 1; i >= 0; i-- {
// 检查当前元素是否等于需要移除的值 // 检查当前元素是否等于需要移除的值
if InArray(slice[i], value) { if InArray[t](value, slice[i]) {
// 如果相等,移除该元素 // 如果相等,移除该元素
// 使用append和切片操作符来实现移除操作将i之前和i之后的元素合并到一起 // 使用append和切片操作符来实现移除操作将i之前和i之后的元素合并到一起
slice = append(slice[:i], slice[i+1:]...) slice = append(slice[:i], slice[i+1:]...)
@@ -149,7 +149,7 @@ func RemoveSlice[t Number](slice []t, value ...t) []t {
// @param value 需要查找的值 // @param value 需要查找的值
// @param array 进行查找的切片 // @param array 进行查找的切片
// @return bool 返回是否存在 // @return bool 返回是否存在
func InArray[t Number](value t, array []t) bool { func InArray[t Number](array []t, value t) bool {
for _, v := range array { for _, v := range array {
if v == value { if v == value {
return true return true