更新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()
|
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) {
|
func doRequest(ctx context.Context, method string, url string, headers map[string]string, target any, data ...any) (err error) {
|
||||||
err = utils.ValidStructPtr(target)
|
err = utils.ValidStructPtr(target)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -65,6 +67,9 @@ func doRequest(ctx context.Context, method string, url string, headers map[strin
|
|||||||
} else {
|
} else {
|
||||||
client.SetHeader("Authorization", g.RequestFromCtx(ctx).GetHeader("Authorization"))
|
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...)
|
response, err := client.DoRequest(ctx, method, url, data...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
@@ -73,9 +78,13 @@ func doRequest(ctx context.Context, method string, url string, headers map[strin
|
|||||||
result := response.ReadAll()
|
result := response.ReadAll()
|
||||||
resultStrut := &ghttp.DefaultHandlerResponse{}
|
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 {
|
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 {
|
} else {
|
||||||
err = errors.New(resultStrut.Message)
|
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)
|
redisKey := fmt.Sprintf(redis.List, user.TenantId, collection, filterKey, optionsKey)
|
||||||
if m.Cache {
|
if m.Cache {
|
||||||
var resultStr *gvar.Var
|
var resultStr *gvar.Var
|
||||||
resultStr, err = redis.RedisClient.Get(ctx, redisKey)
|
resultStr, err = redis.RedisClient().Get(ctx, redisKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -168,7 +168,7 @@ func (m *MongoDB) Find(ctx context.Context, filter bson.M, result interface{}, c
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
if m.Cache {
|
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 {
|
if err != nil {
|
||||||
return
|
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)
|
redisKey := fmt.Sprintf(redis.One, user.TenantId, collection, filterKey)
|
||||||
if m.Cache {
|
if m.Cache {
|
||||||
var resultStr *gvar.Var
|
var resultStr *gvar.Var
|
||||||
resultStr, err = redis.RedisClient.Get(ctx, redisKey)
|
resultStr, err = redis.RedisClient().Get(ctx, redisKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -221,7 +221,7 @@ func (m *MongoDB) FindOne(ctx context.Context, filter bson.M, result interface{}
|
|||||||
err = nil
|
err = nil
|
||||||
}
|
}
|
||||||
if m.Cache {
|
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 {
|
if err != nil {
|
||||||
return err
|
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) {
|
func (m *MongoDB) CleanRedis(ctx context.Context, filter bson.M, tenantId interface{}, collection string) (err error) {
|
||||||
listKeys := fmt.Sprintf(redis.CleanList, tenantId, collection)
|
listKeys := fmt.Sprintf(redis.CleanList, tenantId, collection)
|
||||||
keys, err := redis.RedisClient.Keys(ctx, listKeys)
|
keys, err := redis.RedisClient().Keys(ctx, listKeys)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
for _, key := range keys {
|
for _, key := range keys {
|
||||||
_, err = redis.RedisClient.Del(ctx, key)
|
_, err = redis.RedisClient().Del(ctx, key)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
countKeys := fmt.Sprintf(redis.CleanCount, tenantId, collection)
|
countKeys := fmt.Sprintf(redis.CleanCount, tenantId, collection)
|
||||||
keys, err = redis.RedisClient.Keys(ctx, countKeys)
|
keys, err = redis.RedisClient().Keys(ctx, countKeys)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
for _, key := range keys {
|
for _, key := range keys {
|
||||||
_, err = redis.RedisClient.Del(ctx, key)
|
_, err = redis.RedisClient().Del(ctx, key)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -256,7 +256,7 @@ func (m *MongoDB) CleanRedis(ctx context.Context, filter bson.M, tenantId interf
|
|||||||
delete(filter, "tenantId")
|
delete(filter, "tenantId")
|
||||||
filterKey := fmt.Sprintf("%+v", filter)
|
filterKey := fmt.Sprintf("%+v", filter)
|
||||||
oneKey := fmt.Sprintf(redis.One, tenantId, collection, filterKey)
|
oneKey := fmt.Sprintf(redis.One, tenantId, collection, filterKey)
|
||||||
_, err = redis.RedisClient.Del(ctx, oneKey)
|
_, err = redis.RedisClient().Del(ctx, oneKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
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)
|
redisKey := fmt.Sprintf(redis.Count, user.TenantId, collection, filterKey)
|
||||||
if m.Cache {
|
if m.Cache {
|
||||||
var resultStr *gvar.Var
|
var resultStr *gvar.Var
|
||||||
resultStr, err = redis.RedisClient.Get(ctx, redisKey)
|
resultStr, err = redis.RedisClient().Get(ctx, redisKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
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)
|
count, err = db.Collection(collection).CountDocuments(ctx, filter)
|
||||||
if m.Cache {
|
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 {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ package redis
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
@@ -15,60 +14,27 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
// redisClient 内部使用的 Redis 客户端(单例模式)
|
|
||||||
redisClient *gredis.Redis
|
redisClient *gredis.Redis
|
||||||
redisOnce sync.Once
|
redisOnce sync.Once
|
||||||
)
|
)
|
||||||
|
|
||||||
// getClient 获取 Redis 客户端(延迟初始化)
|
// RedisClient 获取Redis客户端(支持重试3次,每次间隔2秒)
|
||||||
func getClient() *gredis.Redis {
|
func RedisClient() *gredis.Redis {
|
||||||
redisOnce.Do(func() {
|
redisOnce.Do(func() {
|
||||||
|
for i := 0; i < 3; i++ {
|
||||||
redisClient = g.Redis()
|
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
|
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 {
|
func GetReadStream(ctx context.Context, msg ...QueueMessage) error {
|
||||||
for _, t := range msg {
|
for _, t := range msg {
|
||||||
err := GetReadFromStream(ctx, t.StreamKey, t.GroupName, t.ConsumerName, t.BatchSize, t.BlockMs, t.AutoAck, t.HandleFunc)
|
err := GetReadFromStream(ctx, t.StreamKey, t.GroupName, t.ConsumerName, t.BatchSize, t.BlockMs, t.AutoAck, t.HandleFunc)
|
||||||
|
|||||||
Reference in New Issue
Block a user