Skip to content

Commit f6e44bc

Browse files
committed
internal: Add compatcontext.WithoutCancel
Copy the implementation of `context.WithoutCancel` introduced in Go 1.21 to be able to use it when building with older versions. This will use the stdlib directly when building with Go 1.21+. Signed-off-by: Paweł Gronowski <[email protected]>
1 parent d3afa80 commit f6e44bc

2 files changed

Lines changed: 98 additions & 0 deletions

File tree

internal/compatcontext/cancel.go

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//go:build go1.21
2+
3+
package compatcontext // import "github.com/docker/docker/internal/compatcontext"
4+
5+
import "context"
6+
7+
func WithoutCancel(ctx context.Context) context.Context {
8+
return context.WithoutCancel(ctx)
9+
}

0 commit comments

Comments
 (0)