Documentation
¶
Overview ¶
Package ctxs は、context.Context に関するユーティリティが配置されています。
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ToDoneCh ¶
ToDoneCh は、指定された context.Context から done チャネルを取り出してスライスで返します.
取得した done チャネルのスライスを chans.WhenAll() に指定すると全コンテキストが完了するまで 待機することが出来ます。
Example:
<-chans.WhenAll(ctxs.ToDoneCh(contexts...)...) // contexts is []context.Context
Example ¶
package main
import (
"context"
"fmt"
"strconv"
"time"
"github.com/devlights/gomy/chans"
"github.com/devlights/gomy/ctxs"
)
func main() {
const (
goroutineCount = 3
)
var (
iter = func(n int) []struct{} { return make([]struct{}, n) }
g = func(pCtx context.Context, name string, timeout time.Duration) context.Context {
ctx, cancel := context.WithTimeout(pCtx, timeout)
go func() {
defer cancel()
fmt.Printf("[%s] start\n", name)
time.Sleep(10 * time.Millisecond)
fmt.Printf("[%s] done\n", name)
}()
return ctx
}
)
var (
rootCtx = context.Background()
mainCtx, mainCancel = context.WithCancel(rootCtx)
)
defer mainCancel()
contexts := make([]context.Context, goroutineCount)
for i := range iter(goroutineCount) {
contexts[i] = g(mainCtx, strconv.Itoa(i), 500*time.Millisecond)
}
<-chans.WhenAll(ctxs.ToDoneCh(contexts...)...)
fmt.Println("[main] done")
}
Output: [0] start [1] start [2] start [2] done [0] done [1] done [main] done
func WhenAll ¶ added in v0.2.16
WhenAll は、chans.WhenAll() の context.Context 版です.
以下の条件のいずれかが満たされた場合に完了となる context.Context を生成して返します.
- pCtx が完了
- pCtx 以外のコンテキストが全て完了
Example:
<-ctxs.WhenAll(procCtx, ctx1, ctx2, ctx3).Done()
Example ¶
package main
import (
"context"
"fmt"
"time"
"github.com/devlights/gomy/ctxs"
)
func main() {
var (
rootCtx = context.Background()
mainCtx, mainCancel = context.WithCancel(rootCtx)
procCtx, procCancel = context.WithTimeout(mainCtx, 1*time.Second)
)
defer mainCancel()
defer procCancel()
ctx1 := ctxs.StartGoroutine(procCtx, 100*time.Millisecond)
ctx2 := ctxs.StartGoroutine(procCtx, 200*time.Millisecond)
ctx3 := ctxs.StartGoroutine(procCtx, 300*time.Millisecond)
start := time.Now()
<-ctxs.WhenAll(procCtx, ctx1, ctx2, ctx3).Done()
elapsed := time.Since(start)
fmt.Printf("elapsed: about 300msec ==> %v\n", elapsed <= 310*time.Millisecond)
}
Output: elapsed: about 300msec ==> true
func WhenAny ¶ added in v0.2.16
WhenAny は、chans.WhenAny() の context.Context 版です.
指定したコンテキストのどれかが完了したら完了となる context.Context を生成して返します.
Example:
<-ctxs.WhenAny(procCtx, ctx1, ctx2, ctx3).Done()
Example ¶
package main
import (
"context"
"fmt"
"time"
"github.com/devlights/gomy/ctxs"
)
func main() {
var (
rootCtx = context.Background()
mainCtx, mainCancel = context.WithCancel(rootCtx)
procCtx, procCancel = context.WithTimeout(mainCtx, 1*time.Second)
)
defer mainCancel()
defer procCancel()
ctx1 := ctxs.StartGoroutine(procCtx, 100*time.Millisecond)
ctx2 := ctxs.StartGoroutine(procCtx, 200*time.Millisecond)
ctx3 := ctxs.StartGoroutine(procCtx, 300*time.Millisecond)
start := time.Now()
<-ctxs.WhenAny(procCtx, ctx1, ctx2, ctx3).Done()
elapsed := time.Since(start)
fmt.Printf("elapsed: about 100msec ==> %v\n", elapsed <= 110*time.Millisecond)
}
Output: elapsed: about 100msec ==> true
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.