Skip to content
This repository has been archived by the owner on Nov 20, 2018. It is now read-only.

Implemented caching for sas response if a validity period is provided… #2039

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 54 additions & 13 deletions client/js/azure/get-sas.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,44 @@ qq.azure.GetSas = function(o) {

qq.extend(options, o);

function sasResponseReceived(id, xhr, isError) {
var promise = requestPromises[id];
function sasResponseReceived(params, xhr, isError) {
var promise = requestPromises[params.id];

if (isError) {
promise.failure("Received response code " + xhr.status, xhr);
}
else {
if (xhr.responseText.length) {
promise.success(xhr.responseText);

var responseJson;

try {
responseJson = JSON.parse(xhr.responseText);
} catch (error) {
options.log(qq.format("Validity period not provided for SAS for file ID {}. SAS request will be sent for each chunk. JSON parse error: '{}'", params.id, error));
}

if (responseJson) {
var sasData = {
sasUrl: responseJson.sasUrl,
validFor: responseJson.validFor - 15, // extract 15 seconds from validity time to cater for latency between client and server
requestedAtTimestamp: parseInt((new Date().getTime()) / 1000) // current timestamp (in seconds)
};

localStorage['qqazure_sas_' + params.blobUri] = JSON.stringify(sasData);

promise.success(sasData.sasUrl);
} else {
// fallback; only the sas is returned without expiration time
promise.success(xhr.responseText);
}
}
else {
promise.failure("Empty response.", xhr);
}
}

delete requestPromises[id];
delete requestPromises[params.id];
}

requester = qq.extend(this, new qq.AjaxRequester({
Expand All @@ -59,17 +81,36 @@ qq.azure.GetSas = function(o) {
var requestPromise = new qq.Promise(),
restVerb = options.restRequestVerb;

options.log(qq.format("Submitting GET SAS request for a {} REST request related to file ID {}.", restVerb, id));

requestPromises[id] = requestPromise;

requester.initTransport(id)
.withParams({
bloburi: blobUri,
_method: restVerb
})
.withCacheBuster()
.send();
var cachedSasData = localStorage['qqazure_sas_' + blobUri];
var sasUrl;

if (cachedSasData) {
cachedSasData = JSON.parse(cachedSasData);
var currentTimestamp = parseInt((new Date().getTime()) / 1000);

// check if sas is valid based on timestamps
if (cachedSasData.validFor > (currentTimestamp - cachedSasData.requestedAtTimestamp)) {
sasUrl = cachedSasData.sasUrl;
}
}

if (sasUrl) {
options.log(qq.format("Ignoring GET SAS request for file ID {} because the previous request is still valid.", id));
requestPromise.success(sasUrl);
} else {
options.log(qq.format("Submitting GET SAS request for a {} REST request related to file ID {}.", restVerb, id));
var params = { id: id, blobUri: blobUri };
requester.initTransport(params)
.withParams({
bloburi: blobUri,
_method: restVerb
})
.withCacheBuster()
.send();
}


return requestPromise;
}
Expand Down