|
| 1 | +// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 2 | +// or more contributor license agreements. Licensed under the Elastic License 2.0; |
| 3 | +// you may not use this file except in compliance with the Elastic License 2.0. |
| 4 | + |
| 5 | +package filelock |
| 6 | + |
| 7 | +import ( |
| 8 | + "context" |
| 9 | + "errors" |
| 10 | + "fmt" |
| 11 | + "time" |
| 12 | + |
| 13 | + "github.com/gofrs/flock" |
| 14 | +) |
| 15 | + |
| 16 | +var ( |
| 17 | + ErrZeroTimeout = errors.New("must specify a non-zero timeout for a blocking file locker") |
| 18 | + ErrNotLocked = errors.New("file not locked") |
| 19 | +) |
| 20 | + |
| 21 | +// FileLocker is a thin wrapper around "github.com/gofrs/flock" Flock providing both blocking and non-blocking file locking. |
| 22 | +// It exposes a simplified Lock*/Unlock interface and by default is non-blocking. |
| 23 | +// If it's not possible to acquire a lock on the specified file ErrNotLocked (directly or wrapped in another error) is returned by default. |
| 24 | +// It's possible to customize FileLocker behavior specifying one or more FileLockerOption at creation time. |
| 25 | +type FileLocker struct { |
| 26 | + fileLock *flock.Flock |
| 27 | + blocking bool |
| 28 | + timeout time.Duration |
| 29 | + customNotLockedError error |
| 30 | +} |
| 31 | + |
| 32 | +func NewFileLocker(lockFilePath string, opts ...FileLockerOption) (*FileLocker, error) { |
| 33 | + flocker := &FileLocker{fileLock: flock.New(lockFilePath)} |
| 34 | + for _, opt := range opts { |
| 35 | + if err := opt(flocker); err != nil { |
| 36 | + return nil, fmt.Errorf("applying options to new file locker: %w", err) |
| 37 | + } |
| 38 | + } |
| 39 | + return flocker, nil |
| 40 | +} |
| 41 | + |
| 42 | +// Lock() will attempt to lock the configured lockfile. Depending on the options specified at FileLocker creation this |
| 43 | +// call can be blocking or non-blocking. In order to use a blocking FileLocker a timeout must be specified at creation |
| 44 | +// specifying WithTimeout() option. |
| 45 | +// Even in case of a blocking FileLocker the maximum duration of the locking attempt will be the timeout specified at creation. |
| 46 | +// If no lock can be acquired ErrNotLocked error will be returned by default, unless a custom "not locked" error has been |
| 47 | +// specified with WithCustomNotLockedError at creation. |
| 48 | +func (fl *FileLocker) Lock() error { |
| 49 | + return fl.LockContext(context.Background()) |
| 50 | +} |
| 51 | + |
| 52 | +// LockWithContext() will attempt to lock the configured lockfile. It has the same semantics as Lock(), additionally it |
| 53 | +// allows passing a context as an argument to back out of locking attempts when context expires (useful in case of a |
| 54 | +// blocking FileLocker) |
| 55 | +func (fl *FileLocker) LockContext(ctx context.Context) error { |
| 56 | + var locked bool |
| 57 | + var err error |
| 58 | + |
| 59 | + if fl.blocking { |
| 60 | + timeoutCtx, cancel := context.WithTimeout(ctx, fl.timeout) |
| 61 | + defer cancel() |
| 62 | + locked, err = fl.fileLock.TryLockContext(timeoutCtx, time.Second) |
| 63 | + } else { |
| 64 | + locked, err = fl.fileLock.TryLock() |
| 65 | + } |
| 66 | + |
| 67 | + if err != nil { |
| 68 | + return fmt.Errorf("locking %s: %w", fl.fileLock.Path(), err) |
| 69 | + } |
| 70 | + if !locked { |
| 71 | + if fl.customNotLockedError != nil { |
| 72 | + return fmt.Errorf("failed locking %s: %w", fl.fileLock.Path(), fl.customNotLockedError) |
| 73 | + } |
| 74 | + return fmt.Errorf("failed locking %s: %w", fl.fileLock.Path(), ErrNotLocked) |
| 75 | + } |
| 76 | + return nil |
| 77 | +} |
| 78 | + |
| 79 | +func (fl *FileLocker) Unlock() error { |
| 80 | + return fl.fileLock.Unlock() |
| 81 | +} |
| 82 | + |
| 83 | +func (fl *FileLocker) Locked() bool { |
| 84 | + return fl.fileLock.Locked() |
| 85 | +} |
| 86 | + |
| 87 | +type FileLockerOption func(locker *FileLocker) error |
| 88 | + |
| 89 | +// WithCustomNotLockedError will set a custom error to be returned when it's not possible to acquire a lock |
| 90 | +func WithCustomNotLockedError(customError error) FileLockerOption { |
| 91 | + return func(locker *FileLocker) error { |
| 92 | + locker.customNotLockedError = customError |
| 93 | + return nil |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +// WithTimeout will set the FileLocker to be blocking and will enforce a non-zero timeout. |
| 98 | +// If a zero timeout is passed this option will error out, failing the FileLocker creation. |
| 99 | +func WithTimeout(timeout time.Duration) FileLockerOption { |
| 100 | + return func(locker *FileLocker) error { |
| 101 | + |
| 102 | + if timeout == 0 { |
| 103 | + return ErrZeroTimeout |
| 104 | + } |
| 105 | + |
| 106 | + locker.blocking = true |
| 107 | + locker.timeout = timeout |
| 108 | + |
| 109 | + return nil |
| 110 | + } |
| 111 | +} |
0 commit comments