Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add retry to putRecord(s) #109

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 61 additions & 17 deletions lib/kinesis-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,25 +199,69 @@ class KinesisClient {
}

putRecord(...args) {
return internal(this)
.client.putRecord(...args)
.promise()
.catch(err => {
const error = new Error(err.message);
error.code = err.code;
throw error;
});
const { client, retryOpts } = internal(this);
return retry(
bail =>
client
.putRecord(...args)
.promise()
.catch(err => {
if (err.code !== 'ProvisionedThroughputExceededException') bail(err);
else throw err;
}),
retryOpts
).catch(err => {
const error = new Error(err.message);
error.code = err.code;
throw error;
});
}

putRecords(...args) {
return internal(this)
.client.putRecords(...args)
.promise()
.catch(err => {
const error = new Error(err.message);
error.code = err.code;
throw error;
});
async putRecords(params) {
const { client, retryOpts } = internal(this);
const { Records, ...opts } = params;

let records = Records;
let results = [];
let failedRecordCount = 0;

return retry(
bail =>
client
.putRecords({ ...opts, Records: records })
.promise()
.then(payload => {
({ FailedRecordCount: failedRecordCount, Records: results } = payload);
if (failedRecordCount === 0) return;

let code;
let message;

records = records.filter((record, i) => {
const { ErrorCode, ErrorMessage } = results[i];
if (ErrorCode && ErrorCode !== 'ProvisionedThroughputExceededException') {
code = ErrorCode;
message = ErrorMessage;
return false;
}
if (ErrorCode && !code) code = code || ErrorCode;
if (ErrorMessage && !message) message = ErrorMessage;
return ErrorCode;
});

const errObj = { message, code, statusCode: null, requestId: null };
throw errObj;
})
.catch(err => {
if (err.code !== 'ProvisionedThroughputExceededException') bail(err);
else throw err;
}),
retryOpts
).catch(err => {
const error = new Error(err.message);
error.code = err.code;
throw error;
});
}
}

Expand Down