single

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: May 10, 2022 License: MIT Imports: 5 Imported by: 11

README

Go Report Card Coverage Status Build Status GoDoc

Single

Single ensures that only one instance of your program is running

Usage

Use a persistent *Single object for the duration of your program, or Go's garbage collector will automatically close the lockfile.

Documentation

Overview

Package single provides a mechanism to ensure, that only one instance of a program is running

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// ErrAlreadyRunning another instance of is already running
	ErrAlreadyRunning = errors.New("another instance is already running")
)

Functions

This section is empty.

Types

type Option

type Option func(*Single)

Option configures Single

func WithLockPath

func WithLockPath(lockpath string) Option

WithLockPath configures the path for the lockfile

type Single

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

Single represents the name and the open file descriptor

func New

func New(name string, opts ...Option) (*Single, error)

New creates a Single instance where name is the basename of the lock file (<name>.lock) if no path is given (WithLockPath option) the lock will be created in an operating specific path as <name>.lock

Example

ExampleNew is an example how to use single

// create a new lockfile in /var/lock/filename
one, err := New("filename", WithLockPath("/tmp"))
if err != nil {
	log.Fatal(err)
}

// lock and defer unlocking
if err := one.Lock(); err != nil {
	log.Fatal(err)
}

// run

if err := one.Unlock(); err != nil {
	log.Println(err)
}
Output:

func (*Single) Lock

func (s *Single) Lock() error

Lock tries to obtain an exclude lock on a lockfile and exits the program if an error occurs

func (*Single) Lockfile

func (s *Single) Lockfile() string

Lockfile returns the full path of the lock file

func (*Single) Unlock

func (s *Single) Unlock() error

Unlock releases the lock, closes and removes the lockfile

Jump to

Keyboard shortcuts

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