redis lock

This commit is contained in:
李子铭 2025-03-20 11:27:35 +08:00
parent ec15531eac
commit 44ca97b7d0
1 changed files with 2 additions and 1 deletions

View File

@ -2,6 +2,7 @@ package lock
import ( import (
"context" "context"
"fmt"
"github.com/redis/go-redis/v9" "github.com/redis/go-redis/v9"
"time" "time"
) )
@ -21,7 +22,7 @@ func (c *Mutex) Lock(ctx context.Context, resource string, callback func(ctx con
l := c.locker.GetLock(resource) l := c.locker.GetLock(resource)
if err := l.Lock(ctx); err != nil { if err := l.Lock(ctx); err != nil {
return err return fmt.Errorf("lock failed: %w", err)
} }
defer func() { defer func() {
_ = l.Unlock(ctx) _ = l.Unlock(ctx)