-
Notifications
You must be signed in to change notification settings - Fork 132
Handle store backend errors #325
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
Merged
Merged
Changes from 3 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -379,19 +379,23 @@ class ConfigurableProxy extends EventEmitter { | |
} | ||
|
||
targetForReq(req) { | ||
var metricsTimerEnd = this.metrics.findTargetForReqSummary.startTimer(); | ||
// return proxy target for a given url path | ||
var basePath = this.hostRouting ? "/" + parseHost(req) : ""; | ||
var path = basePath + decodeURIComponent(URL.parse(req.url).pathname); | ||
|
||
return this._routes.getTarget(path).then(function (route) { | ||
metricsTimerEnd(); | ||
if (route) { | ||
return { | ||
prefix: route.prefix, | ||
target: route.data.target, | ||
}; | ||
} | ||
return new Promise((resolve, reject) => { | ||
var metricsTimerEnd = this.metrics.findTargetForReqSummary.startTimer(); | ||
// return proxy target for a given url path | ||
var basePath = this.hostRouting ? "/" + parseHost(req) : ""; | ||
var path = basePath + decodeURIComponent(URL.parse(req.url).pathname); | ||
|
||
resolve( | ||
this._routes.getTarget(path).then(function (route) { | ||
metricsTimerEnd(); | ||
if (route) { | ||
return { | ||
prefix: route.prefix, | ||
target: route.data.target, | ||
}; | ||
} | ||
}) | ||
); | ||
}); | ||
} | ||
|
||
|
@@ -528,72 +532,77 @@ class ConfigurableProxy extends EventEmitter { | |
var args = Array.prototype.slice.call(arguments, 1); | ||
|
||
// get the proxy target | ||
return this.targetForReq(req).then((match) => { | ||
if (!match) { | ||
that.handleProxyError(404, kind, req, res); | ||
return; | ||
} | ||
|
||
if (kind === "web") { | ||
that.emit("proxyRequest", req, res); | ||
} else { | ||
that.emit("proxyRequestWs", req, res, args[2]); | ||
} | ||
var prefix = match.prefix; | ||
var target = match.target; | ||
that.log.debug("PROXY %s %s to %s", kind.toUpperCase(), _logUrl(req.url), target); | ||
if (!that.includePrefix) { | ||
req.url = req.url.slice(prefix.length); | ||
} | ||
return this.targetForReq(req) | ||
.then((match) => { | ||
if (!match) { | ||
that.handleProxyError(404, kind, req, res); | ||
return; | ||
} | ||
|
||
target = URL.parse(target); | ||
if (that.options.clientSsl) { | ||
target.key = that.options.clientSsl.key; | ||
target.cert = that.options.clientSsl.cert; | ||
target.ca = that.options.clientSsl.ca; | ||
} | ||
if (kind === "web") { | ||
that.emit("proxyRequest", req, res); | ||
} else { | ||
that.emit("proxyRequestWs", req, res, args[2]); | ||
} | ||
var prefix = match.prefix; | ||
var target = match.target; | ||
that.log.debug("PROXY %s %s to %s", kind.toUpperCase(), _logUrl(req.url), target); | ||
if (!that.includePrefix) { | ||
req.url = req.url.slice(prefix.length); | ||
} | ||
|
||
// add config argument | ||
args.push({ target: target }); | ||
target = URL.parse(target); | ||
if (that.options.clientSsl) { | ||
target.key = that.options.clientSsl.key; | ||
target.cert = that.options.clientSsl.cert; | ||
target.ca = that.options.clientSsl.ca; | ||
} | ||
|
||
// add error handling | ||
args.push(function (e) { | ||
that.handleProxyError(503, kind, req, res, e); | ||
}); | ||
// add config argument | ||
args.push({ target: target }); | ||
|
||
// dispatch the actual method, either: | ||
// - proxy.web(req, res, options, errorHandler) | ||
// - proxy.ws(req, socket, head, options, errorHandler) | ||
that.proxy[kind].apply(that.proxy, args); | ||
// add error handling | ||
args.push(function (e) { | ||
that.handleProxyError(503, kind, req, res, e); | ||
}); | ||
|
||
// update timestamp on any request/reply data as well (this includes websocket data) | ||
req.on("data", function () { | ||
that.updateLastActivity(prefix); | ||
}); | ||
// dispatch the actual method, either: | ||
// - proxy.web(req, res, options, errorHandler) | ||
// - proxy.ws(req, socket, head, options, errorHandler) | ||
that.proxy[kind].apply(that.proxy, args); | ||
|
||
res.on("data", function () { | ||
that.updateLastActivity(prefix); | ||
}); | ||
// update timestamp on any request/reply data as well (this includes websocket data) | ||
req.on("data", function () { | ||
that.updateLastActivity(prefix); | ||
}); | ||
|
||
if (kind === "web") { | ||
// update last activity on completion of the request | ||
// only consider 'successful' requests activity | ||
// A flood of invalid requests such as 404s or 403s | ||
// or 503s because the endpoint is down | ||
// shouldn't make it look like the endpoint is 'active' | ||
|
||
// we no longer register activity at the *start* of the request | ||
// because at that point we don't know if the endpoint is even available | ||
res.on("finish", function () { | ||
// (don't count redirects...but should we?) | ||
if (res.statusCode < 300) { | ||
that.updateLastActivity(prefix); | ||
} else { | ||
that.log.debug("Not recording activity for status %s on %s", res.statusCode, prefix); | ||
} | ||
res.on("data", function () { | ||
that.updateLastActivity(prefix); | ||
}); | ||
} | ||
}); | ||
|
||
if (kind === "web") { | ||
// update last activity on completion of the request | ||
// only consider 'successful' requests activity | ||
// A flood of invalid requests such as 404s or 403s | ||
// or 503s because the endpoint is down | ||
// shouldn't make it look like the endpoint is 'active' | ||
|
||
// we no longer register activity at the *start* of the request | ||
// because at that point we don't know if the endpoint is even available | ||
res.on("finish", function () { | ||
// (don't count redirects...but should we?) | ||
if (res.statusCode < 300) { | ||
that.updateLastActivity(prefix); | ||
} else { | ||
that.log.debug("Not recording activity for status %s on %s", res.statusCode, prefix); | ||
} | ||
}); | ||
} | ||
}) | ||
.catch(function (e) { | ||
if (res.finished) throw e; | ||
that.handleProxyError(500, kind, req, res, e); | ||
}); | ||
Comment on lines
+596
to
+599
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These lines are the only changes in this function: adding the catch. prettier chooses a different indentation when there's both a |
||
} | ||
|
||
handleProxyWs(req, socket, head) { | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Noting for review:
wrapping this in a promise ensures that targetForReq always returns a Promise, even if
routes.getTarget
raises directly (as opposed to returning a failing promise).