presidium 0.16.19 → 0.16.20
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/DynamoStream.js +30 -29
- package/internal/RetryAwsErrors.js +21 -0
- package/package.json +1 -1
package/DynamoStream.js
CHANGED
|
@@ -2,6 +2,7 @@ const DynamoDBStreams = require('aws-sdk/clients/dynamodbstreams')
|
|
|
2
2
|
const rubico = require('rubico')
|
|
3
3
|
const rubicox = require('rubico/x')
|
|
4
4
|
const has = require('./internal/has')
|
|
5
|
+
const RetryAwsErrors = require('./internal/RetryAwsErrors')
|
|
5
6
|
const HttpAgent = require('./HttpAgent')
|
|
6
7
|
const Dynamo = require('./Dynamo')
|
|
7
8
|
const Mux = require('rubico/monad/Mux')
|
|
@@ -70,6 +71,23 @@ const DynamoStream = function (options) {
|
|
|
70
71
|
...awsCreds,
|
|
71
72
|
})
|
|
72
73
|
|
|
74
|
+
this.retryListStreams = RetryAwsErrors(
|
|
75
|
+
this.client.listStreams,
|
|
76
|
+
this.client,
|
|
77
|
+
)
|
|
78
|
+
this.retryDescribeStream = RetryAwsErrors(
|
|
79
|
+
this.client.describeStream,
|
|
80
|
+
this.client,
|
|
81
|
+
)
|
|
82
|
+
this.retryGetShardIterator = RetryAwsErrors(
|
|
83
|
+
this.client.getShardIterator,
|
|
84
|
+
this.client,
|
|
85
|
+
)
|
|
86
|
+
this.retryGetRecords = RetryAwsErrors(
|
|
87
|
+
this.client.getRecords,
|
|
88
|
+
this.client,
|
|
89
|
+
)
|
|
90
|
+
|
|
73
91
|
const dynamo = new Dynamo(awsCreds)
|
|
74
92
|
this.ready = dynamo.describeTable(this.table).then(pipe([
|
|
75
93
|
get('Table.StreamSpecification'),
|
|
@@ -87,19 +105,19 @@ DynamoStream.prototype.close = function close() {
|
|
|
87
105
|
|
|
88
106
|
// () => AsyncGenerator<Stream>
|
|
89
107
|
DynamoStream.prototype.getStreams = async function* getStreams() {
|
|
90
|
-
let streams = await this.
|
|
108
|
+
let streams = await this.retryListStreams({
|
|
91
109
|
Limit: this.listStreamsLimit,
|
|
92
110
|
TableName: this.table
|
|
93
|
-
})
|
|
111
|
+
})
|
|
94
112
|
if (streams.Streams.length > 0) {
|
|
95
113
|
yield* streams.Streams
|
|
96
114
|
}
|
|
97
115
|
while (!this.closed && streams.LastEvaluatedStreamArn != null) {
|
|
98
|
-
streams = await this.
|
|
116
|
+
streams = await this.retryListStreams({
|
|
99
117
|
Limit: this.listStreamsLimit,
|
|
100
118
|
TableName: this.table,
|
|
101
119
|
ExclusiveStartStreamArn: streams.LastEvaluatedStreamArn,
|
|
102
|
-
})
|
|
120
|
+
})
|
|
103
121
|
if (streams.Streams.length > 0) {
|
|
104
122
|
yield* streams.Streams
|
|
105
123
|
}
|
|
@@ -110,19 +128,19 @@ DynamoStream.prototype.getStreams = async function* getStreams() {
|
|
|
110
128
|
DynamoStream.prototype.getShards = async function* getShards(
|
|
111
129
|
Stream,
|
|
112
130
|
) {
|
|
113
|
-
let shards = await this.
|
|
131
|
+
let shards = await this.retryDescribeStream({
|
|
114
132
|
StreamArn: Stream.StreamArn,
|
|
115
133
|
Limit: 100,
|
|
116
|
-
}).
|
|
134
|
+
}).then(get('StreamDescription'))
|
|
117
135
|
if (shards.Shards.length > 0) {
|
|
118
136
|
yield* shards.Shards.map(assign({ Stream: always(Stream) }))
|
|
119
137
|
}
|
|
120
138
|
while (!this.closed && shards.LastEvaluatedShardId != null) {
|
|
121
|
-
shards = await this.
|
|
139
|
+
shards = await this.retryDescribeStream({
|
|
122
140
|
StreamArn: Stream.StreamArn,
|
|
123
141
|
Limit: 100,
|
|
124
142
|
ExclusiveStartShardId: shards.LastEvaluatedShardId,
|
|
125
|
-
}).
|
|
143
|
+
}).then(get('StreamDescription'))
|
|
126
144
|
if (shards.Shards.length > 0) {
|
|
127
145
|
yield* shards.Shards.map(assign({ Stream: always(Stream) }))
|
|
128
146
|
}
|
|
@@ -133,7 +151,7 @@ DynamoStream.prototype.getShards = async function* getShards(
|
|
|
133
151
|
DynamoStream.prototype.getRecords = async function* getRecords(
|
|
134
152
|
Shard,
|
|
135
153
|
) {
|
|
136
|
-
const startingShardIterator = await this.
|
|
154
|
+
const startingShardIterator = await this.retryGetShardIterator({
|
|
137
155
|
ShardId: Shard.ShardId,
|
|
138
156
|
StreamArn: Shard.Stream.StreamArn,
|
|
139
157
|
ShardIteratorType: Shard.ShardIteratorType,
|
|
@@ -145,19 +163,11 @@ DynamoStream.prototype.getRecords = async function* getRecords(
|
|
|
145
163
|
) ? { SequenceNumber: Shard.SequenceNumber } : {},
|
|
146
164
|
*/
|
|
147
165
|
|
|
148
|
-
}).
|
|
166
|
+
}).then(get('ShardIterator'))
|
|
149
167
|
|
|
150
|
-
let records = await this.
|
|
168
|
+
let records = await this.retryGetRecords({
|
|
151
169
|
ShardIterator: startingShardIterator,
|
|
152
170
|
Limit: this.getRecordsLimit
|
|
153
|
-
}).promise().catch(error => {
|
|
154
|
-
if (error.retryable) {
|
|
155
|
-
return this.client.getRecords({
|
|
156
|
-
ShardIterator: startingShardIterator,
|
|
157
|
-
Limit: this.getRecordsLimit
|
|
158
|
-
}).promise()
|
|
159
|
-
}
|
|
160
|
-
throw error
|
|
161
171
|
})
|
|
162
172
|
|
|
163
173
|
if (records.Records.length > 0) {
|
|
@@ -169,19 +179,10 @@ DynamoStream.prototype.getRecords = async function* getRecords(
|
|
|
169
179
|
await new Promise(resolve => setTimeout(resolve, this.getRecordsInterval))
|
|
170
180
|
|
|
171
181
|
while (!this.closed && records.NextShardIterator != null) {
|
|
172
|
-
records = await this.
|
|
182
|
+
records = await this.retryGetRecords({
|
|
173
183
|
ShardIterator: records.NextShardIterator,
|
|
174
184
|
Limit: this.getRecordsLimit
|
|
175
|
-
}).promise().catch(error => {
|
|
176
|
-
if (error.retryable) {
|
|
177
|
-
return this.client.getRecords({
|
|
178
|
-
ShardIterator: records.NextShardIterator,
|
|
179
|
-
Limit: this.getRecordsLimit
|
|
180
|
-
}).promise()
|
|
181
|
-
}
|
|
182
|
-
throw error
|
|
183
185
|
})
|
|
184
|
-
|
|
185
186
|
if (records.Records.length > 0) {
|
|
186
187
|
yield* records.Records.map(assign({
|
|
187
188
|
table: always(this.table),
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @name RetryAwsErrors
|
|
3
|
+
*
|
|
4
|
+
* @synopsis
|
|
5
|
+
* ```coffeescript [specscript]
|
|
6
|
+
* RetryAwsErrors(func function, context object) -> retriesAwsErrors function
|
|
7
|
+
* ```
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
const RetryAwsErrors = function (func, context, name) {
|
|
11
|
+
return function retriesAwsErrors(...args) {
|
|
12
|
+
return func.apply(context, args).promise().catch(error => {
|
|
13
|
+
if (error.retryable) {
|
|
14
|
+
return retriesAwsErrors(...args)
|
|
15
|
+
}
|
|
16
|
+
throw error
|
|
17
|
+
})
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
module.exports = RetryAwsErrors
|