-
Notifications
You must be signed in to change notification settings - Fork 246
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
Use Go 1.18 generics in the store’s locking helpers #1577
Conversation
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.
LGTM
store.go
Outdated
@@ -2088,12 +2076,11 @@ func (s *store) ContainerSize(id string) (int64, error) { | |||
return -1, err | |||
} | |||
|
|||
var res int64 = -1 | |||
err = s.writeToContainerStore(func() error { // Yes, s.containerStore.BigDataSize requires a write lock. | |||
return writeToContainerStore(s, func() (int64, error) { // Yes, rcstore.BigDataSize requires a write lock. |
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.
Where did rcstore
come from?
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.
Thanks, fixed. (It’s a mistake in rebasing, rcstore
used to exist back when this branch was originally prepared.)
store.go
Outdated
}); done { | ||
return res | ||
return res // false if err != nil |
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.
Which err
is this referring to, here and in various similar comments where the closure is calling Exists()
?
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.
The one that was assigned to _
when calling the locking wrapper…
It would be possible to explain that that in those comments, but at that point it seems simpler to just make the code explicit if err { return false }; if found { return true }
now. So updated that way.
It is, thanks. Generally looks good, though I'm mildly surprised that it doesn't make the logic that much shorter. |
I’m afraid that’s just Go. Native error handling and more type inference would be necessary to significantly shorten the boilerplate. Another modern language: class Store {
var containerStore: ContainerStore = ContainerStore()
private func readContainerStore<T>(_ fn: () throws -> T) throws -> T {
try containerStore.startReading()
defer {
containerStore.stopReading()
}
return try fn()
}
func containerBigData(id: String, key: String) throws -> [UInt8] {
return try readContainerStore {
return try containerStore.bigData(id: id, key: key)
}
}
} (And actually, with that kind of a language with a working The grass and the other side…)
I chose this because I thought the slight readability benefit, making it very clear that there is nothing there, is worth it — and because I didn’t want to keep typing that I fully realize that this is a fairly goofy approach, and I’m perfectly willing to drop that part. |
(Another possible alternative would be to define another set of generic wrappers which don’t simply don’t have that return value, e.g. func writeToLayerStoreVoid(s *store, fn func(store rwLayerStore) (error)) (error) {
return writeToLayerStore(s, func(store rwLayerStore) (struct{}, error) {
return struct{}{}, fn(store)
})
} but that feels tedious to me.) |
Updated: I have changed my mind on |
Signed-off-by: Miloslav Trmač <[email protected]>
Yes, please go ahead and drop that part. I don't have an issue with returning an unused variable from a generic convenience function. |
Signed-off-by: Miloslav Trmač <[email protected]>
Signed-off-by: Miloslav Trmač <[email protected]>
Signed-off-by: Miloslav Trmač <[email protected]>
Signed-off-by: Miloslav Trmač <[email protected]>
Signed-off-by: Miloslav Trmač <[email protected]>
... and use it in many more places. Signed-off-by: Miloslav Trmač <[email protected]>
|
return res, err | ||
if err != nil { | ||
return -1, err | ||
} |
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.
If I'm reading it right, err
is always nil
here.
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.
err
can be set if readAllImageStores
fails in imageStore.startReading
, e.g. on an I/O error.
One nit in |
LGTM |
… avoiding quite a bit of the previously-required boilerplate.
This is probably easier to review as individual commits, and maybe with whitespace changes ignored.
@nalind PTAL