Skip to content

Commit 9c88801

Browse files
committed
context: remove definition of Context
Back in the before time, the best practices surrounding usage of Context weren't quite worked out. We defined our own type to make usage easier. As this packaged was used elsewhere, it make it more and more challenging to integrate with the forked `Context` type. Now that it is available in the standard library, we can just use that one directly. To make usage more consistent, we now use `dcontext` when referring to the distribution context package. Signed-off-by: Stephen J Day <stephen.day@docker.com>
1 parent 7a8efe7 commit 9c88801

File tree

90 files changed

+396
-373
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

90 files changed

+396
-373
lines changed

blobs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package distribution
22

33
import (
4+
"context"
45
"errors"
56
"fmt"
67
"io"
78
"net/http"
89
"time"
910

10-
"github.com/docker/distribution/context"
1111
"github.com/docker/distribution/reference"
1212
"github.com/opencontainers/go-digest"
1313
)

context/context.go

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,16 @@
11
package context
22

33
import (
4+
"context"
45
"sync"
56

67
"github.com/docker/distribution/uuid"
7-
"golang.org/x/net/context"
88
)
99

10-
// Context is a copy of Context from the golang.org/x/net/context package.
11-
type Context interface {
12-
context.Context
13-
}
14-
1510
// instanceContext is a context that provides only an instance id. It is
1611
// provided as the main background context.
1712
type instanceContext struct {
18-
Context
13+
context.Context
1914
id string // id of context, logged as "instance.id"
2015
once sync.Once // once protect generation of the id
2116
}
@@ -42,17 +37,10 @@ var background = &instanceContext{
4237
// Background returns a non-nil, empty Context. The background context
4338
// provides a single key, "instance.id" that is globally unique to the
4439
// process.
45-
func Background() Context {
40+
func Background() context.Context {
4641
return background
4742
}
4843

49-
// WithValue returns a copy of parent in which the value associated with key is
50-
// val. Use context Values only for request-scoped data that transits processes
51-
// and APIs, not for passing optional parameters to functions.
52-
func WithValue(parent Context, key, val interface{}) Context {
53-
return context.WithValue(parent, key, val)
54-
}
55-
5644
// stringMapContext is a simple context implementation that checks a map for a
5745
// key, falling back to a parent if not present.
5846
type stringMapContext struct {

context/http.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package context
22

33
import (
4+
"context"
45
"errors"
56
"net"
67
"net/http"
@@ -68,7 +69,7 @@ func RemoteIP(r *http.Request) string {
6869
// is available at "http.request". Other common attributes are available under
6970
// the prefix "http.request.". If a request is already present on the context,
7071
// this method will panic.
71-
func WithRequest(ctx Context, r *http.Request) Context {
72+
func WithRequest(ctx context.Context, r *http.Request) context.Context {
7273
if ctx.Value("http.request") != nil {
7374
// NOTE(stevvooe): This needs to be considered a programming error. It
7475
// is unlikely that we'd want to have more than one request in
@@ -87,7 +88,7 @@ func WithRequest(ctx Context, r *http.Request) Context {
8788
// GetRequest returns the http request in the given context. Returns
8889
// ErrNoRequestContext if the context does not have an http request associated
8990
// with it.
90-
func GetRequest(ctx Context) (*http.Request, error) {
91+
func GetRequest(ctx context.Context) (*http.Request, error) {
9192
if r, ok := ctx.Value("http.request").(*http.Request); r != nil && ok {
9293
return r, nil
9394
}
@@ -96,13 +97,13 @@ func GetRequest(ctx Context) (*http.Request, error) {
9697

9798
// GetRequestID attempts to resolve the current request id, if possible. An
9899
// error is return if it is not available on the context.
99-
func GetRequestID(ctx Context) string {
100+
func GetRequestID(ctx context.Context) string {
100101
return GetStringValue(ctx, "http.request.id")
101102
}
102103

103104
// WithResponseWriter returns a new context and response writer that makes
104105
// interesting response statistics available within the context.
105-
func WithResponseWriter(ctx Context, w http.ResponseWriter) (Context, http.ResponseWriter) {
106+
func WithResponseWriter(ctx context.Context, w http.ResponseWriter) (context.Context, http.ResponseWriter) {
106107
if closeNotifier, ok := w.(http.CloseNotifier); ok {
107108
irwCN := &instrumentedResponseWriterCN{
108109
instrumentedResponseWriter: instrumentedResponseWriter{
@@ -125,7 +126,7 @@ func WithResponseWriter(ctx Context, w http.ResponseWriter) (Context, http.Respo
125126
// GetResponseWriter returns the http.ResponseWriter from the provided
126127
// context. If not present, ErrNoResponseWriterContext is returned. The
127128
// returned instance provides instrumentation in the context.
128-
func GetResponseWriter(ctx Context) (http.ResponseWriter, error) {
129+
func GetResponseWriter(ctx context.Context) (http.ResponseWriter, error) {
129130
v := ctx.Value("http.response")
130131

131132
rw, ok := v.(http.ResponseWriter)
@@ -145,7 +146,7 @@ var getVarsFromRequest = mux.Vars
145146
// example, if looking for the variable "name", it can be accessed as
146147
// "vars.name". Implementations that are accessing values need not know that
147148
// the underlying context is implemented with gorilla/mux vars.
148-
func WithVars(ctx Context, r *http.Request) Context {
149+
func WithVars(ctx context.Context, r *http.Request) context.Context {
149150
return &muxVarsContext{
150151
Context: ctx,
151152
vars: getVarsFromRequest(r),
@@ -155,7 +156,7 @@ func WithVars(ctx Context, r *http.Request) Context {
155156
// GetRequestLogger returns a logger that contains fields from the request in
156157
// the current context. If the request is not available in the context, no
157158
// fields will display. Request loggers can safely be pushed onto the context.
158-
func GetRequestLogger(ctx Context) Logger {
159+
func GetRequestLogger(ctx context.Context) Logger {
159160
return GetLogger(ctx,
160161
"http.request.id",
161162
"http.request.method",
@@ -171,7 +172,7 @@ func GetRequestLogger(ctx Context) Logger {
171172
// Because the values are read at call time, pushing a logger returned from
172173
// this function on the context will lead to missing or invalid data. Only
173174
// call this at the end of a request, after the response has been written.
174-
func GetResponseLogger(ctx Context) Logger {
175+
func GetResponseLogger(ctx context.Context) Logger {
175176
l := getLogrusLogger(ctx,
176177
"http.response.written",
177178
"http.response.status",
@@ -188,7 +189,7 @@ func GetResponseLogger(ctx Context) Logger {
188189

189190
// httpRequestContext makes information about a request available to context.
190191
type httpRequestContext struct {
191-
Context
192+
context.Context
192193

193194
startedAt time.Time
194195
id string
@@ -247,7 +248,7 @@ fallback:
247248
}
248249

249250
type muxVarsContext struct {
250-
Context
251+
context.Context
251252
vars map[string]string
252253
}
253254

@@ -282,7 +283,7 @@ type instrumentedResponseWriterCN struct {
282283
// implemented by the parent ResponseWriter.
283284
type instrumentedResponseWriter struct {
284285
http.ResponseWriter
285-
Context
286+
context.Context
286287

287288
mu sync.Mutex
288289
status int

context/logger.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package context
22

33
import (
4+
"context"
45
"fmt"
6+
"runtime"
57

68
"github.com/sirupsen/logrus"
7-
"runtime"
89
)
910

1011
// Logger provides a leveled-logging interface.
@@ -40,22 +41,24 @@ type Logger interface {
4041
Warnln(args ...interface{})
4142
}
4243

44+
type loggerKey struct{}
45+
4346
// WithLogger creates a new context with provided logger.
44-
func WithLogger(ctx Context, logger Logger) Context {
45-
return WithValue(ctx, "logger", logger)
47+
func WithLogger(ctx context.Context, logger Logger) context.Context {
48+
return context.WithValue(ctx, loggerKey{}, logger)
4649
}
4750

4851
// GetLoggerWithField returns a logger instance with the specified field key
4952
// and value without affecting the context. Extra specified keys will be
5053
// resolved from the context.
51-
func GetLoggerWithField(ctx Context, key, value interface{}, keys ...interface{}) Logger {
54+
func GetLoggerWithField(ctx context.Context, key, value interface{}, keys ...interface{}) Logger {
5255
return getLogrusLogger(ctx, keys...).WithField(fmt.Sprint(key), value)
5356
}
5457

5558
// GetLoggerWithFields returns a logger instance with the specified fields
5659
// without affecting the context. Extra specified keys will be resolved from
5760
// the context.
58-
func GetLoggerWithFields(ctx Context, fields map[interface{}]interface{}, keys ...interface{}) Logger {
61+
func GetLoggerWithFields(ctx context.Context, fields map[interface{}]interface{}, keys ...interface{}) Logger {
5962
// must convert from interface{} -> interface{} to string -> interface{} for logrus.
6063
lfields := make(logrus.Fields, len(fields))
6164
for key, value := range fields {
@@ -71,19 +74,19 @@ func GetLoggerWithFields(ctx Context, fields map[interface{}]interface{}, keys .
7174
// argument passed to GetLogger will be passed to fmt.Sprint when expanded as
7275
// a logging key field. If context keys are integer constants, for example,
7376
// its recommended that a String method is implemented.
74-
func GetLogger(ctx Context, keys ...interface{}) Logger {
77+
func GetLogger(ctx context.Context, keys ...interface{}) Logger {
7578
return getLogrusLogger(ctx, keys...)
7679
}
7780

7881
// GetLogrusLogger returns the logrus logger for the context. If one more keys
7982
// are provided, they will be resolved on the context and included in the
8083
// logger. Only use this function if specific logrus functionality is
8184
// required.
82-
func getLogrusLogger(ctx Context, keys ...interface{}) *logrus.Entry {
85+
func getLogrusLogger(ctx context.Context, keys ...interface{}) *logrus.Entry {
8386
var logger *logrus.Entry
8487

8588
// Get a logger, if it is present.
86-
loggerInterface := ctx.Value("logger")
89+
loggerInterface := ctx.Value(loggerKey{})
8790
if loggerInterface != nil {
8891
if lgr, ok := loggerInterface.(*logrus.Entry); ok {
8992
logger = lgr

context/trace.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package context
22

33
import (
4+
"context"
45
"runtime"
56
"time"
67

@@ -36,7 +37,7 @@ import (
3637
//
3738
// Notice that the function name is automatically resolved, along with the
3839
// package and a trace id is emitted that can be linked with parent ids.
39-
func WithTrace(ctx Context) (Context, func(format string, a ...interface{})) {
40+
func WithTrace(ctx context.Context) (context.Context, func(format string, a ...interface{})) {
4041
if ctx == nil {
4142
ctx = Background()
4243
}
@@ -69,7 +70,7 @@ func WithTrace(ctx Context) (Context, func(format string, a ...interface{})) {
6970
// also provides fast lookup for the various attributes that are available on
7071
// the trace.
7172
type traced struct {
72-
Context
73+
context.Context
7374
id string
7475
parent string
7576
start time.Time

context/trace_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package context
22

33
import (
4+
"context"
45
"runtime"
56
"testing"
67
"time"
@@ -35,7 +36,7 @@ func TestWithTrace(t *testing.T) {
3536
ctx, done := WithTrace(Background())
3637
defer done("this will be emitted at end of test")
3738

38-
checkContextForValues(t, ctx, append(base, valueTestCase{
39+
checkContextForValues(ctx, t, append(base, valueTestCase{
3940
key: "trace.func",
4041
expected: f.Name(),
4142
}))
@@ -48,7 +49,7 @@ func TestWithTrace(t *testing.T) {
4849
ctx, done := WithTrace(ctx)
4950
defer done("this should be subordinate to the other trace")
5051
time.Sleep(time.Second)
51-
checkContextForValues(t, ctx, append(base, valueTestCase{
52+
checkContextForValues(ctx, t, append(base, valueTestCase{
5253
key: "trace.func",
5354
expected: f.Name(),
5455
}, valueTestCase{
@@ -67,8 +68,7 @@ type valueTestCase struct {
6768
notnilorempty bool // just check not empty/not nil
6869
}
6970

70-
func checkContextForValues(t *testing.T, ctx Context, values []valueTestCase) {
71-
71+
func checkContextForValues(ctx context.Context, t *testing.T, values []valueTestCase) {
7272
for _, testcase := range values {
7373
v := ctx.Value(testcase.key)
7474
if testcase.notnilorempty {

context/util.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
package context
22

33
import (
4+
"context"
45
"time"
56
)
67

78
// Since looks up key, which should be a time.Time, and returns the duration
89
// since that time. If the key is not found, the value returned will be zero.
910
// This is helpful when inferring metrics related to context execution times.
10-
func Since(ctx Context, key interface{}) time.Duration {
11+
func Since(ctx context.Context, key interface{}) time.Duration {
1112
if startedAt, ok := ctx.Value(key).(time.Time); ok {
1213
return time.Since(startedAt)
1314
}
@@ -16,7 +17,7 @@ func Since(ctx Context, key interface{}) time.Duration {
1617

1718
// GetStringValue returns a string value from the context. The empty string
1819
// will be returned if not found.
19-
func GetStringValue(ctx Context, key interface{}) (value string) {
20+
func GetStringValue(ctx context.Context, key interface{}) (value string) {
2021
if valuev, ok := ctx.Value(key).(string); ok {
2122
value = valuev
2223
}

context/version.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
11
package context
22

3+
import "context"
4+
5+
type versionKey struct{}
6+
7+
func (versionKey) String() string { return "version" }
8+
39
// WithVersion stores the application version in the context. The new context
410
// gets a logger to ensure log messages are marked with the application
511
// version.
6-
func WithVersion(ctx Context, version string) Context {
7-
ctx = WithValue(ctx, "version", version)
12+
func WithVersion(ctx context.Context, version string) context.Context {
13+
ctx = context.WithValue(ctx, versionKey{}, version)
814
// push a new logger onto the stack
9-
return WithLogger(ctx, GetLogger(ctx, "version"))
15+
return WithLogger(ctx, GetLogger(ctx, versionKey{}))
1016
}
1117

1218
// GetVersion returns the application version from the context. An empty
1319
// string may returned if the version was not set on the context.
14-
func GetVersion(ctx Context) string {
15-
return GetStringValue(ctx, "version")
20+
func GetVersion(ctx context.Context) string {
21+
return GetStringValue(ctx, versionKey{})
1622
}

0 commit comments

Comments
 (0)