From 743d232c38e38c77bf038136ac026ec63f5ec8d6 Mon Sep 17 00:00:00 2001 From: ayflying Date: Wed, 2 Apr 2025 15:19:49 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=8D=95=E5=85=83=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=EF=BC=8C=E7=BB=9F=E4=B8=80=E5=88=9D=E5=A7=8B=E5=8C=96?= =?UTF-8?q?=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- drivers/db/elasticsearch/load.go | 2 +- drivers/db/found/found.go | 21 +++++----- go.mod | 3 ++ go.sum | 2 + internal/boot/boot.go | 36 ++++++++++++----- internal/logic/gameAct/gameAct.go | 4 +- pkg/aycache/cache.go | 13 ------ tools/tools.go | 17 ++++---- utility.go | 15 ++++--- utility_test.go | 66 +++++++++++++++++++++++++++++++ 10 files changed, 133 insertions(+), 46 deletions(-) create mode 100644 utility_test.go diff --git a/drivers/db/elasticsearch/load.go b/drivers/db/elasticsearch/load.go index dce2ba4..f2ec5f6 100644 --- a/drivers/db/elasticsearch/load.go +++ b/drivers/db/elasticsearch/load.go @@ -14,7 +14,7 @@ const ( quoteChar = "`" ) -func init() { +func Init() { var ( err error driverObj = New() diff --git a/drivers/db/found/found.go b/drivers/db/found/found.go index 565f78a..bae25ae 100644 --- a/drivers/db/found/found.go +++ b/drivers/db/found/found.go @@ -2,6 +2,7 @@ package found import ( "database/sql" + "github.com/ayflying/utility_go/internal/boot" "github.com/gogf/gf/v2/database/gdb" "github.com/gogf/gf/v2/frame/g" ) @@ -21,16 +22,18 @@ const ( ) func init() { - var ( - err error - driverObj = New() - driverNames = g.SliceStr{"es", "found"} - ) - for _, driverName := range driverNames { - if err = gdb.Register(driverName, driverObj); err != nil { - panic(err) + boot.AddFunc(func() { + var ( + err error + driverObj = New() + driverNames = g.SliceStr{"es", "found"} + ) + for _, driverName := range driverNames { + if err = gdb.Register(driverName, driverObj); err != nil { + panic(err) + } } - } + }) } // New create and returns a driver that implements gdb.Driver, which supports operations for MySQL. diff --git a/go.mod b/go.mod index 5df7c97..f2b03c0 100644 --- a/go.mod +++ b/go.mod @@ -17,6 +17,7 @@ require ( github.com/lionsoul2014/ip2region/binding/golang v0.0.0-20241220152942-06eb5c6e8230 github.com/minio/minio-go/v7 v7.0.85 github.com/prometheus/client_golang v1.21.1 + github.com/stretchr/testify v1.10.0 github.com/xuri/excelize/v2 v2.9.0 golang.org/x/oauth2 v0.24.0 google.golang.org/api v0.44.0 @@ -31,6 +32,7 @@ require ( github.com/beorn7/perks v1.0.1 // indirect github.com/cespare/xxhash/v2 v2.3.0 // indirect github.com/clbanning/mxj/v2 v2.7.0 // indirect + github.com/davecgh/go-spew v1.1.1 // indirect github.com/dustin/go-humanize v1.0.1 // indirect github.com/elastic/elastic-transport-go/v8 v8.6.1 // indirect github.com/emirpasic/gods v1.18.1 // indirect @@ -60,6 +62,7 @@ require ( github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect github.com/olekukonko/tablewriter v0.0.5 // indirect github.com/pelletier/go-toml v1.9.3 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect github.com/prometheus/client_model v0.6.1 // indirect github.com/prometheus/common v0.62.0 // indirect github.com/prometheus/procfs v0.15.1 // indirect diff --git a/go.sum b/go.sum index 5020eb4..0d81b06 100644 --- a/go.sum +++ b/go.sum @@ -249,6 +249,8 @@ 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/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc= +github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw= github.com/lionsoul2014/ip2region/binding/golang v0.0.0-20241220152942-06eb5c6e8230 h1:B0oaMTAQKDZd8cwYT0qsAI7+c3KbFeBNA8GhgoBMXWw= github.com/lionsoul2014/ip2region/binding/golang v0.0.0-20241220152942-06eb5c6e8230/go.mod h1:C5LA5UO2ZXJrLaPLYtE1wUJMiyd/nwWaCO5cw/2pSHs= github.com/magiconair/properties v1.8.5/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60= diff --git a/internal/boot/boot.go b/internal/boot/boot.go index 6e6fcb7..598cce4 100644 --- a/internal/boot/boot.go +++ b/internal/boot/boot.go @@ -1,28 +1,46 @@ package boot import ( - "context" v1 "github.com/ayflying/utility_go/api/system/v1" + "github.com/ayflying/utility_go/drivers/db/elasticsearch" + "github.com/ayflying/utility_go/pkg/aycache" "github.com/ayflying/utility_go/service" "github.com/gogf/gf/v2/os/gctx" - "github.com/gogf/gf/v2/os/gtimer" - "time" + "math" ) var ( - ctx = gctx.GetInitCtx() + ctx = gctx.GetInitCtx() + _func = []func(){} ) func Boot() (err error) { err = service.SystemCron().StartCron() //用户活动持久化 - gtimer.SetTimeout(ctx, time.Minute, func(ctx context.Context) { - service.SystemCron().AddCron(v1.CronType_DAILY, func() error { - service.GameAct().Saves() - return nil - }) + service.SystemCron().AddCron(v1.CronType_DAILY, func() error { + return service.GameAct().Saves() }) + //初始化ES + elasticsearch.Init() + + //初始化指标 + service.SystemCron().AddCron(v1.CronType_MINUTE, func() error { + aycache.QPS.Set(math.Round(float64(aycache.QPSCount) / 60)) + aycache.QPSCount = 0 + return nil + }) + + //初始化自启动方法 + for _, v := range _func { + v() + } + return nil } + +// AddFunc 注册方法,在启动时执行 +func AddFunc(f func()) { + _func = append(_func, f) +} diff --git a/internal/logic/gameAct/gameAct.go b/internal/logic/gameAct/gameAct.go index 0caf718..f691311 100644 --- a/internal/logic/gameAct/gameAct.go +++ b/internal/logic/gameAct/gameAct.go @@ -11,6 +11,7 @@ import ( "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/os/gctx" "github.com/gogf/gf/v2/os/gtime" + "github.com/gogf/gf/v2/util/gconv" "strconv" "strings" "time" @@ -130,7 +131,8 @@ func (s *sGameAct) Save(actId int) (err error) { result := strings.Split(cacheKey, ":") actId, err = strconv.Atoi(result[1]) var uid int64 - uid, err = strconv.ParseInt(result[2], 10, 64) + uid = gconv.Int64(result[2]) + //uid, err = strconv.ParseInt(result[2], 10, 64) if err != nil { continue } diff --git a/pkg/aycache/cache.go b/pkg/aycache/cache.go index c0052d8..3ab1f7e 100644 --- a/pkg/aycache/cache.go +++ b/pkg/aycache/cache.go @@ -1,14 +1,11 @@ package aycache import ( - v1 "github.com/ayflying/utility_go/api/system/v1" "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/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promauto" - "math" ) type Mod struct { @@ -25,16 +22,6 @@ var ( ) ) -func init() { - - service.SystemCron().AddCron(v1.CronType_MINUTE, func() error { - QPS.Set(math.Round(float64(QPSCount) / 60)) - QPSCount = 0 - return nil - }) - -} - func New(_name ...string) gcache.Adapter { var cacheAdapterObj gcache.Adapter diff --git a/tools/tools.go b/tools/tools.go index ab740ad..c0b91a5 100644 --- a/tools/tools.go +++ b/tools/tools.go @@ -2,6 +2,7 @@ package tools import ( "encoding/json" + "github.com/ayflying/utility_go/internal/boot" "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/os/gctx" "github.com/gogf/gf/v2/os/gtime" @@ -31,15 +32,17 @@ type tools struct { } func init() { - g.Log().Debugf(gctx.New(), "初始化tools工具类") + boot.AddFunc(func() { + g.Log().Debugf(gctx.New(), "初始化tools工具类") - names := []toolsInterface{ - Tools, - } - for _, v := range names { - v.Load() - } + names := []toolsInterface{ + Tools, + } + for _, v := range names { + v.Load() + } + }) } func (m *tools) Load() { diff --git a/utility.go b/utility.go index b27a0e0..b5eea97 100644 --- a/utility.go +++ b/utility.go @@ -1,10 +1,9 @@ package utility_go import ( + "github.com/ayflying/utility_go/config" "github.com/ayflying/utility_go/internal/boot" _ "github.com/ayflying/utility_go/internal/logic" - - "github.com/ayflying/utility_go/config" "github.com/gogf/gf/v2/os/gctx" ) @@ -14,8 +13,12 @@ var ( ) func init() { - var err = boot.Boot() - if err != nil { - panic(err) - } + go func() { + // 初始化配置 + var err = boot.Boot() + if err != nil { + panic(err) + } + }() + } diff --git a/utility_test.go b/utility_test.go new file mode 100644 index 0000000..27f0f97 --- /dev/null +++ b/utility_test.go @@ -0,0 +1,66 @@ +package utility_go_test + +import ( + "github.com/ayflying/utility_go/internal/boot" + _ "github.com/ayflying/utility_go/internal/logic" + + "github.com/gogf/gf/v2/frame/g" + "github.com/gogf/gf/v2/os/gctx" + "testing" +) + +var ( + ctx = gctx.GetInitCtx() +) + +func TestInit(t *testing.T) { + g.Log().Debug(ctx, "开始调试了") + // 初始化配置 + var err = boot.Boot() + if err != nil { + t.Error(err) + } +} + +// +//func TestLoadConfig(t *testing.T) { +// +// tests := []struct { +// name string +// filePath string +// wantErr bool +// }{ +// { +// name: "valid config file", +// filePath: "testdata/valid_config.json", +// wantErr: false, +// }, +// { +// name: "non-existent file", +// filePath: "nonexistent.json", +// wantErr: true, +// }, +// { +// name: "invalid config format", +// filePath: "testdata/invalid_config.json", +// wantErr: true, +// }, +// { +// name: "empty file path", +// filePath: "", +// wantErr: true, +// }, +// } +// +// for _, tt := range tests { +// t.Run(tt.name, func(t *testing.T) { +// +// _, err := config.Load(tt.filePath) +// if tt.wantErr { +// assert.Error(t, err) +// } else { +// assert.NoError(t, err) +// } +// }) +// } +//}