shluz

package module
v0.0.0-...-c6152b2 Latest Latest
Warning

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

Go to latest
Published: Dec 23, 2019 License: Apache-2.0 Imports: 8 Imported by: 0

README

Shluz

Shluz is a locking library for go.

Currently only file locks are supported, processes that use locks should work on a single host and use the same locks directory (see shluz.Init function).

Installation

go get -u github.com/flant/shluz

Usage

import "github.com/flant/shluz"

func main() {
	if err := shluz.Init("locks_service_dir"); err != nil {
		fmt.Fprintf(os.Stderr, "ERROR: failed to init shluz: %s\n", err)
		os.Exit(1)
	}

	// Case 1

	if err := Shluz.Lock("myresource", Shluz.LockOptions{ReadOnly: false, Timeout: 30*time.Second}); err != nil {
		fmt.Fprintf(os.Stderr, "ERROR: failed to lock myresource: %s\n", err)
		os.Exit(1)
	}

	// ...

	if err := Shluz.Unlock("myresource"); err != nil {
		fmt.Fprintf(os.Stderr, "ERROR: failed to unlock myresource: %s\n", err)
		os.Exit(1)
	}

	// Case 2

	if err := Shluz.WithLock("myresource", Shluz.LockOptions{ReadOnly: false, Timeout: 30*time.Second}, func() error {
		// ...
	}); err != nil {
		fmt.Fprintf(os.Stderr, "ERROR: failed to perform action with locked myresource: %s\n", err)
		os.Exit(1)
	}

	// Case 3

	locked, err := Shluz.TryLock("myresource", Shluz.TryLockOptions{ReadOnly: false})
	if err != nil {
		fmt.Fprintf(os.Stderr, "ERROR: failed to lock myresource: %s\n", err)
		os.Exit(1)
	}

	if locked {
		// ...
	} else {
		// ...
	}

	if err := Shluz.Unlock("myresource"); err != nil {
		fmt.Fprintf(os.Stderr, "ERROR: failed to unlock myresource: %s\n", err)
		os.Exit(1)
	}
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	LocksDir       string
	Locks          map[string]LockObject
	DefaultTimeout = 24 * time.Hour
)

Functions

func Init

func Init(locksDir string) error

func Lock

func Lock(name string, opts LockOptions) error

func MurmurHash

func MurmurHash(args ...string) string

func TryLock

func TryLock(name string, opts TryLockOptions) (bool, error)

func Unlock

func Unlock(name string) error

func WithLock

func WithLock(name string, opts LockOptions, f func() error) error

Types

type BaseLock

type BaseLock struct {
	Name        string
	ActiveLocks int
}

func (*BaseLock) GetName

func (lock *BaseLock) GetName() string

func (*BaseLock) Lock

func (lock *BaseLock) Lock(l locker) error

func (*BaseLock) TryLock

func (lock *BaseLock) TryLock(l locker) (bool, error)

func (*BaseLock) Unlock

func (lock *BaseLock) Unlock(l locker) error

func (*BaseLock) WithLock

func (lock *BaseLock) WithLock(locker locker, f func() error) (resErr error)

type FileLock

type FileLock struct {
	BaseLock
	LocksDir string
	// contains filtered or unexported fields
}

func (*FileLock) Lock

func (lock *FileLock) Lock(timeout time.Duration, readOnly bool, onWait func(doWait func() error) error) error

func (*FileLock) LockFilePath

func (lock *FileLock) LockFilePath() string

func (*FileLock) TryLock

func (lock *FileLock) TryLock(readOnly bool) (bool, error)

func (*FileLock) Unlock

func (lock *FileLock) Unlock() error

func (*FileLock) WithLock

func (lock *FileLock) WithLock(timeout time.Duration, readOnly bool, onWait func(doWait func() error) error, f func() error) error

type LockObject

type LockObject interface {
	GetName() string
	TryLock(readOnly bool) (bool, error)
	Lock(timeout time.Duration, readOnly bool, onWait func(doWait func() error) error) error
	Unlock() error
	WithLock(timeout time.Duration, readOnly bool, onWait func(doWait func() error) error, f func() error) error
}

func NewFileLock

func NewFileLock(name string, locksDir string) LockObject

type LockOptions

type LockOptions struct {
	Timeout  time.Duration
	ReadOnly bool
}

type TryLockOptions

type TryLockOptions struct {
	ReadOnly bool
}

Jump to

Keyboard shortcuts

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