generated from moul/golang-repo-template
-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
76 additions
and
0 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,31 @@ | ||
package u | ||
|
||
import "context" | ||
|
||
// MonoChild is a goroutine manager (parent) that can only have one child at a time. | ||
// When you call MonoChild.SetChild(), MonoChild cancels the previous child context (if any), then run a new child. | ||
// The child needs to auto-kill itself when its context is done. | ||
type MonoChild interface { | ||
SetChild(childFn func(context.Context)) | ||
} | ||
|
||
func NewMonoChild(ctx context.Context) MonoChild { return &monoChild{ctx: ctx} } | ||
|
||
type monoChild struct { | ||
ctx context.Context | ||
lastChildCancelFn func() | ||
} | ||
|
||
func (parent *monoChild) SetChild(childFn func(context.Context)) { | ||
if parent.ctx == nil { | ||
panic("requires a parent context") | ||
} | ||
if parent.lastChildCancelFn != nil { | ||
parent.lastChildCancelFn() | ||
} | ||
|
||
var childCtx context.Context | ||
childCtx, parent.lastChildCancelFn = context.WithCancel(parent.ctx) | ||
|
||
go childFn(childCtx) | ||
} |
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,45 @@ | ||
package u_test | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
"moul.io/u" | ||
) | ||
|
||
func ExampleMonoChild() { | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
parent := u.NewMonoChild(ctx) | ||
|
||
parent.SetChild(func(ctx context.Context) { | ||
select { | ||
case <-time.After(50 * time.Millisecond): | ||
fmt.Print("A") | ||
case <-ctx.Done(): | ||
} | ||
}) | ||
|
||
time.Sleep(100 * time.Millisecond) | ||
|
||
parent.SetChild(func(ctx context.Context) { | ||
select { | ||
case <-time.After(50 * time.Millisecond): | ||
fmt.Print("B") | ||
case <-ctx.Done(): | ||
} | ||
}) | ||
|
||
parent.SetChild(func(ctx context.Context) { | ||
select { | ||
case <-time.After(50 * time.Millisecond): | ||
fmt.Print("C") | ||
case <-ctx.Done(): | ||
} | ||
}) | ||
|
||
time.Sleep(100 * time.Millisecond) | ||
|
||
// Output: AC | ||
} |