Skip to content

Commit a25f0e1

Browse files
jkawamotoguregu
authored andcommitted
iterators check if the context is canceled
It's better that each iterator's NextWithContext returns early if the given context is already canceled. Otherwise, they try to unmarshal an item if iter.output has a response or send an unnecessary request to the DB. It can be the caller's responsibility to avoid calling NextWithContext after the context gets canceled. However, it requires this kind of context check in every loop: ``` for ctx.Err() != nil && iter.NextWithContext(ctx, &out){ ... } if ctx.Err() != nil { return ctx.Err() } ``` If NextWithContext does it instead of the caller, we can avoid such duplicated code.
1 parent 2fabced commit a25f0e1

File tree

4 files changed

+12
-0
lines changed

4 files changed

+12
-0
lines changed

batchget.go

+3
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,9 @@ func (itr *bgIter) Next(out interface{}) bool {
199199

200200
func (itr *bgIter) NextWithContext(ctx aws.Context, out interface{}) bool {
201201
// stop if we have an error
202+
if ctx.Err() != nil {
203+
itr.err = ctx.Err()
204+
}
202205
if itr.err != nil {
203206
return false
204207
}

db.go

+3
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ func (itr *ltIter) Next(out interface{}) bool {
8282
}
8383

8484
func (itr *ltIter) NextWithContext(ctx aws.Context, out interface{}) bool {
85+
if ctx.Err() != nil {
86+
itr.err = ctx.Err()
87+
}
8588
if itr.err != nil {
8689
return false
8790
}

query.go

+3
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,9 @@ func (itr *queryIter) Next(out interface{}) bool {
325325

326326
func (itr *queryIter) NextWithContext(ctx aws.Context, out interface{}) bool {
327327
// stop if we have an error
328+
if ctx.Err() != nil {
329+
itr.err = ctx.Err()
330+
}
328331
if itr.err != nil {
329332
return false
330333
}

scan.go

+3
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,9 @@ func (itr *scanIter) Next(out interface{}) bool {
251251

252252
func (itr *scanIter) NextWithContext(ctx aws.Context, out interface{}) bool {
253253
// stop if we have an error
254+
if ctx.Err() != nil {
255+
itr.err = ctx.Err()
256+
}
254257
if itr.err != nil {
255258
return false
256259
}

0 commit comments

Comments
 (0)