-
Notifications
You must be signed in to change notification settings - Fork 16
/
iterator.go
129 lines (118 loc) · 3.02 KB
/
iterator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package cloudstorage
import (
"math"
"math/rand"
"time"
"golang.org/x/net/context"
"google.golang.org/api/iterator"
)
// ObjectsAll get all objects for an iterator.
func ObjectsAll(iter ObjectIterator) (Objects, error) {
objs := make(Objects, 0)
for {
o, err := iter.Next()
if err == iterator.Done {
break
} else if err != nil {
return nil, err
}
objs = append(objs, o)
}
return objs, nil
}
// ObjectResponseFromIter get all objects for an iterator.
func ObjectResponseFromIter(iter ObjectIterator) (*ObjectsResponse, error) {
objs, err := ObjectsAll(iter)
if err != nil {
return nil, err
}
return &ObjectsResponse{Objects: objs}, nil
}
// ObjectPageIterator iterator to facilitate easy paging through store.List() method
// to read all Objects that matched query.
type ObjectPageIterator struct {
s Store
ctx context.Context
cancel context.CancelFunc
q Query
cursor int
page Objects
}
// NewObjectPageIterator create an iterator that wraps the store List interface.
func NewObjectPageIterator(ctx context.Context, s Store, q Query) ObjectIterator {
cancelCtx, cancel := context.WithCancel(ctx)
return &ObjectPageIterator{
s: s,
ctx: cancelCtx,
cancel: cancel,
q: q,
}
}
func (it *ObjectPageIterator) returnPageNext() (Object, error) {
it.cursor++
return it.page[it.cursor-1], nil
}
// Close the object iterator.
func (it *ObjectPageIterator) Close() {
defer func() { recover() }()
select {
case <-it.ctx.Done():
// done
default:
it.cancel()
}
}
// Next iterator to go to next object or else returns error for done.
func (it *ObjectPageIterator) Next() (Object, error) {
retryCt := 0
select {
case <-it.ctx.Done():
// If iterator has been closed
return nil, it.ctx.Err()
default:
if it.cursor < len(it.page) {
return it.returnPageNext()
} else if it.cursor > 0 && it.q.Marker == "" {
// no new page, lets return
return nil, iterator.Done
}
for {
resp, err := it.s.List(it.ctx, it.q)
if err == nil {
it.page = resp.Objects
it.cursor = 0
it.q.Marker = resp.NextMarker
if len(it.page) == 0 {
return nil, iterator.Done
}
return it.returnPageNext()
} else if err == iterator.Done {
return nil, err
} else if err == context.Canceled || err == context.DeadlineExceeded {
// Return to user
return nil, err
}
if retryCt < 5 {
Backoff(retryCt)
} else {
return nil, err
}
retryCt++
}
}
}
// Backoff sleeps a random amount so we can.
// retry failed requests using a randomized exponential backoff:
// wait a random period between [0..1] seconds and retry; if that fails,
// wait a random period between [0..2] seconds and retry; if that fails,
// wait a random period between [0..4] seconds and retry, and so on,
// with an upper bounds to the wait period being 16 seconds.
// http://play.golang.org/p/l9aUHgiR8J
func Backoff(try int) {
nf := math.Pow(2, float64(try))
nf = math.Max(1, nf)
nf = math.Min(nf, 16)
r := rand.Int31n(int32(nf))
d := time.Duration(r) * time.Second
time.Sleep(d)
}