-
-
Notifications
You must be signed in to change notification settings - Fork 169
Expand file tree
/
Copy pathplace.js
More file actions
105 lines (88 loc) · 2.97 KB
/
Copy pathplace.js
File metadata and controls
105 lines (88 loc) · 2.97 KB
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
const _ = require('lodash');
const retry = require('retry');
const mgetService = require('../service/mget');
const logger = require('pelias-logger').get('api');
const Debug = require('../helper/debug');
const debugLog = new Debug('controller:place');
// @todo: remove this and replace with the predicate
function requestHasErrors(request) {
return _.get(request, 'errors', []).length > 0;
}
function isRequestTimeout(err) {
return _.get(err, 'status') === 408;
}
function setup( apiConfig, esclient ){
function controller( req, res, next ){
// do not run controller when a request validation error has occurred.
if (requestHasErrors(req)){
return next();
}
// options for retry
// maxRetries is from the API config with default of 3
// factor of 1 means that each retry attempt will esclient requestTimeout
const operationOptions = {
retries: _.get(apiConfig, 'requestRetries', 3),
factor: 1,
minTimeout: _.get(esclient, 'transport.requestTimeout')
};
// setup a new operation
const operation = retry.operation(operationOptions);
//generate Elasticsearch mget entries based on GID
const cmd = req.clean.ids.map((id) => {
return {
_index: apiConfig.indexName,
_id: `${id.source}:${id.layer}:${id.id}`
};
});
logger.debug('[ES req]', cmd);
operation.attempt((currentAttempt) => {
const start = Date.now();
mgetService(esclient, cmd, (err, docs, data) => {
const message = {
controller: 'place',
queryType: 'place',
result_count: _.get(data, 'docs.length'),
response_time: _.get(data, 'response_time', undefined),
params: req.clean,
retries: currentAttempt - 1
};
logger.info('elasticsearch', message);
// returns true if the operation should be attempted again
// (handles bookkeeping of maxRetries)
// only consider for status 408 (request timeout)
if (isRequestTimeout(err) && operation.retry(err)) {
debugLog.push(req, {
warning: `request timed out on attempt ${currentAttempt}, retrying`,
duration: Date.now() - start
});
return;
}
// if execution has gotten this far then one of three things happened:
// - the request didn't time out
// - maxRetries has been hit so we're giving up
// - another error occurred
// in either case, handle the error or results
// error handler
if( err ){
if (_.isObject(err) && err.message) {
req.errors.push( err.message );
} else {
req.errors.push( err );
}
}
// set response data
else {
res.data = docs;
}
logger.debug('[ES response]', docs);
next();
});
debugLog.push(req, {
ES_req: cmd,
duration: Date.now() - start
});
});
}
return controller;
}
module.exports = setup;