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
1 changed files with 13 additions and 1 deletions

View File

@ -1,10 +1,16 @@
package context
import (
"sync"
"github.com/docker/distribution/uuid"
"golang.org/x/net/context"
)
var (
once sync.Once
)
// Context is a copy of Context from the golang.org/x/net/context package.
type Context interface {
context.Context
@ -19,6 +25,13 @@ type instanceContext struct {
func (ic *instanceContext) Value(key interface{}) interface{} {
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
}
@ -27,7 +40,6 @@ func (ic *instanceContext) Value(key interface{}) interface{} {
var background = &instanceContext{
Context: context.Background(),
id: uuid.Generate().String(),
}
// Background returns a non-nil, empty Context. The background context