etcdlock

package module
Version: v0.0.0-...-32f65b8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 6, 2017 License: MIT Imports: 8 Imported by: 0

README

etcd-lock

Build Status Coverage Status

Distributed locks powered by etcd v3 for Go.

Installation

go get -u github.com/DavidCai1993/etcd-lock

Documentation

API documentation can be found here: https://godoc.org/github.com/DavidCai1993/etcd-lock

Usage

import (
  "github.com/DavidCai1993/etcd-lock"
)
locker, err := etcdlock.NewLocker(etcdlock.LockerOptions{
  Address:        "127.0.0.1:2379",
  DialOptions:    []grpc.DialOption{grpc.WithInsecure()},
})

if err != nil {
  log.Fatalln(err)
}

// Acquire a lock for a specified recource.
if _, err = locker.Lock(context.Background(), "resource_key", 5*time.Second); err != nil {
  log.Fatalln(err)
}

// This lock will be acquired after 5s, and before that current goroutine
// will be blocked.
anotherLock, err := locker.Lock(context.Background(), "resource_key", 5*time.Second)
if err != nil {
  log.Fatalln(err)
}

// Unlock the lock manually.
if err := anotherLock.Unlock(context.Background()); err != nil {
  log.Fatalln(err)
}

Documentation

Overview

Example
locker, err := etcdlock.NewLocker(etcdlock.LockerOptions{
	Address:     "127.0.0.1:2379",
	DialOptions: []grpc.DialOption{grpc.WithInsecure()},
})

if err != nil {
	log.Fatalln(err)
}

// Acquire a lock for a specified recource.
if _, err = locker.Lock(context.Background(), "resource_key", 5*time.Second); err != nil {
	log.Fatalln(err)
}

// This lock will be acquired after 5s, and before that current goroutine
// will be blocked.
anotherLock, err := locker.Lock(context.Background(), "resource_key", 3*time.Second)
if err != nil {
	log.Fatalln(err)
}

// Unlock the lock manually.
if err := anotherLock.Unlock(context.Background()); err != nil {
	log.Fatalln(err)
}
Output:

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrEmptyKey = errors.New("empty key")
)

Exposed errors.

Functions

This section is empty.

Types

type Lock

type Lock struct {
	// contains filtered or unexported fields
}

Lock is a distributed lock of specified resource which was acquired from etcd v3.

func (*Lock) Unlock

func (l *Lock) Unlock(ctx context.Context) error

Unlock unlocks this lock.

type Locker

type Locker struct {
	// contains filtered or unexported fields
}

Locker is the client for acquiring distributed locks from etcd. It should be created from NewLocker() function.

func NewLocker

func NewLocker(options LockerOptions) (*Locker, error)

NewLocker creates a Locker according to the given options.

func (*Locker) IsLocked

func (l *Locker) IsLocked(ctx context.Context, keyName string) (bool, error)

IsLocked checks whether the specified resource has already been locked.

func (*Locker) Lock

func (l *Locker) Lock(ctx context.Context, keyName string, timeout time.Duration) (*Lock, error)

Lock acquires a distributed lock for the specified resource from etcd v3.

Example
locker, err := etcdlock.NewLocker(etcdlock.LockerOptions{
	Address:     "127.0.0.1:2379",
	DialOptions: []grpc.DialOption{grpc.WithInsecure()},
})

if err != nil {
	log.Fatalln(err)
}

// This lock will be expired in 3 seconds.
if _, err := locker.Lock(context.Background(), "resource_key", 3*time.Second); err != nil {
	log.Fatalln(err)
}
Output:

type LockerOptions

type LockerOptions struct {
	// The address of etcd(v3) server.
	Address string
	// Options used for `grpc.Dial`.
	DialOptions []grpc.DialOption
	// Prefix of the keys of locks in etcd, by default is "__etcd_lock/"
	EtcdKeyPrefix string
}

LockerOptions is the options for NewLocker() function.

Source Files

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto