Skip to content

Commit

Permalink
Added requestTimeout and maxRetries support
Browse files Browse the repository at this point in the history
  • Loading branch information
Nataniel López committed Dec 6, 2019
1 parent 65bd788 commit cfdec88
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Added
- `requestTimeout` and `maxRetries` support in config

## [1.2.1] - 2019-12-03
### Fixed
- `distinct` will return all posible results instead 10
Expand Down
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ Constructs the Elasticsearch driver instance, connected with the `config [Object
- limit `[Number]`: Default limit for getting operations.
- prefix `[String]`: Prefix for table name, if it's setted, any operation will use the model table name with the prefix. Ex: `table.prefix`.
- awsCredentials `[Boolean]`: Set to `true` if you need to use AWS credentials ENV variables for the host connection/authentication.
- requestTimeout `[Number]`: The max timeout for operation in miliseconds, default 4000 (4s).
- maxRetries `[Number]`: The max retries for operation, default 3.

**Config usage:**
```js
Expand All @@ -34,7 +36,9 @@ Constructs the Elasticsearch driver instance, connected with the `config [Object
password: 'password', // Default empty
limit: 10, // Default 500
prefix: 'products', // Default empty
awsCredentials: true // Default false
awsCredentials: true, // Default false
requestTimeout: 2000, // Default 4000
maxRetries: 1 // Default 3
}
```

Expand Down
4 changes: 3 additions & 1 deletion lib/config-validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ const ELASTICSEARCH_CONFIG_STRUCT = {
password: 'string',
limit: 'number',
prefix: 'string',
awsCredentials: 'boolean'
awsCredentials: 'boolean',
requestTimeout: 'number',
maxRetries: 'number'
};

/**
Expand Down
8 changes: 7 additions & 1 deletion lib/elasticsearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ const ElasticSearchError = require('./elasticsearch-error');
const ConfigValidator = require('./config-validator');

const DEFAULT_LIMIT = 500;
const DEFAULT_TIMEOUT = 4000;
const DEFAULT_RETRIES = 3;
const ES_DEFAULT_HOST = 'http://localhost';
const ES_MAX_BUCKET_SIZE = 2147483647;

Expand All @@ -41,7 +43,9 @@ class ElasticSearch {
password: config.password || '',
limit: config.limit || DEFAULT_LIMIT,
prefix: config.prefix || '',
awsCredentials: config.awsCredentials || false
awsCredentials: config.awsCredentials || false,
requestTimeout: config.requestTimeout || DEFAULT_TIMEOUT,
maxRetries: config.maxRetries || DEFAULT_RETRIES
};

try {
Expand Down Expand Up @@ -93,6 +97,8 @@ class ElasticSearch {
try {
this._client = new elasticsearch.Client({
host: `${this.config.protocol}${this._userPrefix}${this.config.host}${this.config.port ? `:${this.config.port}` : ''}`,
requestTimeout: this.config.requestTimeout,
maxRetries: this.config.maxRetries,
...this._awsCredentials
});
} catch(err) {
Expand Down

0 comments on commit cfdec88

Please sign in to comment.