cancel.go 2.9 KB

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