Skip to content

Commit f606c4e

Browse files
committed
Add cleanup package for context management during cleanup
Provides a couple helper functions that provide a background context for running cleanup jobs while preserving the original context values. The new contexts will not inherit the errors or cancellations. Signed-off-by: Derek McGowan <[email protected]>
1 parent c5c636b commit f606c4e

File tree

2 files changed

+110
-0
lines changed

2 files changed

+110
-0
lines changed

pkg/cleanup/context.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
Copyright The containerd Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
// Package providing utilies to help cleanup
18+
package cleanup
19+
20+
import (
21+
"context"
22+
"time"
23+
)
24+
25+
type clearCancel struct {
26+
context.Context
27+
}
28+
29+
func (cc clearCancel) Deadline() (deadline time.Time, ok bool) {
30+
return
31+
}
32+
33+
func (cc clearCancel) Done() <-chan struct{} {
34+
return nil
35+
}
36+
37+
func (cc clearCancel) Err() error {
38+
return nil
39+
}
40+
41+
// Background creates a new context which clears out the parent errors
42+
func Background(ctx context.Context) context.Context {
43+
return clearCancel{ctx}
44+
}
45+
46+
// Do runs the provided function with a context in which the
47+
// errors are cleared out and will timeout after 10 seconds.
48+
func Do(ctx context.Context, do func(context.Context)) {
49+
ctx, cancel := context.WithTimeout(clearCancel{ctx}, 10*time.Second)
50+
do(ctx)
51+
cancel()
52+
}

pkg/cleanup/context_test.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
Copyright The containerd Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package cleanup
18+
19+
import (
20+
"context"
21+
"testing"
22+
23+
"github.com/stretchr/testify/assert"
24+
)
25+
26+
func TestBackground(t *testing.T) {
27+
ctx, cancel := context.WithCancel(context.Background())
28+
var k struct{}
29+
v := "incontext"
30+
ctx = context.WithValue(ctx, k, v)
31+
32+
assert.Nil(t, contextError(ctx))
33+
assert.Equal(t, ctx.Value(k), v)
34+
35+
cancel()
36+
assert.Error(t, contextError(ctx))
37+
assert.Equal(t, ctx.Value(k), v)
38+
39+
// cleanup context should no longer be canceled
40+
ctx = Background(ctx)
41+
assert.Nil(t, contextError(ctx))
42+
assert.Equal(t, ctx.Value(k), v)
43+
44+
// cleanup contexts can be rewrapped in cancel context
45+
ctx, cancel = context.WithCancel(ctx)
46+
cancel()
47+
assert.Error(t, contextError(ctx))
48+
assert.Equal(t, ctx.Value(k), v)
49+
}
50+
51+
func contextError(ctx context.Context) error {
52+
select {
53+
case <-ctx.Done():
54+
return ctx.Err()
55+
default:
56+
return nil
57+
}
58+
}

0 commit comments

Comments
 (0)