diff --git a/redislock.go b/redislock.go index b1b00b5..68fdfff 100644 --- a/redislock.go +++ b/redislock.go @@ -49,9 +49,13 @@ func New(client RedisClient) *Client { return &Client{client: client} } +type RedisLocker interface { + Obtain(ctx context.Context, key string, ttl time.Duration, opt *Options) (RedisLock, error) +} + // Obtain tries to obtain a new lock using a key with the given TTL. // May return ErrNotObtained if not successful. -func (c *Client) Obtain(ctx context.Context, key string, ttl time.Duration, opt *Options) (*Lock, error) { +func (c *Client) Obtain(ctx context.Context, key string, ttl time.Duration, opt *Options) (RedisLock, error) { // Create a random token token, err := c.randomToken() if err != nil { @@ -113,6 +117,15 @@ func (c *Client) randomToken() (string, error) { // -------------------------------------------------------------------- +type RedisLock interface { + Key() string + Token() string + Metadata() string + TTL(ctx context.Context) (time.Duration, error) + Refresh(ctx context.Context, ttl time.Duration, opt *Options) error + Release(ctx context.Context) error +} + // Lock represents an obtained, distributed lock. type Lock struct { client *Client @@ -121,7 +134,7 @@ type Lock struct { } // Obtain is a short-cut for New(...).Obtain(...). -func Obtain(ctx context.Context, client RedisClient, key string, ttl time.Duration, opt *Options) (*Lock, error) { +func Obtain(ctx context.Context, client RedisClient, key string, ttl time.Duration, opt *Options) (RedisLock, error) { return New(client).Obtain(ctx, key, ttl, opt) }