From 076d6f15537efbd025d96ec858f4547f8d0c86c9 Mon Sep 17 00:00:00 2001 From: SchwarzSail <1424928981@qq.com> Date: Sun, 29 Sep 2024 15:57:58 +0800 Subject: [PATCH] fix: modify CacheEmptyRooms --- cmd/api/biz/handler/api/class_room_service.go | 6 +- cmd/api/biz/handler/api/user_service.go | 11 ++-- cmd/api/biz/rpc/classroom.go | 5 +- cmd/api/biz/rpc/user.go | 5 +- cmd/api/main.go | 3 +- cmd/classroom/dal/cache/empty_room.go | 8 +-- cmd/classroom/handler.go | 4 +- cmd/classroom/main.go | 3 +- cmd/classroom/service/get_room.go | 6 +- cmd/classroom/service/get_room_test.go | 4 +- cmd/classroom/service/service.go | 56 ++++++++++++++++--- cmd/user/handler.go | 4 +- cmd/user/main.go | 3 +- go.mod | 19 ++++--- go.sum | 24 ++++++++ pkg/client/cache.go | 8 +-- pkg/client/rpc.go | 3 + pkg/constants/constants.go | 7 +++ pkg/{utils => logger}/logger.go | 2 +- pkg/utils/rpc.go | 3 +- 20 files changed, 135 insertions(+), 49 deletions(-) create mode 100644 pkg/client/rpc.go rename pkg/{utils => logger}/logger.go (98%) diff --git a/cmd/api/biz/handler/api/class_room_service.go b/cmd/api/biz/handler/api/class_room_service.go index 58ce0df9..55cfa364 100644 --- a/cmd/api/biz/handler/api/class_room_service.go +++ b/cmd/api/biz/handler/api/class_room_service.go @@ -10,7 +10,7 @@ import ( "github.com/west2-online/fzuhelper-server/cmd/api/biz/rpc" "github.com/west2-online/fzuhelper-server/kitex_gen/classroom" "github.com/west2-online/fzuhelper-server/pkg/errno" - "github.com/west2-online/fzuhelper-server/pkg/utils" + "github.com/west2-online/fzuhelper-server/pkg/logger" ) // GetEmptyClassrooms . @@ -21,7 +21,7 @@ func GetEmptyClassrooms(ctx context.Context, c *app.RequestContext) { var req api.EmptyClassroomRequest err = c.BindAndValidate(&req) if err != nil { - utils.LoggerObj.Error("api.GetEmptyClassrooms: BindAndValidate", err) + logger.LoggerObj.Error("api.GetEmptyClassrooms: BindAndValidate", err) pack.RespError(c, errno.ParamEmpty) return } @@ -32,7 +32,7 @@ func GetEmptyClassrooms(ctx context.Context, c *app.RequestContext) { Campus: req.Campus, }) if err != nil { - utils.LoggerObj.Error("api.GetEmptyClassrooms: GetEmptyRoomRPC", err) + logger.LoggerObj.Error("api.GetEmptyClassrooms: GetEmptyRoomRPC", err) pack.RespError(c, errno.InternalServiceError) return } diff --git a/cmd/api/biz/handler/api/user_service.go b/cmd/api/biz/handler/api/user_service.go index badbaf51..a1ab3ef5 100644 --- a/cmd/api/biz/handler/api/user_service.go +++ b/cmd/api/biz/handler/api/user_service.go @@ -4,14 +4,13 @@ package api import ( "context" + "github.com/cloudwego/hertz/pkg/app" + api "github.com/west2-online/fzuhelper-server/cmd/api/biz/model/api" "github.com/west2-online/fzuhelper-server/cmd/api/biz/pack" "github.com/west2-online/fzuhelper-server/cmd/api/biz/rpc" "github.com/west2-online/fzuhelper-server/kitex_gen/user" "github.com/west2-online/fzuhelper-server/pkg/errno" - "github.com/west2-online/fzuhelper-server/pkg/utils" - - "github.com/cloudwego/hertz/pkg/app" - api "github.com/west2-online/fzuhelper-server/cmd/api/biz/model/api" + "github.com/west2-online/fzuhelper-server/pkg/logger" ) // GetLoginData . @@ -21,7 +20,7 @@ func GetLoginData(ctx context.Context, c *app.RequestContext) { var req api.GetLoginDataRequest err = c.BindAndValidate(&req) if err != nil { - utils.LoggerObj.Error("api.GetLoginData: BindAndValidate", err) + logger.LoggerObj.Error("api.GetLoginData: BindAndValidate", err) pack.RespError(c, errno.ParamEmpty) return } @@ -31,7 +30,7 @@ func GetLoginData(ctx context.Context, c *app.RequestContext) { Password: req.Password, }) if err != nil { - utils.LoggerObj.Error("api.GetLoginData: GetEmptyRoomRPC ", err) + logger.LoggerObj.Error("api.GetLoginData: GetEmptyRoomRPC ", err) pack.RespError(c, errno.InternalServiceError) return } diff --git a/cmd/api/biz/rpc/classroom.go b/cmd/api/biz/rpc/classroom.go index 7931268c..5480ab34 100644 --- a/cmd/api/biz/rpc/classroom.go +++ b/cmd/api/biz/rpc/classroom.go @@ -9,6 +9,7 @@ import ( "github.com/west2-online/fzuhelper-server/kitex_gen/classroom/classroomservice" "github.com/west2-online/fzuhelper-server/kitex_gen/model" "github.com/west2-online/fzuhelper-server/pkg/constants" + "github.com/west2-online/fzuhelper-server/pkg/logger" "github.com/west2-online/fzuhelper-server/pkg/utils" ) @@ -21,13 +22,13 @@ func InitClassroomRPC() { if err != nil { panic(err) } - utils.LoggerObj.Info("InitClassroomRPC success") + logger.LoggerObj.Info("InitClassroomRPC success") } func GetEmptyRoomRPC(ctx context.Context, req *classroom.EmptyRoomRequest) (emptyRooms []*model.Classroom, err error) { resp, err := classroomClient.GetEmptyRoom(ctx, req) if err != nil { - utils.LoggerObj.Errorf("api.rpc.classroom GetEmptyRoomRPC received rpc error %v", err) + logger.LoggerObj.Errorf("api.rpc.classroom GetEmptyRoomRPC received rpc error %v", err) return nil, err } if err = utils.IsSuccess(resp.Base); err != nil { diff --git a/cmd/api/biz/rpc/user.go b/cmd/api/biz/rpc/user.go index 14eccd7a..688eee07 100644 --- a/cmd/api/biz/rpc/user.go +++ b/cmd/api/biz/rpc/user.go @@ -8,6 +8,7 @@ import ( "github.com/west2-online/fzuhelper-server/kitex_gen/user" "github.com/west2-online/fzuhelper-server/kitex_gen/user/userservice" "github.com/west2-online/fzuhelper-server/pkg/constants" + "github.com/west2-online/fzuhelper-server/pkg/logger" "github.com/west2-online/fzuhelper-server/pkg/utils" ) @@ -20,13 +21,13 @@ func InitUserRPC() { if err != nil { panic(err) } - utils.LoggerObj.Info("InitUserRPC success") + logger.LoggerObj.Info("InitUserRPC success") } func GetLoginDataRPC(ctx context.Context, req *user.GetLoginDataRequest) (string, []string, error) { resp, err := userClient.GetLoginData(ctx, req) if err != nil { - utils.LoggerObj.Errorf("api.rpc.user GetLoginDataRPC received rpc error %v", err) + logger.LoggerObj.Errorf("api.rpc.user GetLoginDataRPC received rpc error %v", err) return "", nil, err } if err = utils.IsSuccess(resp.Base); err != nil { diff --git a/cmd/api/main.go b/cmd/api/main.go index 258d806b..a16a9e3f 100755 --- a/cmd/api/main.go +++ b/cmd/api/main.go @@ -9,6 +9,7 @@ import ( "github.com/west2-online/fzuhelper-server/cmd/api/biz/rpc" "github.com/west2-online/fzuhelper-server/config" "github.com/west2-online/fzuhelper-server/pkg/constants" + "github.com/west2-online/fzuhelper-server/pkg/logger" "github.com/west2-online/fzuhelper-server/pkg/utils" ) @@ -24,7 +25,7 @@ func init() { flag.Parse() config.Init(*path, constants.ApiServiceName) //rpc - utils.LoggerInit() + logger.LoggerInit() rpc.Init() } diff --git a/cmd/classroom/dal/cache/empty_room.go b/cmd/classroom/dal/cache/empty_room.go index 75218f37..9ea93f7f 100644 --- a/cmd/classroom/dal/cache/empty_room.go +++ b/cmd/classroom/dal/cache/empty_room.go @@ -4,7 +4,7 @@ import ( "context" "encoding/json" "github.com/west2-online/fzuhelper-server/pkg/constants" - "github.com/west2-online/fzuhelper-server/pkg/utils" + "github.com/west2-online/fzuhelper-server/pkg/logger" ) // SetEmptyRoomCache 设置空教室缓存 key 为 date + campus + startTime + endTime,value直接采用bytes存储 @@ -14,18 +14,18 @@ func SetEmptyRoomCache(ctx context.Context, key string, emptyRoomList []string) // 两天过期 err = RedisClient.Set(ctx, key, emptyRoomJson, constants.ClassroomKeyExpire).Err() if err != nil { - utils.LoggerObj.Fatalf("dal.cache.SetEmptyRoomCache failed, err is %v", err) + logger.LoggerObj.Fatalf("dal.cache.SetEmptyRoomCache failed, err is %v", err) } } func GetEmptyRoomCache(ctx context.Context, key string) (emptyRoomList []string) { data, err := RedisClient.Get(ctx, key).Result() if err != nil { - utils.LoggerObj.Fatalf("dal.cache.GetEmptyRoomCache failed, err is %v", err) + logger.LoggerObj.Fatalf("dal.cache.GetEmptyRoomCache failed, err is %v", err) return nil } err = json.Unmarshal([]byte(data), &emptyRoomList) if err != nil { - utils.LoggerObj.Fatalf("dal.cache.GetEmptyRoomCache Unmarshal failed, err is %v", err) + logger.LoggerObj.Fatalf("dal.cache.GetEmptyRoomCache Unmarshal failed, err is %v", err) return nil } return diff --git a/cmd/classroom/handler.go b/cmd/classroom/handler.go index a9529fd5..bef170f5 100644 --- a/cmd/classroom/handler.go +++ b/cmd/classroom/handler.go @@ -5,7 +5,7 @@ import ( "github.com/west2-online/fzuhelper-server/cmd/classroom/pack" "github.com/west2-online/fzuhelper-server/cmd/classroom/service" classroom "github.com/west2-online/fzuhelper-server/kitex_gen/classroom" - "github.com/west2-online/fzuhelper-server/pkg/utils" + "github.com/west2-online/fzuhelper-server/pkg/logger" ) // ClassroomServiceImpl implements the last service interface defined in the IDL. @@ -22,6 +22,6 @@ func (s *ClassroomServiceImpl) GetEmptyRoom(ctx context.Context, req *classroom. } resp.Base = pack.BuildBaseResp(nil) resp.Rooms = pack.BuildClassRooms(res, req.Campus) - utils.LoggerObj.Info("GetEmptyRoom success") + logger.LoggerObj.Info("GetEmptyRoom success") return } diff --git a/cmd/classroom/main.go b/cmd/classroom/main.go index 25347139..93eaf7fd 100644 --- a/cmd/classroom/main.go +++ b/cmd/classroom/main.go @@ -12,6 +12,7 @@ import ( "github.com/west2-online/fzuhelper-server/config" classroom "github.com/west2-online/fzuhelper-server/kitex_gen/classroom/classroomservice" "github.com/west2-online/fzuhelper-server/pkg/constants" + "github.com/west2-online/fzuhelper-server/pkg/logger" "github.com/west2-online/fzuhelper-server/pkg/utils" "net" ) @@ -25,7 +26,7 @@ var ( func Init() { // config init - utils.LoggerInit() + logger.LoggerInit() path = flag.String("config", "./config", "config path") flag.Parse() config.Init(*path, serviceName) diff --git a/cmd/classroom/service/get_room.go b/cmd/classroom/service/get_room.go index d02ad312..d866fdc0 100644 --- a/cmd/classroom/service/get_room.go +++ b/cmd/classroom/service/get_room.go @@ -5,7 +5,7 @@ import ( "github.com/pkg/errors" "github.com/west2-online/fzuhelper-server/cmd/classroom/dal/cache" "github.com/west2-online/fzuhelper-server/kitex_gen/classroom" - "github.com/west2-online/fzuhelper-server/pkg/utils" + "github.com/west2-online/fzuhelper-server/pkg/logger" "github.com/west2-online/jwch" "strings" ) @@ -51,7 +51,7 @@ func (s *ClassroomService) getQiShanEmptyRooms(req *classroom.EmptyRoomRequest) End: req.EndTime, }) if err != nil { - utils.LoggerObj.Errorf("service.getQiShanEmptyRooms: %v", err) + logger.LoggerObj.Errorf("service.getQiShanEmptyRooms: %v", err) return nil, errors.Wrap(err, "service.getQiShanEmptyRooms failed") } return rooms, nil @@ -65,7 +65,7 @@ func (s *ClassroomService) getOtherEmptyRooms(req *classroom.EmptyRoomRequest) ( End: req.EndTime, }) if err != nil { - utils.LoggerObj.Errorf("service.getOtherEmptyRooms: %v", err) + logger.LoggerObj.Errorf("service.getOtherEmptyRooms: %v", err) return nil, errors.Wrap(err, "service.getOtherEmptyRooms failed") } diff --git a/cmd/classroom/service/get_room_test.go b/cmd/classroom/service/get_room_test.go index 61220c3c..58162b5b 100644 --- a/cmd/classroom/service/get_room_test.go +++ b/cmd/classroom/service/get_room_test.go @@ -7,7 +7,7 @@ import ( "github.com/west2-online/fzuhelper-server/cmd/classroom/dal" "github.com/west2-online/fzuhelper-server/cmd/classroom/pack" "github.com/west2-online/fzuhelper-server/kitex_gen/classroom" - "github.com/west2-online/fzuhelper-server/pkg/utils" + "github.com/west2-online/fzuhelper-server/pkg/logger" "github.com/west2-online/jwch" "net/http" "testing" @@ -20,7 +20,7 @@ func getIDAndCookies() (string, []*http.Cookie) { func Init() { // config init - utils.LoggerInit() + logger.LoggerInit() dal.Init() klog.SetLevel(klog.LevelDebug) } diff --git a/cmd/classroom/service/service.go b/cmd/classroom/service/service.go index 706ce5e2..1cacd312 100644 --- a/cmd/classroom/service/service.go +++ b/cmd/classroom/service/service.go @@ -5,8 +5,10 @@ import ( "github.com/west2-online/fzuhelper-server/config" "github.com/west2-online/fzuhelper-server/kitex_gen/classroom" "github.com/west2-online/fzuhelper-server/pkg/constants" - "github.com/west2-online/fzuhelper-server/pkg/utils" + "github.com/west2-online/fzuhelper-server/pkg/logger" "github.com/west2-online/jwch" + "golang.org/x/time/rate" + "k8s.io/client-go/util/workqueue" "net/http" "strconv" "time" @@ -43,6 +45,21 @@ func CacheEmptyRooms() { ctx := context.Background() id, cookies := jwch.NewStudent().WithUser(config.DefaultUser.Account, config.DefaultUser.Password).GetIdentifierAndCookies() l := NewClassroomService(ctx, id, cookies) + //使用具有限速功能的工作队列,避免教务处的压力过大 + queue := workqueue.NewNamedRateLimitingQueue( + workqueue.NewMaxOfRateLimiter( + // For syncRec failures(i.e. doRecommend return err), the retry time is (2*minutes)*2^ + // The maximum retry time is 24 hours + workqueue.NewItemExponentialFailureRateLimiter(constants.FailureRateLimiterBaseDelay, constants.FailureRateLimiterMaxDelay), + // 10 qps, 100 bucket size. This is only for retry speed, it's only the overall factor (not per item) + //每秒最多产生 10 个令牌(允许处理 10 个任务)。 + //100:令牌桶最多存储 100 个令牌,允许积累的最大任务数量 + &workqueue.BucketRateLimiter{Limiter: rate.NewLimiter(rate.Limit(10), 100)}, + ), + constants.ClassroomService) + for i := 0; i < constants.NumWorkers; i++ { + go worker(queue, l) + } var dates []string currentTime := time.Now() //设定一周时间 @@ -60,15 +77,40 @@ func CacheEmptyRooms() { StartTime: strconv.Itoa(startTime), EndTime: strconv.Itoa(endTime), } - go l.GetEmptyRooms(args) - //给3s的时间让服务器处理 - time.Sleep(3 * time.Second) + queue.Add(args) + logger.LoggerObj.Debugf("classroom.service.CacheEmptyRooms add task %v", args) } } - utils.LoggerObj.Infof("Complete Data of campus %s", campus) + logger.LoggerObj.Infof("classroom.service.CacheEmptyRooms add all tasks of campus %v in the day %v", campus, date) } - utils.LoggerObj.Infof("Complete Data of date %s", date) } - time.Sleep(constants.ClassroomKeyExpire) + time.Sleep(constants.ScheduledTime) + } +} + +// 从工作队列取出task并处理 +func worker(queue workqueue.RateLimitingInterface, l *ClassroomService) { + for { + task, shutDown := queue.Get() + if shutDown { + logger.LoggerObj.Debug("classroom.service.worker worker shutDown") + return + } + func(task any) { + defer queue.Done(task) + args, ok := task.(*classroom.EmptyRoomRequest) + if !ok { + logger.LoggerObj.Errorf("classroom.service.worker task type error: %T", task) + return + } + _, err := l.GetEmptyRooms(args) + if err != nil { + logger.LoggerObj.Errorf("classroom.service.worker GetEmptyRooms failed, args %v: %v", err, args) + return + } + //将任务标记为完成 + queue.Forget(task) + logger.LoggerObj.Debug("classroom.service.worker task %v done", args) + }(task) } } diff --git a/cmd/user/handler.go b/cmd/user/handler.go index f0491afe..efdb2996 100644 --- a/cmd/user/handler.go +++ b/cmd/user/handler.go @@ -5,7 +5,7 @@ import ( "github.com/west2-online/fzuhelper-server/cmd/user/pack" "github.com/west2-online/fzuhelper-server/cmd/user/service" user "github.com/west2-online/fzuhelper-server/kitex_gen/user" - "github.com/west2-online/fzuhelper-server/pkg/utils" + "github.com/west2-online/fzuhelper-server/pkg/logger" ) // UserServiceImpl implements the last service interface defined in the IDL. @@ -23,6 +23,6 @@ func (s *UserServiceImpl) GetLoginData(ctx context.Context, req *user.GetLoginDa resp.Base = pack.BuildBaseResp(nil) resp.Id = id resp.Cookies = cookies - utils.LoggerObj.Info("GetLoginData success") + logger.LoggerObj.Info("GetLoginData success") return } diff --git a/cmd/user/main.go b/cmd/user/main.go index 41270c41..28e0511e 100644 --- a/cmd/user/main.go +++ b/cmd/user/main.go @@ -10,6 +10,7 @@ import ( "github.com/west2-online/fzuhelper-server/config" user "github.com/west2-online/fzuhelper-server/kitex_gen/user/userservice" "github.com/west2-online/fzuhelper-server/pkg/constants" + "github.com/west2-online/fzuhelper-server/pkg/logger" "github.com/west2-online/fzuhelper-server/pkg/utils" "net" ) @@ -28,7 +29,7 @@ func Init() { config.Init(*path, serviceName) klog.SetLevel(klog.LevelDebug) - utils.LoggerInit() + logger.LoggerInit() } func main() { diff --git a/go.mod b/go.mod index e2a8d0ad..40de01e0 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/west2-online/fzuhelper-server -go 1.22 +go 1.22.0 toolchain go1.22.3 @@ -35,6 +35,7 @@ require ( github.com/cloudwego/base64x v0.1.4 // indirect github.com/cloudwego/iasm v0.2.0 // indirect github.com/cloudwego/runtimex v0.1.0 // indirect + github.com/go-logr/logr v1.4.2 // indirect github.com/go-resty/resty/v2 v2.7.0 // indirect github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 // indirect github.com/henrylee2cn/ameda v1.4.10 // indirect @@ -43,12 +44,16 @@ require ( github.com/nyaruka/phonenumbers v1.0.55 // indirect github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d // indirect github.com/smartystreets/goconvey v1.6.4 // indirect + k8s.io/apimachinery v0.31.1 // indirect + k8s.io/client-go v0.31.1 // indirect + k8s.io/klog/v2 v2.130.1 // indirect + k8s.io/utils v0.0.0-20240921022957-49e7df575cb6 // indirect ) require ( cloud.google.com/go v0.110.0 // indirect cloud.google.com/go/compute v1.19.1 // indirect - cloud.google.com/go/compute/metadata v0.2.3 // indirect + cloud.google.com/go/compute/metadata v0.3.0 // indirect cloud.google.com/go/firestore v1.9.0 // indirect cloud.google.com/go/longrunning v0.4.1 // indirect github.com/armon/go-metrics v0.4.0 // indirect @@ -72,8 +77,8 @@ require ( github.com/gogo/protobuf v1.3.2 // indirect github.com/golang-jwt/jwt v3.2.2+incompatible github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect - github.com/golang/protobuf v1.5.3 // indirect - github.com/google/go-cmp v0.5.9 // indirect + github.com/golang/protobuf v1.5.4 // indirect + github.com/google/go-cmp v0.6.0 // indirect github.com/google/pprof v0.0.0-20240727154555-813a5fbdbec8 // indirect github.com/google/s2a-go v0.1.3 // indirect github.com/googleapis/enterprise-certificate-proxy v0.2.3 // indirect @@ -102,7 +107,7 @@ require ( github.com/modern-go/reflect2 v1.0.2 // indirect github.com/pelletier/go-toml/v2 v2.0.8 // indirect github.com/pkg/errors v0.9.1 - github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect github.com/sagikazarmark/crypt v0.10.0 // indirect github.com/spf13/afero v1.9.5 // indirect github.com/spf13/cast v1.5.1 // indirect @@ -128,7 +133,7 @@ require ( golang.org/x/arch v0.2.0 // indirect golang.org/x/crypto v0.27.0 // indirect golang.org/x/net v0.29.0 // indirect - golang.org/x/oauth2 v0.7.0 // indirect + golang.org/x/oauth2 v0.21.0 // indirect golang.org/x/sync v0.8.0 // indirect golang.org/x/sys v0.25.0 // indirect golang.org/x/text v0.18.0 // indirect @@ -138,7 +143,7 @@ require ( google.golang.org/appengine v1.6.7 // indirect google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1 // indirect google.golang.org/grpc v1.56.3 // indirect - google.golang.org/protobuf v1.31.0 // indirect + google.golang.org/protobuf v1.34.2 // indirect gopkg.in/ini.v1 v1.67.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index ff1a4f41..a66932c6 100644 --- a/go.sum +++ b/go.sum @@ -29,6 +29,8 @@ cloud.google.com/go/compute v1.19.1 h1:am86mquDUgjGNWxiGn+5PGLbmgiWXlE/yNWpIpNvu cloud.google.com/go/compute v1.19.1/go.mod h1:6ylj3a05WF8leseCdIf77NK0g1ey+nj5IKd5/kvShxE= cloud.google.com/go/compute/metadata v0.2.3 h1:mg4jlk7mCAj6xXp9UJ4fjI9VUI5rubuGBW5aJ7UnBMY= cloud.google.com/go/compute/metadata v0.2.3/go.mod h1:VAV5nSsACxMJvgaAuX6Pk2AawlZn8kiOGuCv6gTkwuA= +cloud.google.com/go/compute/metadata v0.3.0 h1:Tz+eQXMEqDIKRsmY3cHTL6FVaynIjX2QxYC4trgAKZc= +cloud.google.com/go/compute/metadata v0.3.0/go.mod h1:zFmK7XCadkQkj6TtorcaGlCW1hT1fIilQDwofLpJ20k= cloud.google.com/go/datastore v1.0.0/go.mod h1:LXYbyblFSglQ5pkeyhO+Qmw7ukd3C+pD7TKLgZqpHYE= cloud.google.com/go/datastore v1.1.0/go.mod h1:umbIZjpQpHh4hmRpGhH4tLFup+FVzqBi1b3c64qFpCk= cloud.google.com/go/firestore v1.1.0/go.mod h1:ulACoGHTpvq5r8rxGJ4ddJZBZqakUQqClKRT5SZwBmk= @@ -221,6 +223,8 @@ github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG1KdI/P7A= github.com/go-logr/logr v1.2.4 h1:g01GSCwiDw2xSZfjJ2/T9M+S6pFdcNtFYsp+Y43HYDQ= github.com/go-logr/logr v1.2.4/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= +github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY= +github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag= github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE= github.com/go-resty/resty/v2 v2.7.0 h1:me+K9p3uhSmXtrBZ4k9jcEAfJmuC8IivWHwaLZwPrFY= @@ -272,6 +276,8 @@ github.com/golang/protobuf v1.5.1/go.mod h1:DopwsBzvsk0Fs44TXzsVbJyPhcCPeIwnvohx github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg= github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= +github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek= +github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps= github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ= github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ= github.com/google/btree v1.0.1 h1:gK4Kx5IaGY9CD5sPJ36FHiBJ6ZXl0kilRiiCj+jdYp4= @@ -290,6 +296,8 @@ github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/google/go-cmp v0.5.6/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/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= +github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs= github.com/google/martian/v3 v3.0.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0= @@ -322,6 +330,7 @@ github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORR github.com/gordonklaus/ineffassign v0.0.0-20200309095847-7953dde2c7bf/go.mod h1:cuNKsD1zp2v6XfE/orVX2QE1LC+i254ceGcVeDT3pTU= github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc= github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= +github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc= github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs= github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 h1:+9834+KizmvFV7pXQGSXQTsaWhq2GjuNUt0aUU0YBYw= github.com/grpc-ecosystem/go-grpc-middleware v1.3.0/go.mod h1:z0ButlSOZa5vEBq9m2m2hlwIgKw+rp3sdCBRoJY+30Y= @@ -509,6 +518,7 @@ github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE github.com/pkg/sftp v1.13.1/go.mod h1:3HaPG6Dq1ILlpPZRO0HVMrsydcdLt6HRDccSgb87qRg= 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/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI= github.com/posener/complete v1.2.3/go.mod h1:WZIdtGGp+qx0sLrYKtIRAruyNpv6hFCicSgv7Sy7s/s= github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw= @@ -547,6 +557,7 @@ github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6L github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8= github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= +github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8= github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts= github.com/sagikazarmark/crypt v0.10.0 h1:96E1qrToLBU6fGzo+PRRz7KGOc9FkYFiPnR3/zf8Smg= @@ -822,6 +833,8 @@ golang.org/x/oauth2 v0.0.0-20201208152858-08078c50e5b5/go.mod h1:KelEdhl1UZF7XfJ golang.org/x/oauth2 v0.0.0-20210218202405-ba52d332ba99/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A= golang.org/x/oauth2 v0.7.0 h1:qe6s0zUXlPX80/dITx3440hWZ7GwMwgDDyrSGTPJG/g= golang.org/x/oauth2 v0.7.0/go.mod h1:hPLQkd9LyjfXTiRohC/41GhcFqxisoUQ99sCUOHO9x4= +golang.org/x/oauth2 v0.21.0 h1:tsimM75w1tF/uws5rbeHzIWxEqElMehnc+iW793zsZs= +golang.org/x/oauth2 v0.21.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI= 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-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= @@ -1113,6 +1126,8 @@ google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQ google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8= google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= +google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg= +google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw= gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= @@ -1154,6 +1169,14 @@ honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWh honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg= honnef.co/go/tools v0.0.1-2020.1.3/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k= honnef.co/go/tools v0.0.1-2020.1.4/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k= +k8s.io/apimachinery v0.31.1 h1:mhcUBbj7KUjaVhyXILglcVjuS4nYXiwC+KKFBgIVy7U= +k8s.io/apimachinery v0.31.1/go.mod h1:rsPdaZJfTfLsNJSQzNHQvYoTmxhoOEofxtOsF3rtsMo= +k8s.io/client-go v0.31.1 h1:f0ugtWSbWpxHR7sjVpQwuvw9a3ZKLXX0u0itkFXufb0= +k8s.io/client-go v0.31.1/go.mod h1:sKI8871MJN2OyeqRlmA4W4KM9KBdBUpDLu/43eGemCg= +k8s.io/klog/v2 v2.130.1 h1:n9Xl7H1Xvksem4KFG4PYbdQCQxqc/tTUyrgXaOhHSzk= +k8s.io/klog/v2 v2.130.1/go.mod h1:3Jpz1GvMt720eyJH1ckRHK1EDfpxISzJ7I9OYgaDtPE= +k8s.io/utils v0.0.0-20240921022957-49e7df575cb6 h1:MDF6h2H/h4tbzmtIKTuctcwZmY0tY9mD9fNT47QO6HI= +k8s.io/utils v0.0.0-20240921022957-49e7df575cb6/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0= nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50= rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8= rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4= @@ -1161,3 +1184,4 @@ rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0= rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA= sigs.k8s.io/yaml v1.2.0 h1:kr/MCeFWJWTwyaHoR9c8EjH9OumOmoF9YGiZd7lFm/Q= sigs.k8s.io/yaml v1.2.0/go.mod h1:yfXDCHCao9+ENCvLSE62v9VSji2MKu5jeNfTrofGhJc= +sigs.k8s.io/yaml v1.4.0 h1:Mk1wCc2gy/F0THH0TAp1QYyJNzRm2KCLy3o5ASXVI5E= diff --git a/pkg/client/cache.go b/pkg/client/cache.go index ac2300ee..a4ab747e 100644 --- a/pkg/client/cache.go +++ b/pkg/client/cache.go @@ -4,7 +4,7 @@ import ( "context" "github.com/redis/go-redis/v9" "github.com/west2-online/fzuhelper-server/config" - "github.com/west2-online/fzuhelper-server/pkg/utils" + "github.com/west2-online/fzuhelper-server/pkg/logger" ) // NewRedisClient 传入dbName, 比如classroom将key放在db0中,user放在db1中 @@ -12,10 +12,10 @@ func NewRedisClient(dbName int) *redis.Client { //首先判断config的redis是否初始化过。如果没有,则该field应该是空指针,我们需要报错返回 conf := config.Redis if conf == nil { - utils.LoggerObj.Fatalf("The redis config init failed") + logger.LoggerObj.Fatalf("The redis config init failed") panic("The redis config init failed") } - utils.LoggerObj.Infof("redis addr: %s", conf.Addr) + logger.LoggerObj.Infof("redis addr: %s", conf.Addr) client := redis.NewClient(&redis.Options{ Addr: conf.Addr, Password: conf.Password, @@ -23,7 +23,7 @@ func NewRedisClient(dbName int) *redis.Client { }) _, err := client.Ping(context.TODO()).Result() if err != nil { - utils.LoggerObj.Fatalf("redis client ping failed: %v", err) + logger.LoggerObj.Fatalf("redis client ping failed: %v", err) panic(err) } return client diff --git a/pkg/client/rpc.go b/pkg/client/rpc.go new file mode 100644 index 00000000..52c44825 --- /dev/null +++ b/pkg/client/rpc.go @@ -0,0 +1,3 @@ +package client + +//定义一系列的RPC客户端 diff --git a/pkg/constants/constants.go b/pkg/constants/constants.go index 54063a8c..9b4c2fe2 100644 --- a/pkg/constants/constants.go +++ b/pkg/constants/constants.go @@ -37,6 +37,13 @@ const ( MaxGoroutines = 10 MaxOpenConns = 100 ConnMaxLifetime = 10 * time.Second + NumWorkers = 10 //最大的并发数量 + //timeout + FailureRateLimiterBaseDelay = time.Minute + FailureRateLimiterMaxDelay = 30 * time.Minute + + //定时任务 + ScheduledTime = 24 * 2 * time.Hour ) var CampusArray = []string{"旗山校区", "鼓浪屿校区", "集美校区", "铜盘校区", "怡山校区", "晋江校区", "泉港校区"} diff --git a/pkg/utils/logger.go b/pkg/logger/logger.go similarity index 98% rename from pkg/utils/logger.go rename to pkg/logger/logger.go index 9f2227f4..c77cac26 100644 --- a/pkg/utils/logger.go +++ b/pkg/logger/logger.go @@ -1,5 +1,5 @@ // utils/logger.go -package utils +package logger import ( "go.uber.org/zap" diff --git a/pkg/utils/rpc.go b/pkg/utils/rpc.go index d192aed7..aaf49614 100644 --- a/pkg/utils/rpc.go +++ b/pkg/utils/rpc.go @@ -3,13 +3,14 @@ package utils import ( "github.com/west2-online/fzuhelper-server/kitex_gen/model" "github.com/west2-online/fzuhelper-server/pkg/errno" + "github.com/west2-online/fzuhelper-server/pkg/logger" ) //该文件负责处理通用的rpc结果处理 func IsSuccess(baseResp *model.BaseResp) error { if baseResp.Code != errno.SuccessCode { - LoggerObj.Errorf("utils.rpc.IsSuccess base code is not successful %v", baseResp) + logger.LoggerObj.Errorf("utils.rpc.IsSuccess base code is not successful %v", baseResp) return errno.NewErrNo(baseResp.Code, baseResp.Msg) } return nil