From 0a72cb3ef4de9c73b9b835d7b418c5d359f42ab8 Mon Sep 17 00:00:00 2001 From: Dario Domizioli Date: Fri, 20 Apr 2018 17:29:27 +0100 Subject: [PATCH] Fix missing values in context created with common.BackgroundContext (#950) * Fix missing values in context when created through common.BackgroundContext * pin to mysql 5.7.22 --- api/common/ctx.go | 48 +++++++++++++++++++++++++++-------------------- api_test.sh | 2 +- system_test.sh | 2 +- test.sh | 2 +- 4 files changed, 31 insertions(+), 23 deletions(-) diff --git a/api/common/ctx.go b/api/common/ctx.go index 953683604..c2dccd57b 100644 --- a/api/common/ctx.go +++ b/api/common/ctx.go @@ -2,10 +2,9 @@ package common import ( "context" + "time" "github.com/sirupsen/logrus" - "go.opencensus.io/tag" - "go.opencensus.io/trace" ) type contextKey string @@ -34,25 +33,34 @@ func LoggerWithFields(ctx context.Context, fields logrus.Fields) (context.Contex return ctx, l } +// contextWithNoDeadline is an implementation of context.Context which delegates +// Value() to its parent, but it has no deadline and it is never cancelled, just +// like a context.Background(). +type contextWithNoDeadline struct { + original context.Context +} + +func (ctx *contextWithNoDeadline) Deadline() (deadline time.Time, ok bool) { + return context.Background().Deadline() +} + +func (ctx *contextWithNoDeadline) Done() <-chan struct{} { + return context.Background().Done() +} + +func (ctx *contextWithNoDeadline) Err() error { + return context.Background().Err() +} + +func (ctx *contextWithNoDeadline) Value(key interface{}) interface{} { + return ctx.original.Value(key) +} + // BackgroundContext returns a context that is specifically not a child of the // provided parent context wrt any cancellation or deadline of the parent, -// returning a context that contains all values only. At present, this is a -// best effort as there is not a great way to extract all values, known values: -// * logger -// * span -// * tags -// (TODO(reed): we could have our own context.Context implementer that stores -// all values from WithValue in a bucket we could extract more easily?) +// so that it contains all values only. func BackgroundContext(ctx context.Context) context.Context { - logger := Logger(ctx) - span := trace.FromContext(ctx) - tagMap := tag.FromContext(ctx) - - // fresh context - ctx = context.Background() - - ctx = tag.NewContext(ctx, tagMap) - ctx = trace.WithSpan(ctx, span) - ctx = WithLogger(ctx, logger) - return ctx + return &contextWithNoDeadline{ + original: ctx, + } } diff --git a/api_test.sh b/api_test.sh index cdf0faae5..1f5a7f679 100755 --- a/api_test.sh +++ b/api_test.sh @@ -15,7 +15,7 @@ case "$1" in "mysql" ) DB_CONTAINER="func-mysql-test" docker rm -fv ${DB_CONTAINER} || echo No prev mysql test db container - docker run --name ${DB_CONTAINER} -p 3306:3306 -e MYSQL_DATABASE=funcs -e MYSQL_ROOT_PASSWORD=root -d mysql + docker run --name ${DB_CONTAINER} -p 3306:3306 -e MYSQL_DATABASE=funcs -e MYSQL_ROOT_PASSWORD=root -d mysql:5.7.22 MYSQL_HOST=`host ${DB_CONTAINER}` MYSQL_PORT=3306 export FN_DB_URL="mysql://root:root@tcp(${MYSQL_HOST}:${MYSQL_PORT})/funcs" diff --git a/system_test.sh b/system_test.sh index 1c5a4e34b..305ea78f1 100755 --- a/system_test.sh +++ b/system_test.sh @@ -20,7 +20,7 @@ case "$1" in "mysql" ) DB_CONTAINER="func-mysql-system-test" docker rm -fv ${DB_CONTAINER} || echo No prev mysql test db container - docker run --name ${DB_CONTAINER} -p 3307:3306 -e MYSQL_DATABASE=funcs -e MYSQL_ROOT_PASSWORD=root -d mysql + docker run --name ${DB_CONTAINER} -p 3307:3306 -e MYSQL_DATABASE=funcs -e MYSQL_ROOT_PASSWORD=root -d mysql:5.7.22 MYSQL_HOST=`host ${DB_CONTAINER}` MYSQL_PORT=3307 export FN_DB_URL="mysql://root:root@tcp(${MYSQL_HOST}:${MYSQL_PORT})/funcs" diff --git a/test.sh b/test.sh index 2dd4d79e6..d5dde653c 100755 --- a/test.sh +++ b/test.sh @@ -8,7 +8,7 @@ source ./helpers.sh remove_containers docker run --name func-postgres-test -e "POSTGRES_DB=funcs" -e "POSTGRES_PASSWORD=root" -p 5432:5432 -d postgres:9.3-alpine -docker run --name func-mysql-test -p 3306:3306 -e MYSQL_DATABASE=funcs -e MYSQL_ROOT_PASSWORD=root -d mysql +docker run --name func-mysql-test -p 3306:3306 -e MYSQL_DATABASE=funcs -e MYSQL_ROOT_PASSWORD=root -d mysql:5.7.22 docker run -d -p 9000:9000 --name func-minio-test -e "MINIO_ACCESS_KEY=admin" -e "MINIO_SECRET_KEY=password" minio/minio server /data MYSQL_HOST=`host func-mysql-test`