Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added interfaces #36

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions redislock.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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)
}

Expand Down