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 support using the AWS SDK internal retry logic #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
13 changes: 13 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,19 @@ db.connect({
})
```

As an alternative, the internal retry behaviour of the AWS SDK can also be used by setting [`maxRetries`](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/Config.html#maxRetries-property) and [`retryDelayOptions`](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/Config.html#retryDelayOptions-property).

```js
db.connect({
maxRetries: 3,
retryDelayOptions: { base: 300 }
})
```

As this setting is global, it cannot be overridden at the method level.

Note that it may not advisable to use both `dynongo`'s retry logic and enable the AWS SDK retry behaviour at the same time. If both are enabled, the AWS SDK retry behaviour will trigger first and if the failure persists, the built-in logic inside `dynongo` will keep retrying the failing method.

#### DynamoDB Local

It is possible to connect to a [local DynamoDB](http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Tools.DynamoDBLocal.html) database
Expand Down
14 changes: 13 additions & 1 deletion src/lib/dynamodb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ export interface DynamoDBOptions {
sessionToken?: string;
retries?: number | RetryOptions;
httpOptions?: AWS.HTTPOptions;
maxRetries?: number;
retryDelayOptions?: {
base?: number,
customBackoff?(retryCount: number, err: Error): number
};
}

export class DynamoDB {
Expand All @@ -47,7 +52,14 @@ export class DynamoDB {

this._retries = configureRetryOptions(this.options.retries);

AWS.config.update(pick(this.options, ['region', 'accessKeyId', 'secretAccessKey', 'sessionToken']));
AWS.config.update(pick(this.options, [
'region',
'accessKeyId',
'secretAccessKey',
'sessionToken',
'maxRetries',
'retryDelayOptions'
]));

if (this.options.local) {
// Starts dynamodb in local mode
Expand Down
7 changes: 7 additions & 0 deletions src/test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ test('connect options', t => {
});
});

test('connect with native retry options', t => {
db.connect({maxRetries: 5, retryDelayOptions: {base: 500}});

t.is(db.raw?.config.maxRetries, 5);
t.deepEqual(db.raw?.config.retryDelayOptions, {base: 500});
});

test('connect locally', t => {
db.connect({local: true});
t.is((db.raw!).endpoint.href, 'http://localhost:8000/');
Expand Down