Skip to content

Commit 4905eeb

Browse files
committed
添加queue的错误次数限制
1 parent 145dc00 commit 4905eeb

File tree

6 files changed

+43
-13
lines changed

6 files changed

+43
-13
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@
1818
/.idea
1919

2020
/plugins/logger/zap/testdata/*
21-
go.sum
21+
go.sum

sdk/antd_api/api.go

+4
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,7 @@ func (e *Api) AddError(err error) {
131131
}
132132
}
133133

134+
func (e Api) Translate(form, to interface{}) {
135+
pkg.Translate(form, to)
136+
}
137+

sdk/pkg/jwtauth/jwtauth.go

+21-11
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ type GinJWTMiddleware struct {
6565
// User can define own LoginResponse func.
6666
LoginResponse func(*gin.Context, int, string, time.Time)
6767

68+
// User can define own AntdLoginResponse func.
69+
AntdLoginResponse func(*gin.Context, int, string, time.Time)
70+
6871
// User can define own RefreshResponse func.
6972
RefreshResponse func(*gin.Context, int, string, time.Time)
7073

@@ -213,19 +216,19 @@ var (
213216
RKey = "r"
214217

215218
// RoleIdKey 角色id Old
216-
RoleIdKey = "roleid"
219+
RoleIdKey = "roleid"
217220

218221
// RoleKey 角色名称 Old
219-
RoleKey = "rolekey"
222+
RoleKey = "rolekey"
220223

221224
// RoleNameKey 角色名称 Old
222225
RoleNameKey = "rolename"
223226

224227
// RoleIdKey 部门id
225-
DeptId = "deptId"
228+
DeptId = "deptId"
226229

227230
// RoleKey 部门名称
228-
DeptName = "deptName"
231+
DeptName = "deptName"
229232
)
230233

231234
// New for check error with GinJWTMiddleware
@@ -328,6 +331,18 @@ func (mw *GinJWTMiddleware) MiddlewareInit() error {
328331
}
329332
}
330333

334+
if mw.AntdLoginResponse == nil {
335+
mw.AntdLoginResponse = func(c *gin.Context, code int, token string, expire time.Time) {
336+
c.JSON(http.StatusOK, gin.H{
337+
"code": http.StatusOK,
338+
"success": true,
339+
"token": token,
340+
"currentAuthority": token,
341+
"expire": expire.Format(time.RFC3339),
342+
})
343+
}
344+
}
345+
331346
if mw.RefreshResponse == nil {
332347
mw.RefreshResponse = func(c *gin.Context, code int, token string, expire time.Time) {
333348
c.JSON(http.StatusOK, gin.H{
@@ -446,24 +461,19 @@ func (mw *GinJWTMiddleware) LoginHandler(c *gin.Context) {
446461
mw.unauthorized(c, http.StatusInternalServerError, mw.HTTPStatusMessageFunc(ErrMissingAuthenticatorFunc, c))
447462
return
448463
}
449-
450464
data, err := mw.Authenticator(c)
451-
452465
if err != nil {
453466
mw.unauthorized(c, 400, mw.HTTPStatusMessageFunc(err, c))
454467
return
455468
}
456-
457469
// Create the token
458470
token := jwt.New(jwt.GetSigningMethod(mw.SigningAlgorithm))
459471
claims := token.Claims.(jwt.MapClaims)
460-
461472
if mw.PayloadFunc != nil {
462473
for key, value := range mw.PayloadFunc(data) {
463474
claims[key] = value
464475
}
465476
}
466-
467477
expire := mw.TimeFunc().Add(mw.Timeout)
468478
claims["exp"] = expire.Unix()
469479
claims["orig_iat"] = mw.TimeFunc().Unix()
@@ -488,7 +498,7 @@ func (mw *GinJWTMiddleware) LoginHandler(c *gin.Context) {
488498
)
489499
}
490500

491-
mw.LoginResponse(c, http.StatusOK, tokenString, expire)
501+
mw.AntdLoginResponse(c, http.StatusOK, tokenString, expire)
492502
}
493503

494504
func (mw *GinJWTMiddleware) signedString(token *jwt.Token) (string, error) {
@@ -747,4 +757,4 @@ func GetToken(c *gin.Context) string {
747757
}
748758

749759
return token.(string)
750-
}
760+
}

storage/queue/memory.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,16 @@ func (m *Memory) Append(message storage.Messager) error {
4343
memoryMessage.SetID(message.GetID())
4444
memoryMessage.SetStream(message.GetStream())
4545
memoryMessage.SetValues(message.GetValues())
46+
4647
v, ok := m.queue.Load(message.GetStream())
47-
if !ok {
48+
49+
// TODO: 错误超出5次将放弃
50+
if !ok && memoryMessage.GetErrorCount() < 5 {
4851
v = m.makeQueue()
4952
m.queue.Store(message.GetStream(), v)
53+
memoryMessage.SetErrorCount()
5054
}
55+
5156
var q queue
5257
switch v.(type) {
5358
case queue:

storage/queue/message.go

+9
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
type Message struct {
1010
redisqueue.Message
11+
ErrorCount int
1112
}
1213

1314
func (m *Message) GetID() string {
@@ -49,3 +50,11 @@ func (m *Message) SetPrefix(prefix string) {
4950
}
5051
m.Values[storage.PrefixKey] = prefix
5152
}
53+
54+
func (m *Message) SetErrorCount() {
55+
m.ErrorCount = m.ErrorCount + 1
56+
}
57+
58+
func (m *Message) GetErrorCount() int {
59+
return m.ErrorCount
60+
}

storage/type.go

+2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ type Messager interface {
3939
GetValues() map[string]interface{}
4040
GetPrefix() string
4141
SetPrefix(string)
42+
SetErrorCount()
43+
GetErrorCount() int
4244
}
4345

4446
type ConsumerFunc func(Messager) error

0 commit comments

Comments
 (0)