Skip to content
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

Avoid skipping a storage due to identical string representations #5934

Closed

Conversation

ankon
Copy link
Contributor

@ankon ankon commented Nov 9, 2023

fmt.Sprintf("%v", thing) will use a fmt.Stringer implementation in thing, which means that we cannot easily assume that this value is going to be unique here and can be used to deduplicate the storages.

Instead: Use a map[interface{}]struct{} as we anyways have an interface, and interfaces are perfectly fine map keys.

As a side-effect: Implementors of storage can now implement fmt.Stringer safely, and get the expected output here.

fmt.Sprintf("%v", thing) will use a fmt.Stringer implementation in thing, which means
that we cannot easily assume that this value is going to be unique here and can be used
to deduplicate the storages.

Instead: Use a `map[interface{}]struct{}` as we anyways have an interface, and interfaces
are perfectly fine map keys.
@ankon ankon marked this pull request as draft November 9, 2023 19:15
@ankon ankon marked this pull request as ready for review November 9, 2023 19:17
@ankon
Copy link
Contributor Author

ankon commented Nov 9, 2023

Quick playground: https://go.dev/play/p/UgWKHYRn17H

Copy link
Member

@mholt mholt left a comment

Choose a reason for hiding this comment

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

Thanks!

Hmm, yes, this is probably better; although, I wonder if this is too strict of a comparison. It uses the pointer to the value as the key, so if we have 2 identical storage configurations with different pointers (like they are configured in different automation policies) then it won't deduplicate properly.

Maybe that is fine. It's probably better to clean twice than to skip cleaning entirely. (Unless the storage mechanism is expensive, like a cloud DB or something.)

My original hope was that the %v would be able to capture a representation of the storage config in a general way.

Anyway, I think overall, this change is probably good.

If you think the issue I mention above would be a problem in your case, though, let me know and we can figure something out.

modules/caddytls/tls.go Outdated Show resolved Hide resolved
@ankon
Copy link
Contributor Author

ankon commented Nov 9, 2023

Unless the storage mechanism is expensive, like a cloud DB or something.

Yeah. Well. :)

@ankon
Copy link
Contributor Author

ankon commented Nov 9, 2023

My original hope was that the %v would be able to capture a representation of the storage config in a general way.

I think that's a good goal actually, but maybe that should then be a (optional) requirement on the storage implementation: If you implement interface StorageId, we promise to use that to track storage identity, and you promise to give a sane value.

I'm wondering whether it could be done backwards-compatible, but I really think the foot-shooting potential of falling back to fmt.Stringer is a bit too high.

@mholt
Copy link
Member

mholt commented Nov 14, 2023

As mentioned in Slack, I have a proper fix inbound, I think. Stay tuned in the next day or so for that!

@mholt
Copy link
Member

mholt commented Nov 28, 2023

Gently closing this in favor of #5940 -- we can reopen this if needed, of course. Thanks for helping! 😀

@mholt mholt closed this Nov 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants