lock

package
v0.6.2 Latest Latest
Warning

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

Go to latest
Published: Jun 28, 2018 License: Apache-2.0 Imports: 8 Imported by: 5

Documentation

Index

Constants

View Source
const (

	// SemaphorePrefix is the key in etcd where the semaphore will be stored
	SemaphorePrefix = keyPrefix + "/" + semaphoreBranch
)

Variables

View Source
var (
	// ErrExist is the error returned if a holder with the specified id
	// is already holding the semaphore
	ErrExist = errors.New("holder exists")
	// ErrNotExist is the error returned if there is no holder with the
	// specified id holding the semaphore
	ErrNotExist = errors.New("holder does not exist")
)

Functions

This section is empty.

Types

type EtcdLockClient

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

EtcdLockClient is a wrapper around the etcd client that provides simple primitives to operate on the internal semaphore and holders structs through etcd.

func NewEtcdLockClient

func NewEtcdLockClient(keyapi KeysAPI, group string) (*EtcdLockClient, error)

NewEtcdLockClient creates a new EtcdLockClient. The group parameter defines the etcd key path in which the client will manipulate the semaphore. If the group is the empty string, the default semaphore will be used.

func (*EtcdLockClient) Get

func (c *EtcdLockClient) Get() (*Semaphore, error)

Get fetches the Semaphore from etcd.

func (*EtcdLockClient) Init

func (c *EtcdLockClient) Init() error

Init sets an initial copy of the semaphore if it doesn't exist yet.

func (*EtcdLockClient) Set

func (c *EtcdLockClient) Set(sem *Semaphore) error

Set sets a Semaphore in etcd.

type KeysAPI added in v0.4.0

type KeysAPI interface {
	Get(ctx context.Context, key string, opts *client.GetOptions) (*client.Response, error)
	Set(ctx context.Context, key, value string, opts *client.SetOptions) (*client.Response, error)
	Create(ctx context.Context, key, value string) (*client.Response, error)
}

KeysAPI is the minimum etcd client.KeysAPI interface EtcdLockClient needs to do its job.

type Lock

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

Lock takes care of locking in generic clients

func New

func New(id string, client LockClient) (lock *Lock)

New returns a new lock with the provided arguments

func (*Lock) Get

func (l *Lock) Get() (sem *Semaphore, err error)

Get returns the current semaphore value if the underlying client returns an error, Get passes it through

func (*Lock) Lock

func (l *Lock) Lock() (err error)

Lock adds this lock id as a holder to the semaphore it will return an error if there is a problem getting or setting the semaphore, or if the maximum number of holders has been reached, or if a lock with this id is already a holder

func (*Lock) SetMax

func (l *Lock) SetMax(max int) (sem *Semaphore, oldMax int, err error)

SetMax sets the maximum number of holders the semaphore will allow it returns the current semaphore and the previous maximum if there is a problem getting or setting the semaphore, this function will pass on errors from the underlying client

func (*Lock) Unlock

func (l *Lock) Unlock() error

Unlock removes this lock id as a holder of the semaphore it returns an error if there is a problem getting or setting the semaphore, or if this lock is not locked.

type LockClient

type LockClient interface {
	Init() error
	Get() (*Semaphore, error)
	Set(*Semaphore) error
}

LockClient is a generic interface for a lock

type Semaphore

type Semaphore struct {
	Index     uint64   `json:"-"`
	Semaphore int      `json:"semaphore"`
	Max       int      `json:"max"`
	Holders   []string `json:"holders"`
}

Semaphore is a struct representation of the information held by the semaphore

func (*Semaphore) Lock

func (s *Semaphore) Lock(h string) error

Lock adds a holder with id h to the semaphore It adds the id h to the list of holders, returning ErrExist the id already exists, then it subtracts one from the semaphore. If the semaphore is already held by the maximum number of people it returns an error.

func (*Semaphore) SetMax

func (s *Semaphore) SetMax(max int) error

SetMax sets the maximum number of holders of the semaphore

func (*Semaphore) String

func (s *Semaphore) String() string

String returns a json representation of the semaphore if there is an error when marshalling the json, it is ignored and the empty string is returned.

func (*Semaphore) Unlock

func (s *Semaphore) Unlock(h string) error

Unlock removes a holder with id h from the semaphore It removes the id h from the list of holders, returning ErrNotExist if the id does not exist in the list, then adds one to the semaphore.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL