locker

package module
v2.0.2 Latest Latest
Warning

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

Go to latest
Published: Feb 6, 2023 License: MIT Imports: 5 Imported by: 0

README

Locker

Build Status GoDoc Go Report Card Software License

This is the stable version.

Description

  • FileMutex - To lock via operating system file
    • Supports functions:
      • Lock
      • TryLock
      • Unlock
    • Important:
      • thread safe - You cannot take several locks from one mutex within a single running application.
Supports platforms
  • unix (linux, mac)
  • windows
  • other

Installation

$ go get github.com/mantyr/locker

Example

Note

You will probably be protecting the entire application, rather than a single object.


type Storage struct {
	mutex locker.Mutex
}

func New(fileLock string) (*Storage, error) {
	mutex, err := locker.NewFileMutex(fileLock)
	if err != nil {
		return nil, err
	}
	return &Storage{
		mutex: mutex,
	}, nil
}

func (s *Storage) Action() (err error) {
	defer s.mutex.Unlock()
	err = s.mutex.Lock()
	if err != nil {
		return err
	}
	fmt.Println("action")
	return nil
}

go run ./example/main.go lock ./testdata/file.lock
go run ./example/main.go try-lock ./testdata/file.lock
go run ./example/main.go multi-lock ./testdata/file.lock
go run ./example/main.go unlock ./testdata/file.lock

Author

Oleg Shevelev

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Mutex

type Mutex interface {
	Lock() error
	TryLock() (bool, error)
	Unlock() error
}

func NewFileMutex

func NewFileMutex(address string) (Mutex, error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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