更新redis重试三次,http方法兼容ragflow格式
This commit is contained in:
13
http/http.go
13
http/http.go
@@ -53,6 +53,8 @@ func RouteRegister(controllers []interface{}) {
|
||||
}
|
||||
go Httpserver.Run()
|
||||
}
|
||||
|
||||
// doRequest 统一HTTP请求处理(DELETE用ContentJson发送body,gconv.Struct增加err检查)
|
||||
func doRequest(ctx context.Context, method string, url string, headers map[string]string, target any, data ...any) (err error) {
|
||||
err = utils.ValidStructPtr(target)
|
||||
if err != nil {
|
||||
@@ -65,6 +67,9 @@ func doRequest(ctx context.Context, method string, url string, headers map[strin
|
||||
} else {
|
||||
client.SetHeader("Authorization", g.RequestFromCtx(ctx).GetHeader("Authorization"))
|
||||
}
|
||||
if method == http.MethodDelete && len(data) > 0 { // DELETE请求显式用ContentJson序列化body
|
||||
client = client.ContentJson()
|
||||
}
|
||||
response, err := client.DoRequest(ctx, method, url, data...)
|
||||
if err != nil {
|
||||
return
|
||||
@@ -73,9 +78,13 @@ func doRequest(ctx context.Context, method string, url string, headers map[strin
|
||||
result := response.ReadAll()
|
||||
resultStrut := &ghttp.DefaultHandlerResponse{}
|
||||
|
||||
gconv.Struct(result, &resultStrut)
|
||||
if err = gconv.Struct(result, &resultStrut); err != nil { // 修复:增加err检查
|
||||
return errors.New("响应解析失败: " + err.Error())
|
||||
}
|
||||
if resultStrut.Code == 200 || resultStrut.Code == 0 {
|
||||
gconv.Struct(resultStrut.Data, target)
|
||||
if err = gconv.Struct(resultStrut.Data, target); err != nil { // 修复:增加err检查
|
||||
return errors.New("数据解析失败: " + err.Error())
|
||||
}
|
||||
} else {
|
||||
err = errors.New(resultStrut.Message)
|
||||
}
|
||||
|
||||
@@ -109,7 +109,7 @@ func (m *MongoDB) Find(ctx context.Context, filter bson.M, result interface{}, c
|
||||
redisKey := fmt.Sprintf(redis.List, user.TenantId, collection, filterKey, optionsKey)
|
||||
if m.Cache {
|
||||
var resultStr *gvar.Var
|
||||
resultStr, err = redis.RedisClient.Get(ctx, redisKey)
|
||||
resultStr, err = redis.RedisClient().Get(ctx, redisKey)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@@ -168,7 +168,7 @@ func (m *MongoDB) Find(ctx context.Context, filter bson.M, result interface{}, c
|
||||
return
|
||||
}
|
||||
if m.Cache {
|
||||
err = redis.RedisClient.SetEX(ctx, redisKey, result, int64(time.Hour))
|
||||
err = redis.RedisClient().SetEX(ctx, redisKey, result, int64(time.Hour))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@@ -200,7 +200,7 @@ func (m *MongoDB) FindOne(ctx context.Context, filter bson.M, result interface{}
|
||||
redisKey := fmt.Sprintf(redis.One, user.TenantId, collection, filterKey)
|
||||
if m.Cache {
|
||||
var resultStr *gvar.Var
|
||||
resultStr, err = redis.RedisClient.Get(ctx, redisKey)
|
||||
resultStr, err = redis.RedisClient().Get(ctx, redisKey)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@@ -221,7 +221,7 @@ func (m *MongoDB) FindOne(ctx context.Context, filter bson.M, result interface{}
|
||||
err = nil
|
||||
}
|
||||
if m.Cache {
|
||||
err = redis.RedisClient.SetEX(ctx, redisKey, result, int64(time.Hour))
|
||||
err = redis.RedisClient().SetEX(ctx, redisKey, result, int64(time.Hour))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -231,23 +231,23 @@ func (m *MongoDB) FindOne(ctx context.Context, filter bson.M, result interface{}
|
||||
|
||||
func (m *MongoDB) CleanRedis(ctx context.Context, filter bson.M, tenantId interface{}, collection string) (err error) {
|
||||
listKeys := fmt.Sprintf(redis.CleanList, tenantId, collection)
|
||||
keys, err := redis.RedisClient.Keys(ctx, listKeys)
|
||||
keys, err := redis.RedisClient().Keys(ctx, listKeys)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
for _, key := range keys {
|
||||
_, err = redis.RedisClient.Del(ctx, key)
|
||||
_, err = redis.RedisClient().Del(ctx, key)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
countKeys := fmt.Sprintf(redis.CleanCount, tenantId, collection)
|
||||
keys, err = redis.RedisClient.Keys(ctx, countKeys)
|
||||
keys, err = redis.RedisClient().Keys(ctx, countKeys)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
for _, key := range keys {
|
||||
_, err = redis.RedisClient.Del(ctx, key)
|
||||
_, err = redis.RedisClient().Del(ctx, key)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@@ -256,7 +256,7 @@ func (m *MongoDB) CleanRedis(ctx context.Context, filter bson.M, tenantId interf
|
||||
delete(filter, "tenantId")
|
||||
filterKey := fmt.Sprintf("%+v", filter)
|
||||
oneKey := fmt.Sprintf(redis.One, tenantId, collection, filterKey)
|
||||
_, err = redis.RedisClient.Del(ctx, oneKey)
|
||||
_, err = redis.RedisClient().Del(ctx, oneKey)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@@ -525,7 +525,7 @@ func (m *MongoDB) Count(ctx context.Context, filter bson.M, collection string) (
|
||||
redisKey := fmt.Sprintf(redis.Count, user.TenantId, collection, filterKey)
|
||||
if m.Cache {
|
||||
var resultStr *gvar.Var
|
||||
resultStr, err = redis.RedisClient.Get(ctx, redisKey)
|
||||
resultStr, err = redis.RedisClient().Get(ctx, redisKey)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@@ -536,7 +536,7 @@ func (m *MongoDB) Count(ctx context.Context, filter bson.M, collection string) (
|
||||
}
|
||||
count, err = db.Collection(collection).CountDocuments(ctx, filter)
|
||||
if m.Cache {
|
||||
err = redis.RedisClient.SetEX(ctx, redisKey, count, int64(time.Hour))
|
||||
err = redis.RedisClient().SetEX(ctx, redisKey, count, int64(time.Hour))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@ package redis
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
@@ -15,60 +14,27 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
// redisClient 内部使用的 Redis 客户端(单例模式)
|
||||
redisClient *gredis.Redis
|
||||
redisOnce sync.Once
|
||||
)
|
||||
|
||||
// getClient 获取 Redis 客户端(延迟初始化)
|
||||
func getClient() *gredis.Redis {
|
||||
// RedisClient 获取Redis客户端(支持重试3次,每次间隔2秒)
|
||||
func RedisClient() *gredis.Redis {
|
||||
redisOnce.Do(func() {
|
||||
for i := 0; i < 3; i++ {
|
||||
redisClient = g.Redis()
|
||||
if redisClient != nil {
|
||||
ctx := context.Background()
|
||||
if _, err := redisClient.Do(ctx, "PING"); err == nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
time.Sleep(2 * time.Second)
|
||||
}
|
||||
})
|
||||
return redisClient
|
||||
}
|
||||
|
||||
// GetRedisClient 获取 Redis 客户端(供外部使用)
|
||||
func GetRedisClient() *gredis.Redis {
|
||||
return getClient()
|
||||
}
|
||||
|
||||
// RedisClient 导出的 Redis 客户端(供 mongo.go 使用,兼容旧代码)
|
||||
var RedisClient = getClient()
|
||||
|
||||
// Lock 分布式锁
|
||||
func Lock(ctx context.Context, key string, expireSeconds int64, fn func(ctx context.Context) error) (success bool, err error) {
|
||||
limit := 3
|
||||
LOOP:
|
||||
if limit < 0 {
|
||||
return false, errors.New("锁重试次数耗尽")
|
||||
}
|
||||
limit--
|
||||
if val, err := RedisClient.Set(ctx, key, true, gredis.SetOption{
|
||||
TTLOption: gredis.TTLOption{
|
||||
EX: &expireSeconds,
|
||||
},
|
||||
NX: true,
|
||||
}); err != nil {
|
||||
return false, err
|
||||
} else {
|
||||
if val.Bool() {
|
||||
defer func(RedisClient *gredis.Redis, ctx context.Context, key string) {
|
||||
if _, err = RedisClient.Del(ctx, key); err != nil {
|
||||
glog.Errorf(ctx, "RedisClient.Del error: %v", err)
|
||||
}
|
||||
}(RedisClient, ctx, key)
|
||||
if err = fn(ctx); err != nil {
|
||||
return false, err
|
||||
}
|
||||
return true, nil
|
||||
} else {
|
||||
time.Sleep(time.Second)
|
||||
goto LOOP
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func GetReadStream(ctx context.Context, msg ...QueueMessage) error {
|
||||
for _, t := range msg {
|
||||
err := GetReadFromStream(ctx, t.StreamKey, t.GroupName, t.ConsumerName, t.BatchSize, t.BlockMs, t.AutoAck, t.HandleFunc)
|
||||
|
||||
Reference in New Issue
Block a user