-
Notifications
You must be signed in to change notification settings - Fork 2
/
kinesis-reader.js
149 lines (121 loc) · 4.17 KB
/
kinesis-reader.js
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
var from2 = require('from2');
var Promise = require('promise');
var xtend = require('xtend');
module.exports = function (kinesis, streamName, opts) {
var streamState = initStreamState(kinesis, streamState, opts);
var closed = false;
var readable = from2.obj(function (size, next) {
if (closed) return next(null, null);
var promiseChain;
var promiseChain = (streamState.shards)
? getRecords(streamState)
: getShardList(streamState).then(cycleShards).then(getRecords);
promiseChain
.catch(catchCycle(streamState))
.nodeify(next);
});
readable.close = function () {closed = true;};
readable.streamState = streamState;
return readable;
};
function catchCycle(streamState) {
return function (error) {
return new Promise(function (resolve, reject) {
// right now, we'll only handle provisioned throughputs errors. Otherwise, we'll emit an error
if (!error.code || error.code !== 'ProvisionedThroughputExceededException') return reject(error);
return cycleShards(streamState)
.then(function () { resolve([]); })
});
};
}
function cycleShards(streamState) {
return new Promise(function (resolve, reject) {
streamState.currShard++;
if (streamState.currShard >= streamState.shards.length) {
if (!streamState.options.allowLooping) return reject('all shards exhausted');
streamState.currShard = 0;
}
setTimeout(function () {
resolve(streamState);
}, streamState.options.cyclepause);
}).then(getShardIterator);
}
function getRecords(streamState) {
var shard = streamState.shards[streamState.currShard];
var iter = streamState.shardIterators[shard];
return new Promise(function (resolve, reject) {
streamState.kinesis.getRecords({ShardIterator: iter}, function (err, data) {
if (err) return reject(err);
// Update iterator
streamState.shardIterators[shard] = data.NextShardIterator;
setTimeout(function () {
resolve(data.Records);
}, streamState.options.readpause);
});
});
}
function getShardList(streamState, startShard) {
if (!streamState.shards) streamState.shards = [];
var options = {StreamName: streamState.streamName};
if (startShard) options.ExclusiveStartShardId = startShard;
return new Promise(function (resolve, reject) {
streamState.kinesis.describeStream(options, function (err, data) {
if (err) return reject(err);
data.StreamDescription.Shards.forEach(function (s) {
streamState.shards.push(s.ShardId);
});
if (data.StreamDescription.HasMoreShards) {
// ask for more shards, starting from the last one we got
getShardList(streamState, streamState.shards[streamState.shards.length - 1])
.catch(reject)
.then(resolve);
} else {
resolve(streamState);
}
});
});
}
function getShardIterator(streamState) {
var shard = streamState.shards[streamState.currShard];
return new Promise(function (resolve, reject) {
var params = {
StreamName: streamState.streamName,
ShardId: shard,
ShardIteratorType: streamState.options.iteratorType
};
if (params.ShardIteratorType === 'AT_TIMESTAMP') {
params.Timestamp = streamState.options.iteratorTimestamp || new Date();
}
streamState.kinesis.getShardIterator(params, function (e, d) {
if (e) return reject(e);
streamState.shardIterators[shard] = d.ShardIterator;
return resolve(streamState);
});
});
}
function initStreamState(kinesis, streamName, opts) {
if (!options) options = {};
var options = xtend({
readpause: 1000,
cyclepause: 1000,
allowLooping: false,
iteratorType: 'LATEST'
}, opts);
var closed = false;
var streamState = {
streamName: streamName,
shards: null,
currShard: -1,
shardIterators: {},
kinesis: kinesis,
options: options
};
return streamState;
}
module.exports.catchCycle = catchCycle;
module.exports.cycleShards = cycleShards;
module.exports.getShardIterator = getShardIterator;
module.exports.getShardList = getShardList;
module.exports.getRecords = getRecords;
module.exports.cyleShards = cycleShards;
module.exports.initStreamState = initStreamState;