Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Group ¶
type Group struct {
// contains filtered or unexported fields
}
func (*Group) Add ¶
Example (Basic) ¶
package main import ( "context" "errors" "fmt" "time" "github.com/badu/wg-cc/pkg/runner" ) func main() { var g runner.Group { cancel := make(chan struct{}) g.Add(func() error { select { case <-time.After(time.Second): fmt.Printf("The first funcPair had its time elapsed\n") return nil case <-cancel: fmt.Printf("The first funcPair was canceled\n") return nil } }, func(err error) { fmt.Printf("The first funcPair was interrupted with: %v\n", err) close(cancel) }) } { g.Add(func() error { fmt.Printf("The second funcPair is returning immediately\n") return errors.New("immediate teardown") }, func(err error) { // Note that this interrupt function is called, even though the // corresponding execute function has already returned. fmt.Printf("The second funcPair was interrupted with: %v\n", err) }) } ctx, cancel := context.WithCancel(context.Background()) fmt.Printf("The group was terminated with: %v\n", g.Wait(ctx, cancel)) }
Output: The second funcPair is returning immediately The first funcPair was interrupted with: immediate teardown The second funcPair was interrupted with: immediate teardown The first funcPair was canceled The group was terminated with: immediate teardown
Example (Context) ¶
package main import ( "context" "fmt" "github.com/badu/wg-cc/pkg/runner" ) func main() { ctx, cancel := context.WithCancel(context.Background()) var g runner.Group { ctx, cancel := context.WithCancel(ctx) // note: shadowed g.Add(func() error { return runUntilCanceled(ctx) }, func(error) { cancel() }) } go cancel() fmt.Printf("The group was terminated with: %v\n", g.Wait(ctx, cancel)) } func runUntilCanceled(ctx context.Context) error { <-ctx.Done() return ctx.Err() }
Output: The group was terminated with: context done
Example (Listener) ¶
package main import ( "context" "errors" "fmt" "net" "net/http" "github.com/badu/wg-cc/pkg/runner" ) func main() { var g runner.Group { ln, _ := net.Listen("tcp", ":0") g.Add(func() error { defer fmt.Printf("http.Serve returned\n") return http.Serve(ln, http.NewServeMux()) }, func(error) { ln.Close() }) } { g.Add(func() error { return errors.New("immediate teardown") }, func(error) { // }) } ctx, cancel := context.WithCancel(context.Background()) fmt.Printf("The group was terminated with: %v\n", g.Wait(ctx, cancel)) }
Output: http.Serve returned The group was terminated with: immediate teardown
Click to show internal directories.
Click to hide internal directories.