Merge pull request 'by Robin at 20240326; fix high concurrency problem' (#208) from feat-IRONFANS-ALTER-Robin into main

Reviewed-on: http://121.41.31.146:3000/wishpal_ironfan/service/pulls/208
This commit is contained in:
chenhao 2024-03-26 18:27:06 +08:00
commit 43e2d412fa
1 changed files with 13 additions and 11 deletions

View File

@ -10,7 +10,7 @@ import (
//标签格式为string|int|int
type CryptoTagInterceptor struct {
cryptoEleQueue *list.List //队列
//cryptoEleQueue *list.List //队列
printableCryptoFun printableCryptoFunc //将拦截到的[]byte进行可打印加密/解密的方法
}
@ -21,7 +21,7 @@ func newCryptoTagInterceptor() *CryptoTagInterceptor {
}
func (interceptor *CryptoTagInterceptor) Init() {
interceptor.cryptoEleQueue = list.New()
//interceptor.cryptoEleQueue = list.New()
}
func (interceptor *CryptoTagInterceptor) LoadPrintableCryptoFunc(printableCryptoFun printableCryptoFunc) {
@ -31,15 +31,17 @@ func (interceptor *CryptoTagInterceptor) LoadPrintableCryptoFunc(printableCrypto
// 拦截方法
func (interceptor *CryptoTagInterceptor) Intercept(p any, tags ...string) (err error) {
cryptoEleQueue := list.New()
//解析当前类的标签
if err = interceptor.parse(p, tags...); err != nil {
if err = interceptor.parse(p, cryptoEleQueue, tags...); err != nil {
logger.Error("parse failed:%v", err)
return
}
//开始遍历标签
for interceptor.cryptoEleQueue.Len() != 0 {
ele := interceptor.cryptoEleQueue.Remove(interceptor.cryptoEleQueue.Front())
for cryptoEleQueue.Len() != 0 {
ele := cryptoEleQueue.Remove(cryptoEleQueue.Front())
if ele == nil {
continue
}
@ -50,13 +52,13 @@ func (interceptor *CryptoTagInterceptor) Intercept(p any, tags ...string) (err e
}
if cryptoElement.eleType == StructPtr { // *struct
if err = interceptor.parse(cryptoElement.eleValue.Interface(), tags...); err != nil {
if err = interceptor.parse(cryptoElement.eleValue.Interface(), cryptoEleQueue, tags...); err != nil {
logger.Error("parse failed:%v", err)
return
}
} else if cryptoElement.eleType == StructPtrSlice { // []*struct
for i := 0; i < cryptoElement.eleValue.Len(); i++ {
err = interceptor.parse(cryptoElement.eleValue.Index(i).Interface(), tags...)
err = interceptor.parse(cryptoElement.eleValue.Index(i).Interface(), cryptoEleQueue, tags...)
if err != nil {
logger.Error("parse failed:%v", err)
return
@ -71,7 +73,7 @@ func (interceptor *CryptoTagInterceptor) Intercept(p any, tags ...string) (err e
}
// 解析标签并写入队列
func (interceptor *CryptoTagInterceptor) parse(p any, tags ...string) error {
func (interceptor *CryptoTagInterceptor) parse(p any, cryptoEleQueue *list.List, tags ...string) error {
pType := reflect.TypeOf(p).Elem() //获取到具体的struct名称
pValue := reflect.ValueOf(p).Elem() //获取到struct的值类型
@ -118,15 +120,15 @@ func (interceptor *CryptoTagInterceptor) parse(p any, tags ...string) error {
//4.拦截*struct入队
} else if util.IsTypeStructPtr(fieldType) && field.Tag.Get(tag) == "true" && !pValue.Field(i).IsNil() {
interceptor.cryptoEleQueue.PushBack(newCryptoElement(StructPtr, pValue.Field(i)))
cryptoEleQueue.PushBack(newCryptoElement(StructPtr, pValue.Field(i)))
//5.拦截[]*Struct入队
} else if util.IsTypeStructPtrSlice(fieldType) && field.Tag.Get(tag) == "true" && !pValue.Field(i).IsNil() {
interceptor.cryptoEleQueue.PushBack(newCryptoElement(StructPtrSlice, pValue.Field(i)))
cryptoEleQueue.PushBack(newCryptoElement(StructPtrSlice, pValue.Field(i)))
//6.拦截struct入队
} else if util.IsTypeStruct(fieldType) && field.Tag.Get(tag) != "" {
interceptor.cryptoEleQueue.PushBack(newCryptoElement(StructPtr, pValue.Field(i).Addr()))
cryptoEleQueue.PushBack(newCryptoElement(StructPtr, pValue.Field(i).Addr()))
}
}