Skip to content

RFC: Use Go 1.23 iterators for locking+traversing stores.#2288

Closed
mtrmac wants to merge 1 commit intocontainers:mainfrom
mtrmac:go1.23
Closed

RFC: Use Go 1.23 iterators for locking+traversing stores.#2288
mtrmac wants to merge 1 commit intocontainers:mainfrom
mtrmac:go1.23

Conversation

@mtrmac
Copy link
Copy Markdown
Collaborator

@mtrmac mtrmac commented Mar 18, 2025

RFC: this is a ~demo, only migrating one function out of several.

This replaces one kind of boilerplate with another, but overall seems a bit less repetitive. The primary benefit is that the code working within the locked stores can return directly, without boilerplate, and there is no need to manually declare a func(…) for the loop body.

The for store, err := range syntax is a bit weird, admittedly, but maybe worth it.

The primary downside is that this really doesn’t work for the exactly-one-closure-call helpers (writeTo…Store):

  • Using for store, err := range writeToSingleStore is very unintuitive, but maybe acceptable with good helper names
  • But Go doesn’t recognize that the loop body executes exactly once, and that if the loop body always returns, there is no need for the code afterwards to return as well. So every caller would need to add an unreachable invented return there… and that seems a bit much for me.

So, we would end up with two rather different helper patterns instead of ~one. I’m really unsure whether it is worth it. It might well make sense to leave the code as is, and to wait another 1/5/10 years for Go to gain type inference and a convenient closure syntax.

This replaces one kind of boilerplate with another,
but overall seems a bit less repetitive.

Signed-off-by: Miloslav Trmač <mitr@redhat.com>
@openshift-ci
Copy link
Copy Markdown
Contributor

openshift-ci bot commented Mar 18, 2025

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: mtrmac

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

Copy link
Copy Markdown
Member

@Luap99 Luap99 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not an active c/storage contributor but I read the code regularly (i.e. when debugging a podman issue) and this seems to me clearly an improvement over the callback. It feels much more natural to read the callers as loops so I like this a lot.

//
// }; done {
// return res, err
// for rlstore, err := range s.readAllLayerStores() {
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note to self: This looks almost too easy, without warning callers about the semantics. Maybe range iterateLockingAllLayerStores ?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

iterateReadLockingAllLayerStores? As usual, I tend towards unwieldy names.

@jankaluza
Copy link
Copy Markdown
Member

Hi, and thank you for your contribution!

We’ve recently migrated this repository into a new monorepo: containers/container-libs along with other repositories

As part of this migration, this repository is no longer accepting new Pull-Requests and therefore this Pull-Request is being closed.

Thank you very much for your contribution. We would appreciate your continued help in migrating this PR to the new container-libs repository. Please let us know if you are facing any issues.

You can read more about the migration and the reasoning behind it in our blog post: Upcoming migration of three containers repositories to monorepo.

Thanks again for your work and for supporting the containers ecosystem!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants