mirror of
https://github.com/azures04/crafatar.git
synced 2026-03-22 07:51:17 +01:00
rewrite request queue, fixes #118
This commit is contained in:
parent
189698bed9
commit
01049cb34d
@ -107,39 +107,38 @@ function store_cape(rid, userId, profile, cache_details, callback) {
|
|||||||
|
|
||||||
// used by store_images to queue simultaneous requests for identical userId
|
// used by store_images to queue simultaneous requests for identical userId
|
||||||
// the first request has to be completed until all others are continued
|
// the first request has to be completed until all others are continued
|
||||||
var currently_running = [];
|
// otherwise we risk running into Mojang's rate limit and deleting the cached skin
|
||||||
// calls back all queued requests that match userId and type
|
var requests = {
|
||||||
function callback_for(userId, type, err, hash) {
|
skin: {},
|
||||||
var req_count = 0;
|
cape: {}
|
||||||
for (var i = 0; i < currently_running.length; i++) {
|
};
|
||||||
var current = currently_running[i];
|
|
||||||
if (current.userid === userId && current.type === type) {
|
function push_request(userId, type, fun) {
|
||||||
req_count++;
|
if (!requests[type][userId]) {
|
||||||
if (req_count !== 1) {
|
requests[type][userId] = [];
|
||||||
// otherwise this would show up on single/first requests, too
|
|
||||||
logging.debug(current.rid, "queued", type + " request continued");
|
|
||||||
}
|
|
||||||
currently_running.splice(i, 1); // remove from array
|
|
||||||
current.callback(err, hash);
|
|
||||||
i--;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (req_count > 1) {
|
|
||||||
logging.debug(req_count + " simultaneous requests for", userId);
|
|
||||||
}
|
}
|
||||||
|
requests[type][userId].push(fun);
|
||||||
}
|
}
|
||||||
|
|
||||||
// returns true if any object in +arr+ has a +property+ that matches +value+
|
// calls back all queued requests that match userId and type
|
||||||
//
|
function resume(userId, type, err, hash) {
|
||||||
// deep_property_check([{foo: "bar"}, {foo: "baz"}], "foo", "baz");
|
var callbacks = requests[type][userId];
|
||||||
//
|
if (callbacks) {
|
||||||
function deep_property_check(arr, property, value) {
|
if (callbacks.length > 1) {
|
||||||
for (var i = 0; i < arr.length; i++) {
|
logging.debug(callbacks.length, "simultaneous requests for", userId);
|
||||||
if (arr[i][property] === value) {
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for (var i = 0; i < callbacks.length; i++) {
|
||||||
|
// continue the request
|
||||||
|
callbacks[i](err, hash);
|
||||||
|
// remove from array
|
||||||
|
callbacks.splice(i, 1);
|
||||||
|
i--;
|
||||||
|
}
|
||||||
|
|
||||||
|
// it's still an empty array
|
||||||
|
delete requests[type][userId];
|
||||||
}
|
}
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// downloads the images for +userId+ while checking the cache
|
// downloads the images for +userId+ while checking the cache
|
||||||
@ -148,14 +147,13 @@ function deep_property_check(arr, property, value) {
|
|||||||
// callback: error, image hash
|
// callback: error, image hash
|
||||||
function store_images(rid, userId, cache_details, type, callback) {
|
function store_images(rid, userId, cache_details, type, callback) {
|
||||||
var is_uuid = userId.length > 16;
|
var is_uuid = userId.length > 16;
|
||||||
var new_hash = {
|
if (requests[type][userId]) {
|
||||||
rid: rid,
|
logging.log(rid, "adding to request queue");
|
||||||
userid: userId,
|
push_request(userId, type, callback);
|
||||||
type: type,
|
} else {
|
||||||
callback: callback
|
// add request to the queue
|
||||||
};
|
push_request(userId, type, callback);
|
||||||
if (!deep_property_check(currently_running, "userid", userId)) {
|
|
||||||
currently_running.push(new_hash);
|
|
||||||
networking.get_profile(rid, (is_uuid ? userId : null), function(err, profile) {
|
networking.get_profile(rid, (is_uuid ? userId : null), function(err, profile) {
|
||||||
if (err || (is_uuid && !profile)) {
|
if (err || (is_uuid && !profile)) {
|
||||||
// error or uuid without profile
|
// error or uuid without profile
|
||||||
@ -163,40 +161,37 @@ function store_images(rid, userId, cache_details, type, callback) {
|
|||||||
// no error, but uuid without profile
|
// no error, but uuid without profile
|
||||||
cache.save_hash(rid, userId, null, null, function(cache_err) {
|
cache.save_hash(rid, userId, null, null, function(cache_err) {
|
||||||
// we have no profile, so we have neither skin nor cape
|
// we have no profile, so we have neither skin nor cape
|
||||||
callback_for(userId, "skin", cache_err, null);
|
resume(userId, "skin", cache_err, null);
|
||||||
callback_for(userId, "cape", cache_err, null);
|
resume(userId, "cape", cache_err, null);
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
// an error occured, not caching. we can try in 60 seconds
|
// an error occured, not caching. we can try in 60 seconds
|
||||||
callback_for(userId, type, err, null);
|
resume(userId, type, err, null);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// no error and we have a profile (if it's a uuid)
|
// no error and we have a profile (if it's a uuid)
|
||||||
store_skin(rid, userId, profile, cache_details, function(store_err, skin_hash) {
|
store_skin(rid, userId, profile, cache_details, function(store_err, skin_hash) {
|
||||||
if (store_err && !skin_hash) {
|
if (store_err && !skin_hash) {
|
||||||
// an error occured, not caching. we can try in 60 seconds
|
// an error occured, not caching. we can try in 60 seconds
|
||||||
callback_for(userId, "skin", store_err, null);
|
resume(userId, "skin", store_err, null);
|
||||||
} else {
|
} else {
|
||||||
cache.save_hash(rid, userId, skin_hash, undefined, function(cache_err) {
|
cache.save_hash(rid, userId, skin_hash, undefined, function(cache_err) {
|
||||||
callback_for(userId, "skin", (store_err || cache_err), skin_hash);
|
resume(userId, "skin", (store_err || cache_err), skin_hash);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
store_cape(rid, userId, profile, cache_details, function(store_err, cape_hash) {
|
store_cape(rid, userId, profile, cache_details, function(store_err, cape_hash) {
|
||||||
if (store_err && !cape_hash) {
|
if (store_err && !cape_hash) {
|
||||||
// an error occured, not caching. we can try in 60 seconds
|
// an error occured, not caching. we can try in 60 seconds
|
||||||
callback_for(userId, "cape", (store_err), cape_hash);
|
resume(userId, "cape", (store_err), cape_hash);
|
||||||
} else {
|
} else {
|
||||||
cache.save_hash(rid, userId, undefined, cape_hash, function(cache_err) {
|
cache.save_hash(rid, userId, undefined, cape_hash, function(cache_err) {
|
||||||
callback_for(userId, "cape", (store_err || cache_err), cape_hash);
|
resume(userId, "cape", (store_err || cache_err), cape_hash);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} else {
|
|
||||||
logging.log(rid, "ID already being processed, adding to queue");
|
|
||||||
currently_running.push(new_hash);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -74,7 +74,7 @@ exp.get_from_options = function(rid, url, options, callback) {
|
|||||||
// log url + code + description
|
// log url + code + description
|
||||||
var code = response && response.statusCode;
|
var code = response && response.statusCode;
|
||||||
if (error) {
|
if (error) {
|
||||||
logging.error(url, error);
|
logging.error(rid, url, error);
|
||||||
} else {
|
} else {
|
||||||
var logfunc = code && code < 405 ? logging.log : logging.warn;
|
var logfunc = code && code < 405 ? logging.log : logging.warn;
|
||||||
logfunc(rid, url, code, http_code[code]);
|
logfunc(rid, url, code, http_code[code]);
|
||||||
|
|||||||
@ -46,8 +46,8 @@ module.exports = function(request, response, result) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
if (result.err) {
|
if (result.err) {
|
||||||
logging.error(result.err);
|
logging.error(request.id, result.err);
|
||||||
logging.error(result.err.stack);
|
logging.error(request.id, result.err.stack);
|
||||||
result.status = -1;
|
result.status = -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user