Skip to content

Commit 3713ed4

Browse files
committed
add comments to BatchFuture
1 parent f6b8ece commit 3713ed4

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

internal/batch/batch_future.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import (
99
"go.uber.org/cadence/internal"
1010
)
1111

12-
// BatchFutureImpl is an implementation of BatchFuture
13-
type BatchFutureImpl struct {
12+
// BatchFuture is an implementation of public BatchFuture interface.
13+
type BatchFuture struct {
1414
futures []internal.Future
1515
settables []internal.Settable
1616
factories []func(ctx internal.Context) internal.Future
@@ -20,7 +20,7 @@ type BatchFutureImpl struct {
2020
wg internal.WaitGroup
2121
}
2222

23-
func NewBatchFuture(ctx internal.Context, batchSize int, factories []func(ctx internal.Context) internal.Future) (*BatchFutureImpl, error) {
23+
func NewBatchFuture(ctx internal.Context, batchSize int, factories []func(ctx internal.Context) internal.Future) (*BatchFuture, error) {
2424
var futures []internal.Future
2525
var settables []internal.Settable
2626
for range factories {
@@ -29,7 +29,7 @@ func NewBatchFuture(ctx internal.Context, batchSize int, factories []func(ctx in
2929
settables = append(settables, settable)
3030
}
3131

32-
batchFuture := &BatchFutureImpl{
32+
batchFuture := &BatchFuture{
3333
futures: futures,
3434
settables: settables,
3535
factories: factories,
@@ -41,11 +41,11 @@ func NewBatchFuture(ctx internal.Context, batchSize int, factories []func(ctx in
4141
return batchFuture, nil
4242
}
4343

44-
func (b *BatchFutureImpl) GetFutures() []internal.Future {
44+
func (b *BatchFuture) GetFutures() []internal.Future {
4545
return b.futures
4646
}
4747

48-
func (b *BatchFutureImpl) start(ctx internal.Context) {
48+
func (b *BatchFuture) start(ctx internal.Context) {
4949

5050
buffered := internal.NewBufferedChannel(ctx, b.batchSize) // buffered channel to limit the number of concurrent futures
5151
channel := internal.NewNamedChannel(ctx, "batch-future-channel")
@@ -87,7 +87,7 @@ func (b *BatchFutureImpl) start(ctx internal.Context) {
8787
})
8888
}
8989

90-
func (b *BatchFutureImpl) IsReady() bool {
90+
func (b *BatchFuture) IsReady() bool {
9191
for _, future := range b.futures {
9292
if !future.IsReady() {
9393
return false
@@ -101,7 +101,7 @@ func (b *BatchFutureImpl) IsReady() bool {
101101
// If valuePtr is a pointer to a slice, the slice will be resized to the length of the futures. Each element of the slice will be assigned with the underlying Future.Get() and thus behaves the same way.
102102
// If valuePtr is nil, no assignment will be made.
103103
// If error occurs, values will be set on successful futures and the errors of failed futures will be returned.
104-
func (b *BatchFutureImpl) Get(ctx internal.Context, valuePtr interface{}) error {
104+
func (b *BatchFuture) Get(ctx internal.Context, valuePtr interface{}) error {
105105
// No assignment if valuePtr is nil
106106
if valuePtr == nil {
107107
b.wg.Wait(ctx)

x/batch.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,25 @@ import (
55
"go.uber.org/cadence/workflow"
66
)
77

8-
// BatchFuture is a interface that extends workflow.Future and adds a method to get the futures created
8+
var _ workflow.Future = (BatchFuture)(nil) // to ensure it's compatible
9+
10+
// BatchFuture wraps a collection of futures, and provides some convenience methods for dealing with them in bulk.
911
type BatchFuture interface {
10-
workflow.Future
12+
// IsReady returns true when all wrapped futures return true from their IsReady
13+
IsReady() bool
14+
// Get acts like workflow.Future.Get, but it reads out all wrapped futures into the provided slice pointer.
15+
// You MUST either
16+
// 1. provide a pointer to a slice as the value-pointer here, but the slice itself can be nil - it will be allocated and/or resized to fit if needed.
17+
// 2. provide a nil to indicate that you don't want to collect the results.
18+
//
19+
// This call will wait for all futures to resolve, and will then write all results to the output slice in the same order as the input.
20+
//
21+
// Any errors encountered are merged with go.uber.org/multierr, so single errors are
22+
// exposed normally, but multiple ones are bundled in the same way as errors.Join.
23+
// For consistency when checking individual errors, consider using `multierr.Errors(err)` in all cases.
24+
Get(ctx workflow.Context, valuePtr interface{}) error
25+
// GetFutures returns a slice of all the wrapped futures.
26+
// This slice MUST NOT be modified, but the individual futures can be used normally.
1127
GetFutures() []workflow.Future
1228
}
1329

0 commit comments

Comments
 (0)