Fix(picker): add WithTimeout for some situation

This commit is contained in:
Dreamacro
2019-07-18 00:12:01 +08:00
parent 0670275533
commit f00dfdd34d
5 changed files with 22 additions and 9 deletions

View File

@ -3,6 +3,7 @@ package picker
import (
"context"
"sync"
"time"
)
// Picker provides synchronization, and Context cancelation
@ -18,11 +19,19 @@ type Picker struct {
}
// WithContext returns a new Picker and an associated Context derived from ctx.
// and cancel when first element return.
func WithContext(ctx context.Context) (*Picker, context.Context) {
ctx, cancel := context.WithCancel(ctx)
return &Picker{cancel: cancel}, ctx
}
// WithTimeout returns a new Picker and an associated Context derived from ctx with timeout,
// but it doesn't cancel when first element return.
func WithTimeout(ctx context.Context, timeout time.Duration) (*Picker, context.Context, context.CancelFunc) {
ctx, cancel := context.WithTimeout(ctx, timeout)
return &Picker{}, ctx, cancel
}
// Wait blocks until all function calls from the Go method have returned,
// then returns the first nil error result (if any) from them.
func (p *Picker) Wait() interface{} {