recipes

package
v0.0.0-...-4045ee1 Latest Latest
Warning

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

Go to latest
Published: Mar 15, 2016 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrLockFound = errors.New("lock found")
	ErrNotLocked = errors.New("not locked")
)

Functions

func CleanOlderLocks

func CleanOlderLocks(client *ezk.Client, base string, t time.Duration) error

CleanOlderLocks removes the locks in a path that are older than t.

Types

type Lock

type Lock struct {
	Path   string
	MaxAge time.Duration
	// contains filtered or unexported fields
}

func NewLock

func NewLock(client *ezk.Client, path string, acl []zk.ACL) *Lock

NewLock initializes a new distributed lock.

func (*Lock) Lock

func (l *Lock) Lock() error

Lock implements a distributed lock on zookeeper where only one instance will be able to proceed while the rest of them will wait.

func (*Lock) TryLock

func (l *Lock) TryLock() error

TryLock will attempt to acquire the lock only if it is free at the time of invocation. If it's free it will return nil, if not it will return the error recipes.ErrLockFound unless any other error is found.

func (*Lock) Unlock

func (l *Lock) Unlock() error

Unlock removes the current lock znode.

func (*Lock) WithCleaner

func (l *Lock) WithCleaner(t time.Duration)

WithCleaner adds a cleaner to the lock, this will remove all the files in the Lock.Path older than t.

WithCleaner is used to support special cases where we want to execute the code protected by one path twice when the user holding the lock does not unlock it in less than t. The initial user holding the lock will know that his lock was removed because the Unlock will return the zk.ErrNoNode error.

type PathWatcher

type PathWatcher struct {
	Path string
	// contains filtered or unexported fields
}

PathWatcher is a recipe that watches for child events in a ZNode. It will trigger an event when a child is created or deleted.

Usage:

client := ezk.NewClient(ezk.ClientConfig{})
watcher := recipes.NewPathWatcher(client, "/")
watcher.Start()
go func() {
	for {
		select {
			case e, ok := <-watcher.Event():
			if !ok {
				return
			}
			if e.Type == zk.EventNodeChildrenChanged {
				// do something
			}

			case err := <-watcher.Error():
				// do something
		}
	}
}()
...
// Stop the watcher
watcher.Stop()

func NewPathWatcher

func NewPathWatcher(client *ezk.Client, path string) *PathWatcher

NewPathWatcher creates a new PathWatcher on the passed path.

func (*PathWatcher) Error

func (p *PathWatcher) Error() <-chan error

Error is the channel used to listen for errors.

func (*PathWatcher) Event

func (p *PathWatcher) Event() <-chan zk.Event

Event returns the channel used to listen for child events.

func (*PathWatcher) Start

func (p *PathWatcher) Start()

Start starts PathWatcher. It will send the events to event channel and the errors to th error channel.

func (*PathWatcher) Stop

func (p *PathWatcher) Stop()

Stop stops the watcher. Stop will close the event channel and return once the watcher finishes.

Jump to

Keyboard shortcuts

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