Move context once to instanceContext from global scope

Signed-off-by: Stephen J Day <stephen.day@docker.com>
pull/792/head
Stephen J Day 2015-07-30 13:59:44 -07:00
parent 6086124485
commit c737d19235
1 changed files with 3 additions and 6 deletions

View File

@ -7,10 +7,6 @@ import (
"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
@ -20,12 +16,13 @@ type Context interface {
// provided as the main background context. // provided as the main background context.
type instanceContext struct { type instanceContext struct {
Context Context
id string // id of context, logged as "instance.id" id string // id of context, logged as "instance.id"
once sync.Once // once protect generation of the id
} }
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() { ic.once.Do(func() {
// We want to lazy initialize the UUID such that we don't // We want to lazy initialize the UUID such that we don't
// call a random generator from the package initialization // call a random generator from the package initialization
// code. For various reasons random could not be available // code. For various reasons random could not be available