Documentation ¶
Overview ¶
Example ¶
package main import ( "context" "fmt" "log" "time" "github.com/k1LoW/donegroup" ) func main() { ctx, cancel := donegroup.WithCancel(context.Background()) // Cleanup process of some kind if err := donegroup.Cleanup(ctx, func(_ context.Context) error { time.Sleep(10 * time.Millisecond) fmt.Println("cleanup with sleep") return nil }); err != nil { log.Fatal(err) } // Cleanup process of some kind if err := donegroup.Cleanup(ctx, func(_ context.Context) error { fmt.Println("cleanup") return nil }); err != nil { log.Fatal(err) } defer func() { cancel() if err := donegroup.Wait(ctx); err != nil { log.Fatal(err) } }() // Main process of some kind fmt.Println("main start") fmt.Println("main finish") }
Output: main start main finish cleanup cleanup with sleep
Index ¶
- Variables
- func Awaitable(ctx context.Context) (completed func())
- func AwaitableWithKey(ctx context.Context, key any) (completed func())
- func Awaiter(ctx context.Context) (completed func(), err error)
- func AwaiterWithKey(ctx context.Context, key any) (completed func(), err error)
- func Cancel(ctx context.Context) error
- func CancelWithContext(ctx, ctxw context.Context) error
- func CancelWithContextAndKey(ctx, ctxw context.Context, key any) error
- func CancelWithKey(ctx context.Context, key any) error
- func CancelWithTimeout(ctx context.Context, timeout time.Duration) error
- func CancelWithTimeoutAndKey(ctx context.Context, timeout time.Duration, key any) error
- func Cleanup(ctx context.Context, f func(ctx context.Context) error) error
- func CleanupWithKey(ctx context.Context, key any, f func(ctx context.Context) error) error
- func Go(ctx context.Context, f func() error)
- func GoWithKey(ctx context.Context, key any, f func() error)
- func Wait(ctx context.Context) error
- func WaitWithContext(ctx, ctxw context.Context) error
- func WaitWithContextAndKey(ctx, ctxw context.Context, key any) error
- func WaitWithKey(ctx context.Context, key any) error
- func WaitWithTimeout(ctx context.Context, timeout time.Duration) error
- func WaitWithTimeoutAndKey(ctx context.Context, timeout time.Duration, key any) error
- func WithCancel(ctx context.Context) (context.Context, context.CancelFunc)
- func WithCancelWithKey(ctx context.Context, key any) (context.Context, context.CancelFunc)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrNotContainDoneGroup = errors.New("donegroup: context does not contain a doneGroup. Use donegroup.WithCancel to create a context with a doneGroup")
Functions ¶
func Awaitable ¶ added in v1.3.0
Awaitable returns a function that guarantees execution of the process until it is called. Note that if the timeout of WaitWithTimeout has passed (or the context of WaitWithContext has canceled), it will not wait.
Example ¶
package main import ( "context" "fmt" "log" "time" "github.com/k1LoW/donegroup" ) func main() { ctx, cancel := donegroup.WithCancel(context.Background()) go func() { defer donegroup.Awaitable(ctx)() for { select { case <-ctx.Done(): time.Sleep(100 * time.Millisecond) fmt.Println("cleanup") return case <-time.After(10 * time.Millisecond): fmt.Println("do something") } } }() // Main process of some kind fmt.Println("main") time.Sleep(35 * time.Millisecond) cancel() if err := donegroup.Wait(ctx); err != nil { log.Fatal(err) } }
Output: main do something do something do something cleanup
func AwaitableWithKey ¶ added in v1.3.0
AwaitableWithKey returns a function that guarantees execution of the process until it is called. Note that if the timeout of WaitWithTimeout has passed (or the context of WaitWithContext has canceled), it will not wait.
func Awaiter ¶ added in v1.1.0
Awaiter returns a function that guarantees execution of the process until it is called. Note that if the timeout of WaitWithTimeout has passed (or the context of WaitWithContext has canceled), it will not wait.
Example ¶
package main import ( "context" "fmt" "log" "time" "github.com/k1LoW/donegroup" ) func main() { ctx, cancel := donegroup.WithCancel(context.Background()) go func() { completed, err := donegroup.Awaiter(ctx) if err != nil { log.Fatal(err) return } <-ctx.Done() time.Sleep(100 * time.Millisecond) fmt.Println("do something") completed() }() // Main process of some kind fmt.Println("main") time.Sleep(10 * time.Millisecond) cancel() if err := donegroup.Wait(ctx); err != nil { log.Fatal(err) } fmt.Println("finish") }
Output: main do something finish
func AwaiterWithKey ¶ added in v1.1.0
AwaiterWithKey returns a function that guarantees execution of the process until it is called. Note that if the timeout of WaitWithTimeout has passed (or the context of WaitWithContext has canceled), it will not wait.
func Cancel ¶ added in v1.2.0
Cancel cancels the context. Then calls the function registered by Cleanup.
func CancelWithContext ¶ added in v1.2.0
CancelWithContext cancels the context. Then calls the function registered by Cleanup with context (ctxw).
func CancelWithContextAndKey ¶ added in v1.2.0
CancelWithContextAndKey cancels the context. Then calls the function registered by Cleanup with context (ctxw).
func CancelWithKey ¶ added in v1.2.0
CancelWithKey cancels the context. Then calls the function registered by Cleanup.
func CancelWithTimeout ¶ added in v1.2.0
CancelWithTimeout cancels the context. Then calls the function registered by Cleanup with timeout.
func CancelWithTimeoutAndKey ¶ added in v1.2.0
CancelWithTimeoutAndKey cancels the context. Then calls the function registered by Cleanup with timeout.
func Cleanup ¶ added in v0.2.3
Cleanup registers a function to be called when the context is canceled.
func CleanupWithKey ¶ added in v0.2.3
CleanupWithKey Cleanup registers a function to be called when the context is canceled.
func Go ¶ added in v1.5.0
Go calls the function now asynchronously. If an error occurs, it is stored in the doneGroup. Note that if the timeout of WaitWithTimeout has passed (or the context of WaitWithContext has canceled), it will not wait.
func GoWithKey ¶ added in v1.5.0
GoWithKey calls the function now asynchronously. If an error occurs, it is stored in the doneGroup. Note that if the timeout of WaitWithTimeout has passed (or the context of WaitWithContext has canceled), it will not wait.
func Wait ¶
Wait blocks until the context is canceled. Then calls the function registered by Cleanup.
func WaitWithContext ¶
WaitWithContext blocks until the context (ctx) is canceled. Then calls the function registered by Cleanup with context (ctxw).
func WaitWithContextAndKey ¶
WaitWithContextAndKey blocks until the context is canceled. Then calls the function registered by Cleanup with context (ctxx).
func WaitWithKey ¶
WaitWithKey blocks until the context is canceled. Then calls the function registered by Cleanup.
func WaitWithTimeout ¶ added in v0.2.2
WaitWithTimeout blocks until the context (ctx) is canceled. Then calls the function registered by Cleanup with timeout.
Example ¶
package main import ( "context" "fmt" "log" "time" "github.com/k1LoW/donegroup" ) func main() { ctx, cancel := donegroup.WithCancel(context.Background()) // Cleanup process of some kind if err := donegroup.Cleanup(ctx, func(ctx context.Context) error { fmt.Println("cleanup start") for i := 0; i < 10; i++ { select { case <-ctx.Done(): return ctx.Err() default: time.Sleep(2 * time.Millisecond) } } fmt.Println("cleanup finish") return nil }); err != nil { log.Fatal(err) } defer func() { cancel() timeout := 5 * time.Millisecond if err := donegroup.WaitWithTimeout(ctx, timeout); err != nil { fmt.Println(err) } }() // Main process of some kind fmt.Println("main start") fmt.Println("main finish") }
Output: main start main finish cleanup start context deadline exceeded
func WaitWithTimeoutAndKey ¶ added in v0.2.2
WaitWithTimeoutAndKey blocks until the context is canceled. Then calls the function registered by Cleanup with timeout.
func WithCancel ¶
WithCancel returns a copy of parent with a new Done channel and a doneGroup.
func WithCancelWithKey ¶
WithCancelWithKey returns a copy of parent with a new Done channel and a doneGroup.
Types ¶
This section is empty.