-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support attaching to Stdin and TTY from multiple clients at the same …
…time
- Loading branch information
1 parent
01b23b9
commit 2b68f7c
Showing
5 changed files
with
543 additions
and
64 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package ctxio | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
) | ||
|
||
// MultiContext returns a context that is canceled when any of the provided contexts are canceled. | ||
func MultiContext(ctxs ...context.Context) (context.Context, context.CancelFunc) { | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
|
||
var once sync.Once | ||
for _, c := range ctxs { | ||
c := c // Capture range variable | ||
go func() { | ||
select { | ||
case <-c.Done(): | ||
once.Do(func() { | ||
cancel() | ||
}) | ||
case <-ctx.Done(): | ||
// The merged context was canceled elsewhere | ||
} | ||
}() | ||
} | ||
|
||
return ctx, cancel | ||
} |
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
Oops, something went wrong.