github.com/moby/docker@v26.1.3+incompatible/internal/compatcontext/cancel.go (about)

     1  //go:build !go1.21
     2  
     3  // Copyright (c) 2009 The Go Authors. All rights reserved.
     4  //
     5  // Redistribution and use in source and binary forms, with or without
     6  // modification, are permitted provided that the following conditions are
     7  // met:
     8  //
     9  //   - Redistributions of source code must retain the above copyright
    10  //
    11  // notice, this list of conditions and the following disclaimer.
    12  //   - Redistributions in binary form must reproduce the above
    13  //
    14  // copyright notice, this list of conditions and the following disclaimer
    15  // in the documentation and/or other materials provided with the
    16  // distribution.
    17  //   - Neither the name of Google Inc. nor the names of its
    18  //
    19  // contributors may be used to endorse or promote products derived from
    20  // this software without specific prior written permission.
    21  //
    22  // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    23  // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    24  // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    25  // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
    26  // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    27  // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    28  // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    29  // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    30  // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    31  // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    32  // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    33  //
    34  // Source: https://cs.opensource.google/go/go/+/refs/tags/go1.21.1:src/context/context.go
    35  // The only modifications to the original source were:
    36  // - replacing the usage of internal reflectlite with reflect
    37  // - replacing the usage of private value function with Value method call
    38  package compatcontext // import "github.com/docker/docker/internal/compatcontext"
    39  
    40  import (
    41  	"context"
    42  	"reflect"
    43  	"time"
    44  )
    45  
    46  // WithoutCancel returns a copy of parent that is not canceled when parent is canceled.
    47  // The returned context returns no Deadline or Err, and its Done channel is nil.
    48  // Calling [Cause] on the returned context returns nil.
    49  func WithoutCancel(parent context.Context) context.Context {
    50  	if parent == nil {
    51  		panic("cannot create context from nil parent")
    52  	}
    53  	return withoutCancelCtx{parent}
    54  }
    55  
    56  type withoutCancelCtx struct {
    57  	c context.Context
    58  }
    59  
    60  func (withoutCancelCtx) Deadline() (deadline time.Time, ok bool) {
    61  	return
    62  }
    63  
    64  func (withoutCancelCtx) Done() <-chan struct{} {
    65  	return nil
    66  }
    67  
    68  func (withoutCancelCtx) Err() error {
    69  	return nil
    70  }
    71  
    72  func (c withoutCancelCtx) Value(key any) any {
    73  	return c.c.Value(key)
    74  }
    75  
    76  func (c withoutCancelCtx) String() string {
    77  	return contextName(c.c) + ".WithoutCancel"
    78  }
    79  
    80  type stringer interface {
    81  	String() string
    82  }
    83  
    84  func contextName(c context.Context) string {
    85  	if s, ok := c.(stringer); ok {
    86  		return s.String()
    87  	}
    88  	return reflect.TypeOf(c).String()
    89  }