Skip to content

Commit

Permalink
Update docs that are missing LockType argument
Browse files Browse the repository at this point in the history
  • Loading branch information
sysread committed Oct 24, 2024
1 parent b684d50 commit e734664
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 78 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ if err != nil {
log.Fatalf("failed to create touch file: %v", err)
}

if err := tf.Lock(ctx); err != nil {
if err := tf.Lock(ctx, touchfile.Exclusive); err != nil {
log.Fatalf("failed to acquire lock: %v", err)
}
defer func() {
Expand Down Expand Up @@ -83,7 +83,7 @@ if err != nil {
log.Fatalf("failed to create touch file: %v", err)
}

err = tf.WithLock(ctx, func() error {
err = tf.WithLock(ctx, touchfile.Exclusive, func() error {
fmt.Println("Doing some work while holding the lock")
return nil
})
Expand Down
151 changes: 75 additions & 76 deletions touchfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,48 +20,48 @@
//
// Basic usage:
//
// ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
// defer cancel()
// ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
// defer cancel()
//
// tf, err := touchfile.NewTouchFile("/tmp/mylockfile")
// if err != nil {
// log.Fatalf("failed to create touch file: %v", err)
// }
// tf, err := touchfile.NewTouchFile("/tmp/mylockfile")
// if err != nil {
// log.Fatalf("failed to create touch file: %v", err)
// }
//
// if err := tf.Lock(ctx); err != nil {
// log.Fatalf("failed to acquire lock: %v", err)
// }
// defer func() {
// if err := tf.Unlock(); err != nil {
// log.Printf("failed to release lock: %v", err)
// }
// }()
// if err := tf.Lock(ctx, touchfile.Exclusive); err != nil {
// log.Fatalf("failed to acquire lock: %v", err)
// }
// defer func() {
// if err := tf.Unlock(); err != nil {
// log.Printf("failed to release lock: %v", err)
// }
// }()
//
// Global lock using the program binary:
//
// program, err := os.Executable()
// if err != nil {
// log.Fatalf("failed to determine executable path: %v", err)
// }
// program, err := os.Executable()
// if err != nil {
// log.Fatalf("failed to determine executable path: %v", err)
// }
//
// tf, err := touchfile.NewTouchFile(program)
// ...
// tf, err := touchfile.NewTouchFile(program)
// ...
//
// Using WithLock for a critical section:
//
// tf, err := touchfile.NewTouchFile("/tmp/mylockfile")
// if err != nil {
// log.Fatalf("failed to create touch file: %v", err)
// }
// tf, err := touchfile.NewTouchFile("/tmp/mylockfile")
// if err != nil {
// log.Fatalf("failed to create touch file: %v", err)
// }
//
// err = tf.WithLock(ctx, func() error {
// // Critical section
// fmt.Println("Doing some work while holding the lock")
// return nil
// })
// if err != nil {
// log.Fatalf("operation failed: %v", err)
// }
// err = tf.WithLock(ctx, touchfile.Exclusive, func() error {
// // Critical section
// fmt.Println("Doing some work while holding the lock")
// return nil
// })
// if err != nil {
// log.Fatalf("operation failed: %v", err)
// }
package touchfile

import (
Expand Down Expand Up @@ -100,17 +100,16 @@ const (

const lockRetryInterval = 10 * time.Millisecond


// NewTouchFile creates a TouchFile using flock for advisory locking. If the
// provided path is empty, a temporary file path will be used instead. The path
// is converted to an absolute path.
//
// Example:
//
// tf, err := touchfile.NewTouchFile("/tmp/mylockfile")
// if err != nil {
// log.Fatalf("failed to create touch file: %v", err)
// }
// tf, err := touchfile.NewTouchFile("/tmp/mylockfile")
// if err != nil {
// log.Fatalf("failed to create touch file: %v", err)
// }
func NewTouchFile(path string) (*TouchFile, error) {
absPath, err := createTouchFile(path)
if err != nil {
Expand Down Expand Up @@ -163,22 +162,22 @@ func (tf *TouchFile) Path() string {
//
// Example:
//
// ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
// defer cancel()
// ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
// defer cancel()
//
// tf, err := touchfile.NewTouchFile("/tmp/mylockfile")
// if err != nil {
// log.Fatalf("failed to create touch file: %v", err)
// }
// tf, err := touchfile.NewTouchFile("/tmp/mylockfile")
// if err != nil {
// log.Fatalf("failed to create touch file: %v", err)
// }
//
// if err := tf.Lock(ctx, touchfile.Shared); err != nil {
// log.Fatalf("failed to acquire shared lock: %v", err)
// }
// defer func() {
// if err := tf.Unlock(); err != nil {
// log.Printf("failed to release lock: %v", err)
// }
// }()
// if err := tf.Lock(ctx, touchfile.Shared); err != nil {
// log.Fatalf("failed to acquire shared lock: %v", err)
// }
// defer func() {
// if err := tf.Unlock(); err != nil {
// log.Printf("failed to release lock: %v", err)
// }
// }()
func (tf *TouchFile) Lock(ctx context.Context, lockType LockType) error {
tf.mu.Lock()
defer tf.mu.Unlock()
Expand Down Expand Up @@ -226,21 +225,21 @@ func (tf *TouchFile) lockExclusive(ctx context.Context) (bool, error) {
//
// Example:
//
// tf, err := touchfile.NewTouchFile("/tmp/mylockfile")
// if err != nil {
// log.Fatalf("failed to create touch file: %v", err)
// }
// tf, err := touchfile.NewTouchFile("/tmp/mylockfile")
// if err != nil {
// log.Fatalf("failed to create touch file: %v", err)
// }
//
// ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
// defer cancel()
// ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
// defer cancel()
//
// if err := tf.Lock(ctx); err != nil {
// log.Fatalf("failed to acquire lock: %v", err)
// }
// if err := tf.Lock(ctx, touchfile.Exclusive); err != nil {
// log.Fatalf("failed to acquire lock: %v", err)
// }
//
// if err := tf.Unlock(); err != nil {
// log.Printf("failed to release lock: %v", err)
// }
// if err := tf.Unlock(); err != nil {
// log.Printf("failed to release lock: %v", err)
// }
func (tf *TouchFile) Unlock() error {
tf.mu.Lock()
defer tf.mu.Unlock()
Expand All @@ -258,21 +257,21 @@ func (tf *TouchFile) Unlock() error {
//
// Example:
//
// ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
// defer cancel()
// ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
// defer cancel()
//
// tf, err := touchfile.NewTouchFile("/tmp/mylockfile")
// if err != nil {
// log.Fatalf("failed to create touch file: %v", err)
// }
// tf, err := touchfile.NewTouchFile("/tmp/mylockfile")
// if err != nil {
// log.Fatalf("failed to create touch file: %v", err)
// }
//
// err = tf.WithLock(ctx, func() error {
// fmt.Println("Doing some work while holding the lock")
// return nil
// })
// if err != nil {
// log.Fatalf("operation failed: %v", err)
// }
// err = tf.WithLock(ctx, touchfile.Exclusive, func() error {
// fmt.Println("Doing some work while holding the lock")
// return nil
// })
// if err != nil {
// log.Fatalf("operation failed: %v", err)
// }
func (tf *TouchFile) WithLock(ctx context.Context, lockType LockType, f func() error) error {
if err := tf.Lock(ctx, lockType); err != nil {
return err
Expand Down

0 comments on commit e734664

Please sign in to comment.