2015-04-01 16:46:35 +00:00
|
|
|
package context
|
|
|
|
|
|
|
|
import (
|
2017-08-11 22:31:16 +00:00
|
|
|
"context"
|
2015-07-30 16:47:12 +00:00
|
|
|
"sync"
|
|
|
|
|
2020-08-24 11:18:39 +00:00
|
|
|
"github.com/distribution/distribution/v3/uuid"
|
2015-04-01 16:46:35 +00:00
|
|
|
)
|
|
|
|
|
2015-04-10 01:45:39 +00:00
|
|
|
// instanceContext is a context that provides only an instance id. It is
|
|
|
|
// provided as the main background context.
|
|
|
|
type instanceContext struct {
|
2017-08-11 22:31:16 +00:00
|
|
|
context.Context
|
2015-07-30 20:59:44 +00:00
|
|
|
id string // id of context, logged as "instance.id"
|
|
|
|
once sync.Once // once protect generation of the id
|
2015-04-10 01:45:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ic *instanceContext) Value(key interface{}) interface{} {
|
|
|
|
if key == "instance.id" {
|
2015-07-30 20:59:44 +00:00
|
|
|
ic.once.Do(func() {
|
2015-07-30 16:47:12 +00:00
|
|
|
// 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
|
2020-08-24 11:18:39 +00:00
|
|
|
// https://github.com/distribution/distribution/issues/782
|
2015-07-30 16:47:12 +00:00
|
|
|
ic.id = uuid.Generate().String()
|
|
|
|
})
|
2015-04-10 01:45:39 +00:00
|
|
|
return ic.id
|
|
|
|
}
|
|
|
|
|
|
|
|
return ic.Context.Value(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
var background = &instanceContext{
|
|
|
|
Context: context.Background(),
|
|
|
|
}
|
|
|
|
|
|
|
|
// Background returns a non-nil, empty Context. The background context
|
|
|
|
// provides a single key, "instance.id" that is globally unique to the
|
|
|
|
// process.
|
2017-08-11 22:31:16 +00:00
|
|
|
func Background() context.Context {
|
2015-04-10 01:45:39 +00:00
|
|
|
return background
|
2015-04-01 16:46:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// stringMapContext is a simple context implementation that checks a map for a
|
|
|
|
// key, falling back to a parent if not present.
|
|
|
|
type stringMapContext struct {
|
|
|
|
context.Context
|
|
|
|
m map[string]interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithValues returns a context that proxies lookups through a map. Only
|
|
|
|
// supports string keys.
|
|
|
|
func WithValues(ctx context.Context, m map[string]interface{}) context.Context {
|
|
|
|
mo := make(map[string]interface{}, len(m)) // make our own copy.
|
|
|
|
for k, v := range m {
|
|
|
|
mo[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
return stringMapContext{
|
|
|
|
Context: ctx,
|
|
|
|
m: mo,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (smc stringMapContext) Value(key interface{}) interface{} {
|
|
|
|
if ks, ok := key.(string); ok {
|
|
|
|
if v, ok := smc.m[ks]; ok {
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return smc.Context.Value(key)
|
|
|
|
}
|