@autofleet/network 1.4.8 → 1.4.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.js +22 -5
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -42,12 +42,27 @@ const defaultSettings = {
42
42
 
43
43
  const createRequestString = (request) => `[${(request.method || '').toUpperCase()}] ${request.baseURL}${request.url}`;
44
44
 
45
+ /*
46
+ The default free socket keepalive timeout, in milliseconds.
47
+ Setting to 5000 (5 seconds) as default because out grace period is 10 seconds
48
+ and we want to make sure we don't have any socket issues.
49
+ See https://www.npmjs.com/package/agentkeepalive
50
+
51
+ There is also autoscaling reason for that, if we dont timeout at some point,
52
+ new pods that were auto scaled will not be used.
53
+ */
54
+ const FREE_SOCKET_TIMEOUT = process.env.FREE_SOCKET_TIMEOUT || 5000;
55
+
45
56
  module.exports = class Network {
46
57
  constructor(settings = {}) {
47
58
  this.settings = merge(defaultSettings, settings);
48
59
  if (this.settings.keepAlive) {
49
- const keepaliveAgent = new Agent();
50
- const keepaliveHttpsAgent = new HttpsAgent();
60
+ const keepaliveAgent = new Agent({
61
+ freeSocketTimeout: FREE_SOCKET_TIMEOUT,
62
+ });
63
+ const keepaliveHttpsAgent = new HttpsAgent({
64
+ freeSocketTimeout: FREE_SOCKET_TIMEOUT,
65
+ });
51
66
  this.settings.httpAgent = keepaliveAgent;
52
67
  this.settings.httpsAgent = keepaliveHttpsAgent;
53
68
  delete this.settings.keepAlive;
@@ -96,11 +111,13 @@ module.exports = class Network {
96
111
  return response;
97
112
  }, (error) => {
98
113
  if(error.request && error.request._currentRequest &&
99
- error.request._currentRequest.reusedSocket && error.code === 'ECONNRESET') {
114
+ error.request._currentRequest.reusedSocket &&
115
+ ['ECONNRESET', 'EPIPE'].includes(error.code)
116
+ ) {
100
117
  // See https://www.npmjs.com/package/agentkeepalive
101
118
  // Support req.reusedSocket
102
119
  // https://code-examples.net/en/q/28a8069
103
- logger.warn('ECONNRESET issue, will retry', {
120
+ logger.warn(`${error.code} issue, will retry`, {
104
121
  req: error.request._currentRequest._currentUrl,
105
122
  // method: error.request._currentRequest._options.method,
106
123
  })
@@ -164,7 +181,7 @@ module.exports = class Network {
164
181
 
165
182
  async getAllPagesFromQueryEndpoint(url, options = {}) {
166
183
  let currentResult = [];
167
-
184
+
168
185
  let moreResultsToLoad = true;
169
186
  let page = 1;
170
187
  while(moreResultsToLoad && page < 100) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autofleet/network",
3
- "version": "1.4.8",
3
+ "version": "1.4.10",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {