Chore: adjust batch

This commit is contained in:
gVisor bot 2021-07-23 00:30:23 +08:00
parent 8928b3d71d
commit 44360ef29b
3 changed files with 8 additions and 13 deletions

View file

@ -59,7 +59,7 @@ func (hc *HealthCheck) touch() {
} }
func (hc *HealthCheck) check() { func (hc *HealthCheck) check() {
b := batch.New(batch.WithConcurrencyNum(10)) b, _ := batch.New(context.Background(), batch.WithConcurrencyNum(10))
for _, proxy := range hc.proxies { for _, proxy := range hc.proxies {
p := proxy p := proxy
b.Go(p.Name(), func() (interface{}, error) { b.Go(p.Name(), func() (interface{}, error) {

View file

@ -89,7 +89,9 @@ func (b *Batch) Result() map[string]Result {
return copy return copy
} }
func New(opts ...Option) *Batch { func New(ctx context.Context, opts ...Option) (*Batch, context.Context) {
ctx, cancel := context.WithCancel(ctx)
b := &Batch{ b := &Batch{
result: map[string]Result{}, result: map[string]Result{},
} }
@ -98,14 +100,6 @@ func New(opts ...Option) *Batch {
o(b) o(b)
} }
return b
}
func WithContext(ctx context.Context, opts ...Option) (*Batch, context.Context) {
ctx, cancel := context.WithCancel(ctx)
b := New(opts...)
b.cancel = cancel b.cancel = cancel
return b, ctx return b, ctx
} }

View file

@ -11,7 +11,7 @@ import (
) )
func TestBatch(t *testing.T) { func TestBatch(t *testing.T) {
b := New() b, _ := New(context.Background())
now := time.Now() now := time.Now()
b.Go("foo", func() (interface{}, error) { b.Go("foo", func() (interface{}, error) {
@ -37,7 +37,8 @@ func TestBatch(t *testing.T) {
} }
func TestBatchWithConcurrencyNum(t *testing.T) { func TestBatchWithConcurrencyNum(t *testing.T) {
b := New( b, _ := New(
context.Background(),
WithConcurrencyNum(3), WithConcurrencyNum(3),
) )
@ -61,7 +62,7 @@ func TestBatchWithConcurrencyNum(t *testing.T) {
} }
func TestBatchContext(t *testing.T) { func TestBatchContext(t *testing.T) {
b, ctx := WithContext(context.Background()) b, ctx := New(context.Background())
b.Go("error", func() (interface{}, error) { b.Go("error", func() (interface{}, error) {
time.Sleep(time.Millisecond * 100) time.Sleep(time.Millisecond * 100)