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() 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() 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 CancelWithCause(ctx context.Context, cause error) error
- func CancelWithCauseAndKey(ctx context.Context, cause error, key any) error
- func CancelWithKey(ctx context.Context, key any) error
- func Cleanup(ctx context.Context, f func() error) error
- func CleanupWithKey(ctx context.Context, key any, f func() 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 WithCancelCause(ctx context.Context) (context.Context, context.CancelCauseFunc)
- func WithCancelCauseWithKey(ctx context.Context, key any) (context.Context, context.CancelCauseFunc)
- func WithCancelWithKey(ctx context.Context, key any) (context.Context, context.CancelFunc)
- func WithDeadline(ctx context.Context, d time.Time) (context.Context, context.CancelFunc)
- func WithDeadlineCause(ctx context.Context, d time.Time, cause error) (context.Context, context.CancelFunc)
- func WithDeadlineCauseWithKey(ctx context.Context, d time.Time, cause error, key any) (context.Context, context.CancelFunc)
- func WithDeadlineWithKey(ctx context.Context, d time.Time, key any) (context.Context, context.CancelFunc)
- func WithTimeout(ctx context.Context, timeout time.Duration) (context.Context, context.CancelFunc)
- func WithTimeoutCause(ctx context.Context, timeout time.Duration, cause error) (context.Context, context.CancelFunc)
- func WithTimeoutCauseWithKey(ctx context.Context, timeout time.Duration, cause error, key any) (context.Context, context.CancelFunc)
- func WithTimeoutWithKey(ctx context.Context, timeout time.Duration, key any) (context.Context, context.CancelFunc)
- func WithoutCancel(ctx context.Context) context.Context
- func WithoutCancelWithKey(ctx context.Context, key any) context.Context
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrNotContainDoneGroup = errors.New("donegroup: context does not contain a doneGroup. Use donegroup.With* 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 CancelWithCause ¶ added in v1.7.0
CancelWithCause cancels the context with cause. Then calls the function registered by Cleanup.
func CancelWithCauseAndKey ¶ added in v1.9.0
CancelWithCauseAndKey cancels the context with cause.
func CancelWithKey ¶ added in v1.2.0
CancelWithKey cancels the context.
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() error { fmt.Println("cleanup start") for i := 0; i < 10; i++ { 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 WithCancelCause ¶ added in v1.6.0
WithCancelCause returns a copy of parent with a new Done channel and a doneGroup.
func WithCancelCauseWithKey ¶ added in v1.6.0
func WithCancelCauseWithKey(ctx context.Context, key any) (context.Context, context.CancelCauseFunc)
WithCancelCauseWithKey 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.
func WithDeadline ¶ added in v1.6.0
WithDeadline returns a copy of parent with a new Done channel and a doneGroup. If the deadline is exceeded, the cause is set to context.DeadlineExceeded.
func WithDeadlineCause ¶ added in v1.6.0
func WithDeadlineCause(ctx context.Context, d time.Time, cause error) (context.Context, context.CancelFunc)
WithDeadlineCause returns a copy of parent with a new Done channel and a doneGroup.
func WithDeadlineCauseWithKey ¶ added in v1.6.0
func WithDeadlineCauseWithKey(ctx context.Context, d time.Time, cause error, key any) (context.Context, context.CancelFunc)
WithDeadlineCauseWithKey returns a copy of parent with a new Done channel and a doneGroup.
func WithDeadlineWithKey ¶ added in v1.6.0
func WithDeadlineWithKey(ctx context.Context, d time.Time, key any) (context.Context, context.CancelFunc)
WithDeadlineWithKey returns a copy of parent with a new Done channel and a doneGroup.
func WithTimeout ¶ added in v1.6.0
WithTimeout returns a copy of parent with a new Done channel and a doneGroup. If the timeout is exceeded, the cause is set to context.DeadlineExceeded.
func WithTimeoutCause ¶ added in v1.6.0
func WithTimeoutCause(ctx context.Context, timeout time.Duration, cause error) (context.Context, context.CancelFunc)
WithTimeoutCause returns a copy of parent with a new Done channel and a doneGroup.
func WithTimeoutCauseWithKey ¶ added in v1.6.0
func WithTimeoutCauseWithKey(ctx context.Context, timeout time.Duration, cause error, key any) (context.Context, context.CancelFunc)
WithTimeoutCauseWithKey returns a copy of parent with a new Done channel and a doneGroup.
func WithTimeoutWithKey ¶ added in v1.6.0
func WithTimeoutWithKey(ctx context.Context, timeout time.Duration, key any) (context.Context, context.CancelFunc)
WithTimeoutWithKey returns a copy of parent with a new Done channel and a doneGroup.
func WithoutCancel ¶ added in v1.10.0
WithoutCancel returns a copy of parent that is not canceled when parent is canceled and does not have a doneGroup.
Types ¶
This section is empty.