-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Lazy initialize UUID for Background context #786
Conversation
Fixes distribution#782 Signed-off-by: Darren Shepherd <darren@rancher.com>
This looks okay to me. I agree with you that it's better not to do UUID generation in |
// code. For various reasons random could not be available | ||
// https://github.com/docker/distribution/issues/782 | ||
ic.id = uuid.Generate().String() | ||
}) |
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.
I think there's a chance to return an empty Id, as Once does not prevent other goroutines to return valur
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.
That's an interesting point, but I think the code is actually correct. once.Do will block in other threads on the slow path mutex. The mutex unlock involves a memory barrier, so when Do returns in the other goroutines, the write to ic.id will be visible on those threads.
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.
Oh, my bad. It prevents. Shame on me :(
@ibuildthecloud This looks fine. Move the |
Removed my approval, I also need this: diff --git a/context/logger.go b/context/logger.go
index b0f0c50..78e4212 100644
--- a/context/logger.go
+++ b/context/logger.go
@@ -3,8 +3,6 @@ package context
import (
"fmt"
- "github.com/docker/distribution/uuid"
-
"github.com/Sirupsen/logrus"
)
@@ -101,8 +99,3 @@ func getLogrusLogger(ctx Context, keys ...interface{}) *logrus.Entry {
return logger.WithFields(fields)
}
-
-func init() {
- // inject a logger into the uuid library.
- uuid.Loggerf = GetLogger(Background()).Warnf
-} |
Ping @stevvooe @ibuildthecloud |
Closing for #792. |
Fixes #782
This will lazy initialize the UUID using
sync/once
. Given that in the happy path of already initialized value,sync/once
does aatomic.LoadUint32()
instead of a full Lock(), I can't imagine this will have any real performance impact.I don't really know under what context this is used so not totally sure if a tests should be added for this or if in general it should be obvious if this code works because of other tests.