Disassociate instance id from application

This moves the instance id out of the app so that it is associated with an
instantiation of the runtime. The instance id is stored on the background
context. This allows allow contexts using the main background context to
include an instance id for log messages. It also simplifies the application
slightly.

Signed-off-by: Stephen J Day <stephen.day@docker.com>
This commit is contained in:
Stephen J Day 2015-04-09 18:45:39 -07:00
parent 44fa39e4ad
commit 36a076995b

View file

@ -8,7 +8,6 @@ import (
"os" "os"
"time" "time"
"code.google.com/p/go-uuid/uuid"
"github.com/docker/distribution" "github.com/docker/distribution"
"github.com/docker/distribution/configuration" "github.com/docker/distribution/configuration"
ctxu "github.com/docker/distribution/context" ctxu "github.com/docker/distribution/context"
@ -32,11 +31,8 @@ import (
// fields should be protected. // fields should be protected.
type App struct { type App struct {
context.Context context.Context
Config configuration.Configuration
// InstanceID is a unique id assigned to the application on each creation. Config configuration.Configuration
// Provides information in the logs and context to identify restarts.
InstanceID string
router *mux.Router // main application router, configured with dispatchers router *mux.Router // main application router, configured with dispatchers
driver storagedriver.StorageDriver // driver maintains the app global storage driver instance. driver storagedriver.StorageDriver // driver maintains the app global storage driver instance.
@ -52,29 +48,17 @@ type App struct {
redis *redis.Pool redis *redis.Pool
} }
// Value intercepts calls context.Context.Value, returning the current app id,
// if requested.
func (app *App) Value(key interface{}) interface{} {
switch key {
case "app.id":
return app.InstanceID
}
return app.Context.Value(key)
}
// NewApp takes a configuration and returns a configured app, ready to serve // NewApp takes a configuration and returns a configured app, ready to serve
// requests. The app only implements ServeHTTP and can be wrapped in other // requests. The app only implements ServeHTTP and can be wrapped in other
// handlers accordingly. // handlers accordingly.
func NewApp(ctx context.Context, configuration configuration.Configuration) *App { func NewApp(ctx context.Context, configuration configuration.Configuration) *App {
app := &App{ app := &App{
Config: configuration, Config: configuration,
Context: ctx, Context: ctx,
InstanceID: uuid.New(), router: v2.RouterWithPrefix(configuration.HTTP.Prefix),
router: v2.RouterWithPrefix(configuration.HTTP.Prefix),
} }
app.Context = ctxu.WithLogger(app.Context, ctxu.GetLogger(app, "app.id")) app.Context = ctxu.WithLogger(app.Context, ctxu.GetLogger(app, "instance.id"))
// Register the handler dispatchers. // Register the handler dispatchers.
app.register(v2.RouteNameBase, func(ctx *Context, r *http.Request) http.Handler { app.register(v2.RouteNameBase, func(ctx *Context, r *http.Request) http.Handler {
@ -200,7 +184,7 @@ func (app *App) configureEvents(configuration *configuration.Configuration) {
app.events.source = notifications.SourceRecord{ app.events.source = notifications.SourceRecord{
Addr: hostname, Addr: hostname,
InstanceID: app.InstanceID, InstanceID: ctxu.GetStringValue(app, "instance.id"),
} }
} }