1 // Copyright (C) MongoDB, Inc. 2017-present. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); you may 4 // not use this file except in compliance with the License. You may obtain 5 // a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 6 7 package mongo 8 9 import "context" 10 11 // backgroundContext is an implementation of the context.Context interface that wraps a child Context. Value requests 12 // are forwarded to the child Context but the Done and Err functions are overridden to ensure the new context does not 13 // time out or get cancelled. 14 type backgroundContext struct { 15 context.Context 16 childValuesCtx context.Context 17 } 18 19 // newBackgroundContext creates a new Context whose behavior matches that of context.Background(), but Value calls are 20 // forwarded to the provided ctx parameter. If ctx is nil, context.Background() is returned. 21 func newBackgroundContext(ctx context.Context) context.Context { 22 if ctx == nil { 23 return context.Background() 24 } 25 26 return &backgroundContext{ 27 Context: context.Background(), 28 childValuesCtx: ctx, 29 } 30 } 31 32 func (b *backgroundContext) Value(key interface{}) interface{} { 33 return b.childValuesCtx.Value(key) 34 } 35