Skip to content

Commit dafbd9a

Browse files
committed
fix context options
1 parent fc5d216 commit dafbd9a

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

options.go

+11-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
// BlockOption define an option function applied to a nursery block.
99
type BlockOption func(cfg *nursery)
1010

11-
// WithContext returns a nursery block option that replace nursery context with
11+
// WithContext returns a nursery block option that replaces nursery context with
1212
// the given one.
1313
func WithContext(ctx context.Context) BlockOption {
1414
return func(n *nursery) {
@@ -20,15 +20,23 @@ func WithContext(ctx context.Context) BlockOption {
2020
// a new one that timeout after the given duration.
2121
func WithTimeout(timeout time.Duration) BlockOption {
2222
return func(n *nursery) {
23-
n.Context, n.cancel = context.WithTimeout(n.Context, timeout)
23+
ctx := n.Context
24+
if ctx == nil {
25+
ctx = context.Background()
26+
}
27+
n.Context, n.cancel = context.WithTimeout(ctx, timeout)
2428
}
2529
}
2630

2731
// WithDeadline returns a nursery block option that wraps nursery context with
2832
// a new one that will be canceled at `d`.
2933
func WithDeadline(d time.Time) BlockOption {
3034
return func(n *nursery) {
31-
n.Context, n.cancel = context.WithDeadline(n.Context, d)
35+
ctx := n.Context
36+
if ctx == nil {
37+
ctx = context.Background()
38+
}
39+
n.Context, n.cancel = context.WithDeadline(ctx, d)
3240
}
3341
}
3442

0 commit comments

Comments
 (0)