Skip to content

Commit

Permalink
SNOW-1814745-Fix-aborting-requests-in-HttpClient: Added aborting requ…
Browse files Browse the repository at this point in the history
…est logic. Unified request and request async
  • Loading branch information
sfc-gh-fpawlowski committed Nov 19, 2024
1 parent c79c2fc commit 2f9ddc1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
16 changes: 10 additions & 6 deletions lib/http/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,16 @@ HttpClient.prototype.request = function (options) {

let requestPromise;
const requestOptions = prepareRequestOptions.call(this, options);
const requestController = new AbortController();

let sendRequest = async function sendRequest() {
Logger.getInstance().trace('Request%s - sending.', requestUtil.describeRequestFromOptions(requestOptions));
const timer = new ExecutionTimer().start();
requestPromise = axios.request(requestOptions).then(response => {
requestPromise = axios.request(requestOptions, { signal: requestController }).then(response => {
const httpResponseTime = timer.getDuration();
Logger.getInstance().debug('Request%s - response received after %s milliseconds with status %s.', requestUtil.describeRequestFromOptions(requestOptions), httpResponseTime, response.status);
sanitizeAxiosResponse(response);
parseResponseData(response);
if (Util.isFunction(options.callback)) {
Logger.getInstance().trace('Request%s - calling callback function.', requestUtil.describeRequestFromOptions(requestOptions));
return options.callback(null, normalizeResponse(response), response.data);
Expand Down Expand Up @@ -78,12 +81,13 @@ HttpClient.prototype.request = function (options) {
// methods we're comfortable exposing to the outside world
return {
abort: function () {
if (requestPromise) {
if (requestController) {
Logger.getInstance().trace('Request%s - aborting.', requestUtil.describeRequestFromOptions(requestOptions));
// TODO: This line won't work - promise has no method called abort
requestPromise.abort();
requestController.abort();
Logger.getInstance().debug('Request%s - aborted.', requestUtil.describeRequestFromOptions(requestOptions));
}
}
},
requestPromise: requestPromise
};
};

Expand All @@ -106,7 +110,7 @@ HttpClient.prototype.requestAsync = async function (options) {
Logger.getInstance().debug('Request%s - response received after %s milliseconds with status %s.', requestUtil.describeRequestFromOptions(requestOptions), httpResponseTime, response.status);
parseResponseData(response);
sanitizeAxiosResponse(response);
return response;
return normalizeResponse(response);
} catch (err) {
const httpResponseTime = timer.getDuration();
Logger.getInstance().debug('Request%s - failed after %s milliseconds. Error will be re-raised.', requestUtil.describeRequestFromOptions(options), httpResponseTime);
Expand Down
2 changes: 1 addition & 1 deletion lib/services/sf.js
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,7 @@ function StateAbstract(options) {

if (body.code === GSErrors.code.ID_TOKEN_INVALID && data.authnMethod === 'TOKEN') {
Logger.getInstance().debug('ID Token being used has expired. Reauthenticating');
const key = Util.buildCredentialCacheKey(connectionConfig.host,
const key = Util.buildCredentialCacheKey(connectionConfig.host,
connectionConfig.username, AuthenticationTypes.ID_TOKEN_AUTHENTICATOR);
await GlobalConfig.getCredentialManager().remove(key);
await auth.reauthenticate(requestOptions.json);
Expand Down

0 comments on commit 2f9ddc1

Please sign in to comment.