Skip to content

Commit d86705a

Browse files
authored
Added chunking to enable batching ssm-run instances (#6)
1 parent fa4f56f commit d86705a

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

util/batch/chunk.go

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package batch
2+
3+
// ChunkFunc is a function when used e.g with chunk, provides a minimum and maximum range
4+
// to batch the items of a slice.
5+
type ChunkFunc func(min int, max int) (bool, error)
6+
7+
func Chunk(length, batch int, fn ChunkFunc) error {
8+
for i := 0; i < length; i += batch {
9+
j := i + batch
10+
11+
if j > length {
12+
j = length
13+
}
14+
15+
cont, err := fn(i, j)
16+
17+
if err != nil {
18+
return err
19+
}
20+
21+
if !cont {
22+
return nil
23+
}
24+
}
25+
26+
return nil
27+
}

util/batch/chunk_test.go

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package batch
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestChunk(t *testing.T) {
10+
items := []int{12, 22, 53, 24, 75, 96, 67, 18, 39, 10, 13, 99, 31}
11+
12+
counter := 0
13+
fn := func(min int, max int) (bool, error) {
14+
assert.Equal(t, counter, min)
15+
16+
if max == len(items) {
17+
assert.Equal(t, len(items), max)
18+
} else {
19+
assert.Equal(t, counter+2, max)
20+
}
21+
22+
counter += 2
23+
return true, nil
24+
}
25+
26+
err := Chunk(len(items), 2, fn)
27+
assert.NoError(t, err)
28+
}

0 commit comments

Comments
 (0)