set 60s timeout on errors; #99

This commit is contained in:
jomo 2015-02-19 01:55:35 +01:00
parent dc33912ec7
commit 0e46019d40
4 changed files with 72 additions and 41 deletions

View File

@ -92,11 +92,14 @@ exp.info = function(callback) {
}); });
}; };
// sets the timestamp for +userId+ and its face file's date to now // sets the timestamp for +userId+ and its face file's (+hash+) date to the current time
// if +temp+ is true, the timestamp is set so that the record will be outdated after 60 seconds
// these 60 seconds match the duration of Mojang's rate limit ban
// +callback+ contains error // +callback+ contains error
exp.update_timestamp = function(rid, userId, hash, callback) { exp.update_timestamp = function(rid, userId, hash, temp, callback) {
logging.log(rid + "cache: updating timestamp"); logging.log(rid + "cache: updating timestamp");
var time = new Date().getTime(); sub = temp ? (config.local_cache_time - 60) : 0;
var time = new Date().getTime() - sub;
// store userId in lower case if not null // store userId in lower case if not null
userId = userId && userId.toLowerCase(); userId = userId && userId.toLowerCase();
redis.hmset(userId, "t", time, function(err) { redis.hmset(userId, "t", time, function(err) {
@ -105,20 +108,32 @@ exp.update_timestamp = function(rid, userId, hash, callback) {
update_file_date(rid, hash); update_file_date(rid, hash);
}; };
// create the key +userId+, store +skin_hash+ hash, +cape_hash+ hash and time // create the key +userId+, store +skin_hash+, +cape_hash+ and time
// if either +skin_hash+ or +cape_hash+ are undefined, they will not be stored
// this feature can be used to write both cape and skin at separate times
// +callback+ contans error // +callback+ contans error
exp.save_hash = function(rid, userId, skin_hash, cape_hash, callback) { exp.save_hash = function(rid, userId, skin_hash, cape_hash, callback) {
logging.log(rid + "cache: saving hash"); logging.log(rid + "cache: saving hash");
logging.log(rid + "skin:" + skin_hash + " cape:" + cape_hash); logging.log(rid + "skin:" + skin_hash + " cape:" + cape_hash);
var time = new Date().getTime(); var time = new Date().getTime();
// store shorter null byte instead of "null" // store shorter null byte instead of "null"
skin_hash = skin_hash || "."; skin_hash = (skin_hash === null ? "." : skin_hash);
cape_hash = cape_hash || "."; cape_hash = (cape_hash === null ? "." : cape_hash);
// store userId in lower case if not null // store userId in lower case if not null
userId = userId && userId.toLowerCase(); userId = userId && userId.toLowerCase();
redis.hmset(userId, "s", skin_hash, "c", cape_hash, "t", time, function(err){ if (skin_hash === undefined) {
callback(err); redis.hmset(userId, "c", cape_hash, "t", time, function(err){
}); callback(err);
});
} else if (cape_hash === undefined) {
redis.hmset(userId, "s", skin_hash, "t", time, function(err){
callback(err);
});
} else {
redis.hmset(userId, "s", skin_hash, "c", cape_hash, "t", time, function(err){
callback(err);
});
}
}; };
// removes the hash for +userId+ from the cache // removes the hash for +userId+ from the cache
@ -129,7 +144,8 @@ exp.remove_hash = function(rid, userId) {
// get a details object for +userId+ // get a details object for +userId+
// {skin: "0123456789abcdef", cape: "gs1gds1g5d1g5ds1", time: 1414881524512} // {skin: "0123456789abcdef", cape: "gs1gds1g5d1g5ds1", time: 1414881524512}
// null when userId unkown // +callbacl+ contains error, details
// details is null when userId not cached
exp.get_details = function(userId, callback) { exp.get_details = function(userId, callback) {
// get userId in lower case if not null // get userId in lower case if not null
userId = userId && userId.toLowerCase(); userId = userId && userId.toLowerCase();
@ -137,8 +153,8 @@ exp.get_details = function(userId, callback) {
var details = null; var details = null;
if (data) { if (data) {
details = { details = {
skin: (data.s === "." ? null : data.s), skin: (!data.s || data.s === ".") ? null : data.s,
cape: (data.c === "." ? null : data.c), cape: (!data.c || data.c === ".") ? null : data.c,
time: Number(data.t) time: Number(data.t)
}; };
} }

View File

@ -16,11 +16,11 @@ function get_hash(url) {
} }
function store_skin(rid, userId, profile, details, callback) { function store_skin(rid, userId, profile, details, callback) {
networking.get_skin_url(rid, userId, profile, function(url) { networking.get_skin_url(rid, userId, profile, function(err, url) {
if (url) { if (!err && url) {
var skin_hash = get_hash(url); var skin_hash = get_hash(url);
if (details && details.skin === skin_hash) { if (details && details.skin === skin_hash) {
cache.update_timestamp(rid, userId, skin_hash, function(err) { cache.update_timestamp(rid, userId, skin_hash, false, function(err) {
callback(err, skin_hash); callback(err, skin_hash);
}); });
} else { } else {
@ -55,17 +55,17 @@ function store_skin(rid, userId, profile, details, callback) {
}); });
} }
} else { } else {
callback(null, null); callback(err, null);
} }
}); });
} }
function store_cape(rid, userId, profile, details, callback) { function store_cape(rid, userId, profile, details, callback) {
networking.get_cape_url(rid, userId, profile, function(url) { networking.get_cape_url(rid, userId, profile, function(err, url) {
if (url) { if (!err && url) {
var cape_hash = get_hash(url); var cape_hash = get_hash(url);
if (details && details.cape === cape_hash) { if (details && details.cape === cape_hash) {
cache.update_timestamp(rid, userId, cape_hash, function(err) { cache.update_timestamp(rid, userId, cape_hash, false, function(err) {
callback(err, cape_hash); callback(err, cape_hash);
}); });
} else { } else {
@ -91,7 +91,7 @@ function store_cape(rid, userId, profile, details, callback) {
}); });
} }
} else { } else {
callback(null, null); callback(err, null);
} }
}); });
} }
@ -155,20 +155,30 @@ function store_images(rid, userId, details, type, callback) {
callback_for(userId, "cape", cache_err, null); callback_for(userId, "cape", cache_err, null);
}); });
} else { } else {
// an error occured, not caching // an error occured, not caching. we can try in 60 seconds
callback_for(userId, type, err, null); callback_for(userId, type, err, null);
} }
} else { } else {
// no error and we have a profile or it's not a uuid // no error and we have a profile (if it's a uuid)
store_skin(rid, userId, profile, details, function(err, skin_hash) { store_skin(rid, userId, profile, details, function(err, skin_hash) {
cache.save_hash(rid, userId, skin_hash, null, function(cache_err) { if (err && !skin_hash) {
callback_for(userId, "skin", (err || cache_err), skin_hash); // an error occured, not caching. we can try in 60 seconds
store_cape(rid, userId, profile, details, function(err, cape_hash) { callback_for(userId, "skin", err, null);
cache.save_hash(rid, userId, skin_hash, cape_hash, function(cache_err) { } else {
callback_for(userId, "cape", (err || cache_err), cape_hash); cache.save_hash(rid, userId, skin_hash, null, function(cache_err) {
}); callback_for(userId, "skin", (err || cache_err), skin_hash);
}); });
}); }
});
store_cape(rid, userId, profile, details, function(err, cape_hash) {
if (err && !cape_hash) {
// an error occured, not caching. we can try in 60 seconds
callback_for(userId, "cape", (err || cache_err), cape_hash);
} else {
cache.save_hash(rid, userId, undefined, cape_hash, function(cache_err) {
callback_for(userId, "cape", (err || cache_err), cape_hash);
});
}
}); });
} }
}); });
@ -215,7 +225,9 @@ exp.get_image_hash = function(rid, userId, type, callback) {
if (err) { if (err) {
// we might have a cached hash although an error occured // we might have a cached hash although an error occured
// (e.g. Mojang servers not reachable, using outdated hash) // (e.g. Mojang servers not reachable, using outdated hash)
callback(err, -1, details && cached_hash); cache.update_timestamp(rid, userId, cached_hash, true, function(err2) {
callback(err2 || err, -1, details && cached_hash);
});
} else { } else {
var status = details && (cached_hash === new_hash) ? 3 : 2; var status = details && (cached_hash === new_hash) ? 3 : 2;
logging.debug(rid + "cached hash: " + (details && cached_hash)); logging.debug(rid + "cached hash: " + (details && cached_hash));

View File

@ -54,15 +54,18 @@ exp.get_from_options = function(rid, url, options, callback) {
encoding: (options.encoding || null), encoding: (options.encoding || null),
}, function(error, response, body) { }, function(error, response, body) {
// log url + code + description // log url + code + description
var code = response.statusCode; var code = response && response.statusCode;
logfunc = code && code < 405 ? logging.log : logging.warn; if (!error) {
logfunc(rid + url + " " + code + " " + http_code[code]); var logfunc = code && code < 405 ? logging.log : logging.warn;
logfunc(rid + url + " " + code + " " + http_code[code]);
}
// 200 or 301 depending on content type // 200 or 301 depending on content type
if (!error && (code === 200 || code === 301)) { if (!error && (code === 200 || code === 301)) {
// response received successfully // response received successfully
callback(body, response, null); callback(body, response, null);
} else if (error) { } else if (error) {
logging.error(error);
callback(body || null, response, error); callback(body || null, response, error);
} else if (code === 404 || code === 204) { } else if (code === 404 || code === 204) {
// page does not exist // page does not exist
@ -126,16 +129,16 @@ exp.get_profile = function(rid, uuid, callback) {
// get the skin URL for +userId+ // get the skin URL for +userId+
// +profile+ is used if +userId+ is a uuid // +profile+ is used if +userId+ is a uuid
exp.get_skin_url = function(rid, userId, profile, callback) { exp.get_skin_url = function(rid, userId, profile, callback) {
get_url(rid, userId, profile, 0, function(url) { get_url(rid, userId, profile, 0, function(err, url) {
callback(url); callback(err, url);
}); });
}; };
// get the cape URL for +userId+ // get the cape URL for +userId+
// +profile+ is used if +userId+ is a uuid // +profile+ is used if +userId+ is a uuid
exp.get_cape_url = function(rid, userId, profile, callback) { exp.get_cape_url = function(rid, userId, profile, callback) {
get_url(rid, userId, profile, 1, function(url) { get_url(rid, userId, profile, 1, function(err, url) {
callback(url); callback(err, url);
}); });
}; };
@ -143,11 +146,11 @@ function get_url(rid, userId, profile, type, callback) {
if (userId.length <= 16) { if (userId.length <= 16) {
//username //username
exp.get_username_url(rid, userId, type, function(err, url) { exp.get_username_url(rid, userId, type, function(err, url) {
callback(url || null); callback(err, url || null);
}); });
} else { } else {
exp.get_uuid_url(profile, type, function(url) { exp.get_uuid_url(profile, type, function(url) {
callback(url || null); callback(null, url || null);
}); });
} }
} }

View File

@ -15,7 +15,7 @@ var request = require("request");
config.http_timeout *= 3; config.http_timeout *= 3;
// no spam // no spam
logging.log = function() {}; //logging.log = function() {};
var uuids = fs.readFileSync("test/uuids.txt").toString().split(/\r?\n/); var uuids = fs.readFileSync("test/uuids.txt").toString().split(/\r?\n/);
var names = fs.readFileSync("test/usernames.txt").toString().split(/\r?\n/); var names = fs.readFileSync("test/usernames.txt").toString().split(/\r?\n/);
@ -166,7 +166,7 @@ describe("Crafatar", function() {
}); });
it("should ignore file updates on invalid files", function(done) { it("should ignore file updates on invalid files", function(done) {
assert.doesNotThrow(function() { assert.doesNotThrow(function() {
cache.update_timestamp(rid, "0123456789abcdef0123456789abcdef", "invalid-file.png", function(err) { cache.update_timestamp(rid, "0123456789abcdef0123456789abcdef", "invalid-file.png", false, function(err) {
done(); done();
}); });
}); });