Skip to content

Commit

Permalink
fix(redis): add error on fetch (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
shahabeshaghi67 authored Jun 26, 2022
1 parent a09078d commit d492d62
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions redis/shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var keysList = make(map[*redis.Client]map[string]*redsync.Mutex)
func SharedFetch[K any](ctx context.Context, client *redis.Client, key string, t, retryT time.Duration, retry int, f func() (K, error)) (K, error) {
var res K
redisRes := client.Get(ctx, key)
go Fetch(ctx, client, key, t, retryT, retry, f)
go func() { _ = Fetch(ctx, client, key, t, retryT, retry, f) }()
err := redisRes.Err()
if err != nil {
return res, err
Expand All @@ -42,17 +42,17 @@ func SharedFetch[K any](ctx context.Context, client *redis.Client, key string, t
return res, nil
}

func Fetch[K any](ctx context.Context, client *redis.Client, key string, t, retryT time.Duration, retry int, f func() (K, error)) {
func Fetch[K any](ctx context.Context, client *redis.Client, key string, t, retryT time.Duration, retry int, f func() (K, error)) error {
lock, err := CheckLock(client, key)
if err != nil {
logrus.Infof("goxy:SharedFetch:lock:%s", err)
return
return err
}
opt := redsync.WithExpiry(retryT)
opt.Apply(lock)
if err := lock.Lock(); err != nil {
logrus.Infof("goxy:SharedFetch:lock:%s", err)
return
return err
}
var res K
for i := 0; i < retry; i++ {
Expand All @@ -62,24 +62,25 @@ func Fetch[K any](ctx context.Context, client *redis.Client, key string, t, retr
}
}
if err != nil {
_, err = lock.Unlock()
if err != nil {
logrus.Errorf("goxy:SharedFetch:unlock:%s", err)
_, lErr := lock.Unlock()
if lErr != nil {
logrus.Errorf("goxy:SharedFetch:unlock:%s", lErr)
}
logrus.Errorf("goxy:SharedFetch:fetch(after %d times):%s:%s", retry, key, err.Error())
return
return err
}
redisSetVal, err := goxy.Encode(res)
if err != nil {
logrus.Errorf("goxy:SharedFetch:encode:%s", err.Error())
return
return err
}
err = client.Set(ctx, key, redisSetVal, t).Err()
if err != nil {
logrus.Errorf("goxy:SharedFetch:Set:%s:%s", key, err.Error())
return
return err
}
logrus.Infof("goxy:SharedFetch:Set:%s:Successfully", key)
return nil
}

func CheckLock(client *redis.Client, key string) (*redsync.Mutex, error) {
Expand Down

0 comments on commit d492d62

Please sign in to comment.