Lazy initialize UUID for Background context

Fixes #782

Signed-off-by: Darren Shepherd <darren@rancher.com>
This commit is contained in:
Darren Shepherd 2015-07-30 09:47:12 -07:00
parent 9038e48c3b
commit 6086124485

View file

@ -1,10 +1,16 @@
package context package context
import ( import (
"sync"
"github.com/docker/distribution/uuid" "github.com/docker/distribution/uuid"
"golang.org/x/net/context" "golang.org/x/net/context"
) )
var (
once sync.Once
)
// Context is a copy of Context from the golang.org/x/net/context package. // Context is a copy of Context from the golang.org/x/net/context package.
type Context interface { type Context interface {
context.Context context.Context
@ -19,6 +25,13 @@ type instanceContext struct {
func (ic *instanceContext) Value(key interface{}) interface{} { func (ic *instanceContext) Value(key interface{}) interface{} {
if key == "instance.id" { if key == "instance.id" {
once.Do(func() {
// We want to lazy initialize the UUID such that we don't
// call a random generator from the package initialization
// code. For various reasons random could not be available
// https://github.com/docker/distribution/issues/782
ic.id = uuid.Generate().String()
})
return ic.id return ic.id
} }
@ -27,7 +40,6 @@ func (ic *instanceContext) Value(key interface{}) interface{} {
var background = &instanceContext{ var background = &instanceContext{
Context: context.Background(), Context: context.Background(),
id: uuid.Generate().String(),
} }
// Background returns a non-nil, empty Context. The background context // Background returns a non-nil, empty Context. The background context