-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #148 from sunmi-OS/feature/imp
add WithoutCancel context
- Loading branch information
Showing
3 changed files
with
35 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package utils | ||
|
||
import ( | ||
"context" | ||
"time" | ||
) | ||
|
||
// WithoutCancel returns a copy of parent that is not canceled when parent is canceled. | ||
// The returned context returns no Deadline or Err, and its Done channel is nil. | ||
func WithoutCancel(parent context.Context) context.Context { | ||
if parent == nil { | ||
parent = context.Background() | ||
} | ||
return withoutCancelCtx{parent} | ||
} | ||
|
||
type withoutCancelCtx struct { | ||
context.Context | ||
} | ||
|
||
func (withoutCancelCtx) Deadline() (deadline time.Time, ok bool) { | ||
return | ||
} | ||
|
||
func (withoutCancelCtx) Done() <-chan struct{} { | ||
return nil | ||
} | ||
|
||
func (withoutCancelCtx) Err() error { | ||
return nil | ||
} |