package singleinstance
import "git.sr.ht/~pingoo/stdx/singleinstance"
Package singleinstance provides a mechanism to ensure, that only one instance of a program is running
Index ¶
Examples ¶
Variables ¶
var ( // ErrAlreadyRunning another instance of is already running ErrAlreadyRunning = errors.New("another instance is already running") )
Types ¶
type Option ¶
type Option func(*SingleInstance)
Option configures Single
func WithLockPath ¶
WithLockPath configures the path for the lockfile
type SingleInstance ¶
type SingleInstance struct {
// contains filtered or unexported fields
}
SingleInstance represents the name and the open file descriptor
func New ¶
func New(name string, opts ...Option) *SingleInstance
New creates a SingleInstance 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
panics if namr is empty
ExampleNew is an example how to use singleinstance
Code:
Example¶
{
// create a new lockfile in /var/lock/filename
one := New("filename", WithLockPath("/tmp"))
// lock and defer unlocking
if err := one.Lock(); err != nil {
log.Fatal(err)
}
// run
if err := one.Unlock(); err != nil {
log.Println(err)
}
}
func (*SingleInstance) Lock ¶
func (s *SingleInstance) Lock() error
Lock tries to obtain an exclude lock on a lockfile and exits the program if an error occurs
func (*SingleInstance) Lockfile ¶
func (s *SingleInstance) Lockfile() string
Lockfile returns the full path of the lock file
func (*SingleInstance) Unlock ¶
func (s *SingleInstance) Unlock() error
Unlock releases the lock, closes and removes the lockfile
Source Files ¶
singleinstance.go singleinstance_unix.go
- Version
- v0.0.0-20240218134121-094174641f6e (latest)
- Published
- Feb 18, 2024
- Platform
- linux/amd64
- Imports
- 5 packages
- Last checked
- 4 months ago –
Tools for package owners.