1 // Copyright (C) The Lightning Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
15 type batchArgs struct {
20 func (b *batchArgs) Flags(flags *flag.FlagSet) {
21 flags.IntVar(&b.batches, "batches", 1, "number of batches")
22 flags.IntVar(&b.batch, "batch", -1, "only do `N`th batch (-1 = all)")
25 func (b *batchArgs) Args(batch int) []string {
27 fmt.Sprintf("-batches=%d", b.batches),
28 fmt.Sprintf("-batch=%d", batch),
32 // RunBatches calls runFunc once per batch, and returns a slice of
33 // return values and the first returned error, if any.
34 func (b *batchArgs) RunBatches(ctx context.Context, runFunc func(context.Context, int) (string, error)) ([]string, error) {
35 ctx, cancel := context.WithCancel(ctx)
37 outputs := make([]string, b.batches)
39 for batch := 0; batch < b.batches; batch++ {
40 if b.batch >= 0 && b.batch != batch {
47 out, err := runFunc(ctx, batch)
57 outputs = outputs[b.batch : b.batch+1]
62 func (b *batchArgs) Slice(in []string) []string {
63 if b.batches == 0 || b.batch < 0 {
66 batchsize := (len(in) + b.batches - 1) / b.batches
67 out := in[batchsize*b.batch:]
68 if len(out) > batchsize {
74 type WaitGroup struct {
80 func (wg *WaitGroup) Error(err error) {
82 wg.errOnce.Do(func() { wg.err = err })
86 func (wg *WaitGroup) Wait() error {