Skip to content

Commit

Permalink
feat: make ci happy
Browse files Browse the repository at this point in the history
  • Loading branch information
Ayanami1314 committed Nov 9, 2024
1 parent 6455a43 commit 88a6cfe
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
8 changes: 3 additions & 5 deletions middleware/statistic.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,8 @@ func InitStatistic(r *gin.Engine) {
timer := time.NewTicker(DurationRefreshDupJudge)
go func() {
for {
select {
case <-timer.C:
UpdateDuplicateJudgeDuration(context.Background())
}
<-timer.C
UpdateDuplicateJudgeDuration(context.Background())
}
}()
}
Expand Down Expand Up @@ -57,7 +55,7 @@ func (u *UserRequestCache) IsDuplicate(req string) bool {
if !ok {
return false
}
return time.Now().Sub(lastReq) < GetDuplicateJudgeDuration()
return time.Since(lastReq) < GetDuplicateJudgeDuration()
}

func IsRobot(c *gin.Context) bool {
Expand Down
22 changes: 13 additions & 9 deletions middleware/statistic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/RoaringBitmap/roaring"
)

type userKeyType = struct{ key string } // make ci happy
func TestUVStatistic(t *testing.T) {
t.Run("TestRBMAddDup", func(t *testing.T) {
testRbm := roaring.New()
Expand Down Expand Up @@ -201,7 +202,8 @@ func TestCtxPass(t *testing.T) {
mockLogin := func(c *gin.Context) {
// 从gin.Context中获取请求的上下文
reqCtx := c.Request.Context()
user, ok := reqCtx.Value(constant.CtxKeyUser).(*model.UserDetail)
userKey := userKeyType{key: constant.CtxKeyUser}
user, ok := reqCtx.Value(userKey).(*model.UserDetail)
assert.True(t, ok)

// 将用户信息设置回gin.Context的上下文中
Expand All @@ -215,7 +217,8 @@ func TestCtxPass(t *testing.T) {
c.Status(http.StatusOK)
})
req := httptest.NewRequest(http.MethodGet, "/some-path", nil)
req = req.WithContext(context.WithValue(context.Background(), constant.CtxKeyUser, &model.UserDetail{}))
var userKey = userKeyType{key: constant.CtxKeyUser}
req = req.WithContext(context.WithValue(context.Background(), userKey, &model.UserDetail{}))

w := httptest.NewRecorder()
r.ServeHTTP(w, req)
Expand All @@ -238,14 +241,13 @@ func SimulateHighConcurrency(qps int, seconds int, initUserNum int, totalUserNum
// 因为ServeHTTP只传入Request, 所以将登录的context放在Request中, 需要取出放回context
mockLogin := func(c *gin.Context) {
reqCtx := c.Request.Context()
user, ok := reqCtx.Value(constant.CtxKeyUser).(*model.UserDetail)
userKey := userKeyType{key: constant.CtxKeyUser}
user, ok := reqCtx.Value(userKey).(*model.UserDetail)
assert.True(b, ok)
c.Set(constant.CtxKeyUser, user)
c.Next()
}
endMiddleWare := func(c *gin.Context) {
return
}
endMiddleWare := func(c *gin.Context) { c.Status(http.StatusOK) }
middlewares = append(middlewares, mockLogin)
middlewares = append(middlewares, PVStatistic())
middlewares = append(middlewares, UVStatistic())
Expand Down Expand Up @@ -281,14 +283,16 @@ func SimulateHighConcurrency(qps int, seconds int, initUserNum int, totalUserNum
r.Handle(method, path, mockHandler)
}
}

for i := 0; i < totalUserNum; i++ {
c := &gin.Context{}
user := &model.UserDetail{
UserMinimal: model.UserMinimal{
ID: int64(i),
},
}
reqCtx := context.WithValue(context.Background(), constant.CtxKeyUser, user)
userKey := userKeyType{key: constant.CtxKeyUser}
reqCtx := context.WithValue(context.Background(), userKey, user)
c.Request = testReqs[i].WithContext(reqCtx)
testCtxs[i] = c
}
Expand Down Expand Up @@ -334,7 +338,7 @@ func SimulateHighConcurrency(qps int, seconds int, initUserNum int, totalUserNum
}

func BenchmarkGetPVCount(b *testing.B) {
// 23 us / op
// <23 us / op
b.Run("TestHighConcurrency", func(b *testing.B) {
countLog := map[int64]bool{}
mu := sync.Mutex{}
Expand All @@ -357,7 +361,7 @@ func BenchmarkGetPVCount(b *testing.B) {
})
}
func BenchmarkGetUVCount(b *testing.B) {
// 23 us / op
// <23 us / op
b.Run("TestHighConcurrency", func(b *testing.B) {
countLog := map[int64]bool{}
mu := sync.Mutex{}
Expand Down

0 comments on commit 88a6cfe

Please sign in to comment.