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:
parent
44fa39e4ad
commit
36a076995b
1 changed files with 6 additions and 22 deletions
|
@ -8,7 +8,6 @@ import (
|
|||
"os"
|
||||
"time"
|
||||
|
||||
"code.google.com/p/go-uuid/uuid"
|
||||
"github.com/docker/distribution"
|
||||
"github.com/docker/distribution/configuration"
|
||||
ctxu "github.com/docker/distribution/context"
|
||||
|
@ -32,11 +31,8 @@ import (
|
|||
// fields should be protected.
|
||||
type App struct {
|
||||
context.Context
|
||||
Config configuration.Configuration
|
||||
|
||||
// InstanceID is a unique id assigned to the application on each creation.
|
||||
// Provides information in the logs and context to identify restarts.
|
||||
InstanceID string
|
||||
Config configuration.Configuration
|
||||
|
||||
router *mux.Router // main application router, configured with dispatchers
|
||||
driver storagedriver.StorageDriver // driver maintains the app global storage driver instance.
|
||||
|
@ -52,17 +48,6 @@ type App struct {
|
|||
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
|
||||
// requests. The app only implements ServeHTTP and can be wrapped in other
|
||||
// handlers accordingly.
|
||||
|
@ -70,11 +55,10 @@ func NewApp(ctx context.Context, configuration configuration.Configuration) *App
|
|||
app := &App{
|
||||
Config: configuration,
|
||||
Context: ctx,
|
||||
InstanceID: uuid.New(),
|
||||
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.
|
||||
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{
|
||||
Addr: hostname,
|
||||
InstanceID: app.InstanceID,
|
||||
InstanceID: ctxu.GetStringValue(app, "instance.id"),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue