-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New Close() method, for use when making many New() filemutexs. #2
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,16 +18,17 @@ const ( | |
// FileMutex is similar to sync.RWMutex, but also synchronizes across processes. | ||
// This implementation is based on flock syscall. | ||
type FileMutex struct { | ||
mu sync.RWMutex | ||
fd int | ||
mu sync.RWMutex | ||
fd int | ||
path string | ||
} | ||
|
||
func New(filename string) (*FileMutex, error) { | ||
fd, err := syscall.Open(filename, syscall.O_CREAT|syscall.O_RDONLY, mkdirPerm) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &FileMutex{fd: fd}, nil | ||
return &FileMutex{fd: fd, path: filename}, nil | ||
} | ||
|
||
func (m *FileMutex) Lock() { | ||
|
@@ -57,3 +58,15 @@ func (m *FileMutex) RUnlock() { | |
} | ||
m.mu.RUnlock() | ||
} | ||
|
||
// Close does an Unlock() combined with closing and unlinking the associated | ||
// lock file. You should create a New() FileMutex for every Lock() attempt if | ||
// using Close(). | ||
func (m *FileMutex) Close() { | ||
if err := syscall.Flock(m.fd, syscall.LOCK_UN); err != nil { | ||
panic(err) | ||
} | ||
syscall.Close(m.fd) | ||
syscall.Unlink(m.path) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm a bit worried about a function called "Close" also deleting the file. If I were using this API, I'd expect Close() to have similar semantics to os.File.Close, which closes the file descriptor but does not actually delete the file. How about having There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think there's no reason why anyone would want the lock file to exist if there will no longer be a lock held on it, so I don't think it makes sense to have 2 methods. So I could either rename Close() to CloseAndRemove(), or come up with some other shorter name that conveys things better. Finish()? This makes it clearer you're not supposed to use it again. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The case I'm thinking of is where there are multiple processes that grab and release locks on the same file (which is one of the use cases this library was designed for) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, the idea is that each process does New() -> Lock() -> Close(), and only one of the processes will hold the lock at any one time. This works (I think, not strictly tested) if Close() unlinks. I don't see the use case of needing the lock file to remain on disk once Close()d. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @sb10 As an API user, I would be very surprised to find that In our use case for CNI plugins, we are opening the lock on a directory containing data that needs to outlive multiple cycles of We would not be able to use this library if |
||
m.mu.Unlock() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hmm what if you call
It seems to me that the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you call Unlock() or Lock() after Close(), you get a panic due to bad file descriptor. The documentation for Close() suggests you don't do that. The alternative of handling the closed state in the other methods doesn't seem worthwhile since you need to know you're using it wrong regardless. If I don't unlock at the end of Close(), however, and another Lock() is attempted, you'd get deadlock, which is much more difficult to debug than getting a panic. Leave as is? Make the documentation more explicit? "Do not call other methods after calling Close()."? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As for preventing Unlock() before Close()... again, I can only think the solution is explicit documentation, and allow the panic to happen. If sync.Mutex Unlock() -> Unlock() panics, so should FileMutex.Unlock() -> Close(); they're the same mistake by the user. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The case I'm thinking of is where I have a FileMutex that I repeatedly Lock and Unlock, and then at the end of all this I want to Close it. This means the mutex would be in the unlocked state when I want to close it, and it would feel weird to be required to lock it before closing it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the user has to be aware of the last time they wish to release the lock. The last time they call Close() instead of Unlock(). I did consider the possibility of Close() not doing what Unlock() does, and so strictly correct usage would be that you'd always have to call Unlock() before Close(), but that wasn't nice from my use case of only ever locking once and then wanting to close in a simple defer. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree with @alexflint that we should just drop the |
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please return err instead of panic (other panics have been removed and method signatures have been updated to return err instead)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍