Combines a set of Golang context.Context objects into one.
The returned context will act according to the following rules:
- It is fulfilled as soon as one of the components is fulfilled.
- If it involves an error, the merged object will return the same error.
- The context object has an additional method that returns the component index that was signalled.
import (
"github.com/mxmauro/mergecontext"
)
func main() {
ctx1, cancelCtx1 := context.WithTimeout(context.Background(), 5*time.Second)
defer cancelCtx1()
ctx2, cancelCtx2 := context.WithTimeout(context.Background(), 2*time.Second)
defer cancelCtx2()
ctx3, cancelCtx3 := context.WithTimeout(context.Background(), 8*time.Second)
defer cancelCtx3()
ctx := mergectx.New(ctx1, ctx2, ctx3)
<-ctx.Done()
if ctx.DoneIndex() != 1 {
// Error: The second context should be signalled
}
// ....
}See the license file for details.