Skip to content
/ golock Public

Very simple file locking with optional timeouts. 🔒 📄

License

Notifications You must be signed in to change notification settings

schollz/golock

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

golock

travis go report card coverage godocs

Very simple (<100 LOC) file locking with optional timeouts.

Install

go get github.com/schollz/golock

Usage

Initialize the lock and then obtain it. If you specify the timeout and interval, it will poll at the given interval for the specified time until it successfully gets a lock, otherwise throw an error. If you don't specify the timeout, then it will throw an error immediately if it does not obtain the lock.

If you get no errors from locking, then you are good to go. Make sure to unlock it when you are done.

// first initiate lockfile
l := golock.New(
    golock.OptionSetName("mylockfile"), 
    golock.OptionSetInterval(1*time.Millisecond), 
    golock.OptionSetTimeout(60*time.Second),
)

// lock it
err := l.Lock()
if err != nil {
    // error means we didn't get the lock
    // handle it
}

// do stuff

// unlock it
err = l.Unlock()
if err != nil {
    panic(err)
}

Benchmarks

goos: linux
goarch: amd64
pkg: github.com/schollz/golock
BenchmarkLocking-4     200000     12224 ns/op    128 B/op    5 allocs/op

Contributing

Pull requests are welcome. Feel free to...

  • Revise documentation
  • Add new features
  • Fix bugs
  • Suggest improvements

License

MIT

About

Very simple file locking with optional timeouts. 🔒 📄

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages