Skip to content

Commit

Permalink
feat: support keepAlive: true option
Browse files Browse the repository at this point in the history
Just setting `forever: true` as a pass-thru to request (a) didn't work
because we were forcing `Connection: close` and (b) isn't
forward-compatible with Gofer 3.x
  • Loading branch information
dbushong committed Jun 6, 2018
1 parent fd729ed commit 2e50961
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
2 changes: 2 additions & 0 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,8 @@ In addition to the options mentioned in the [request docs](https://github.com/mi
* `searchDomain`: Inspired by the `search` setting in `/etc/resolv.conf`.
Append this to any hostname that doesn't already end in a ".".
E.g. `my-hostname` turns into `my-hostname.<searchDomain>.` but `my.fully.qualified.name.` won't be touched.
* `keepAlive`: If set to `true`, enables the request `forever` option and does
not send the `Connection: close` header

In addition the following options are added that are useful for instrumentation but do not affect the actual HTTP request:

Expand Down
9 changes: 6 additions & 3 deletions lib/hub.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ module.exports = Hub = function() {
options.headers = {};
}
options.method = options.method != null ? options.method.toUpperCase() : 'GET';
hubHeaders = generateHeaders(options.requestId, fetchId);
options.forever = options.keepAlive;
hubHeaders = generateHeaders(options.requestId, fetchId, options.keepAlive);
extend(options.headers, hubHeaders);
options.headers = mapValues(options.headers, removeInvalidHeaderChars);
logPendingRequests(http.globalAgent);
Expand Down Expand Up @@ -321,12 +322,14 @@ generateUUID = function() {
return uuid.v1().replace(/-/g, '');
};

generateHeaders = function(requestId, fetchId) {
generateHeaders = function(requestId, fetchId, keepAlive) {
var headers;
headers = {
'Connection': 'close',
'X-Fetch-ID': fetchId
};
if (!keepAlive) {
headers.Connection = 'close';
}
if (requestId != null) {
headers['X-Request-ID'] = requestId;
}
Expand Down
10 changes: 5 additions & 5 deletions src/hub.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ module.exports = Hub = ->
options.method.toUpperCase()
else
'GET'
hubHeaders = generateHeaders options.requestId, fetchId
options.forever = options.keepAlive
hubHeaders = generateHeaders options.requestId, fetchId, options.keepAlive
extend options.headers, hubHeaders
options.headers = mapValues options.headers, removeInvalidHeaderChars

Expand Down Expand Up @@ -295,10 +296,9 @@ module.exports = Hub = ->
generateUUID = ->
uuid.v1().replace /-/g, ''

generateHeaders = (requestId, fetchId) ->
headers =
'Connection': 'close'
'X-Fetch-ID': fetchId
generateHeaders = (requestId, fetchId, keepAlive) ->
headers = 'X-Fetch-ID': fetchId
headers.Connection = 'close' unless keepAlive

headers['X-Request-ID'] = requestId if requestId?
headers
Expand Down

0 comments on commit 2e50961

Please sign in to comment.