Merge pull request #792 from stevvooe/uuid-package-improvements

UUID package improvements
This commit is contained in:
Stephen Day 2015-07-30 18:10:25 -07:00
commit ba5ab723ad
4 changed files with 17 additions and 11 deletions

View File

@ -29,6 +29,7 @@ import (
_ "github.com/docker/distribution/registry/storage/driver/middleware/cloudfront" _ "github.com/docker/distribution/registry/storage/driver/middleware/cloudfront"
_ "github.com/docker/distribution/registry/storage/driver/s3" _ "github.com/docker/distribution/registry/storage/driver/s3"
_ "github.com/docker/distribution/registry/storage/driver/swift" _ "github.com/docker/distribution/registry/storage/driver/swift"
"github.com/docker/distribution/uuid"
"github.com/docker/distribution/version" "github.com/docker/distribution/version"
gorhandlers "github.com/gorilla/handlers" gorhandlers "github.com/gorilla/handlers"
"github.com/yvasiyarov/gorelic" "github.com/yvasiyarov/gorelic"
@ -62,6 +63,10 @@ func main() {
fatalf("error configuring logger: %v", err) fatalf("error configuring logger: %v", err)
} }
// inject a logger into the uuid library. warns us if there is a problem
// with uuid generation under low entropy.
uuid.Loggerf = context.GetLogger(ctx).Warnf
app := handlers.NewApp(ctx, *config) app := handlers.NewApp(ctx, *config)
handler := configureReporting(app) handler := configureReporting(app)
handler = panicHandler(handler) handler = panicHandler(handler)

View File

@ -1,6 +1,8 @@
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"
) )
@ -14,11 +16,19 @@ 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" {
ic.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 +37,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

View File

@ -3,8 +3,6 @@ package context
import ( import (
"fmt" "fmt"
"github.com/docker/distribution/uuid"
"github.com/Sirupsen/logrus" "github.com/Sirupsen/logrus"
) )
@ -101,8 +99,3 @@ func getLogrusLogger(ctx Context, keys ...interface{}) *logrus.Entry {
return logger.WithFields(fields) return logger.WithFields(fields)
} }
func init() {
// inject a logger into the uuid library.
uuid.Loggerf = GetLogger(Background()).Warnf
}

View File

@ -8,7 +8,6 @@ import (
"crypto/rand" "crypto/rand"
"fmt" "fmt"
"io" "io"
"log"
"os" "os"
"syscall" "syscall"
"time" "time"
@ -30,7 +29,7 @@ var (
// Loggerf can be used to override the default logging destination. Such // Loggerf can be used to override the default logging destination. Such
// log messages in this library should be logged at warning or higher. // log messages in this library should be logged at warning or higher.
Loggerf = log.Printf Loggerf = func(format string, args ...interface{}) {}
) )
// UUID represents a UUID value. UUIDs can be compared and set to other values // UUID represents a UUID value. UUIDs can be compared and set to other values