mirror of
https://github.com/azures04/crafatar.git
synced 2026-03-22 07:51:17 +01:00
set 60s timeout on errors; #99
This commit is contained in:
parent
dc33912ec7
commit
0e46019d40
@ -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
|
||||
exp.update_timestamp = function(rid, userId, hash, callback) {
|
||||
exp.update_timestamp = function(rid, userId, hash, temp, callback) {
|
||||
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
|
||||
userId = userId && userId.toLowerCase();
|
||||
redis.hmset(userId, "t", time, function(err) {
|
||||
@ -105,20 +108,32 @@ exp.update_timestamp = function(rid, userId, hash, callback) {
|
||||
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
|
||||
exp.save_hash = function(rid, userId, skin_hash, cape_hash, callback) {
|
||||
logging.log(rid + "cache: saving hash");
|
||||
logging.log(rid + "skin:" + skin_hash + " cape:" + cape_hash);
|
||||
var time = new Date().getTime();
|
||||
// store shorter null byte instead of "null"
|
||||
skin_hash = skin_hash || ".";
|
||||
cape_hash = cape_hash || ".";
|
||||
skin_hash = (skin_hash === null ? "." : skin_hash);
|
||||
cape_hash = (cape_hash === null ? "." : cape_hash);
|
||||
// store userId in lower case if not null
|
||||
userId = userId && userId.toLowerCase();
|
||||
if (skin_hash === undefined) {
|
||||
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
|
||||
@ -129,7 +144,8 @@ exp.remove_hash = function(rid, userId) {
|
||||
|
||||
// get a details object for +userId+
|
||||
// {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) {
|
||||
// get userId in lower case if not null
|
||||
userId = userId && userId.toLowerCase();
|
||||
@ -137,8 +153,8 @@ exp.get_details = function(userId, callback) {
|
||||
var details = null;
|
||||
if (data) {
|
||||
details = {
|
||||
skin: (data.s === "." ? null : data.s),
|
||||
cape: (data.c === "." ? null : data.c),
|
||||
skin: (!data.s || data.s === ".") ? null : data.s,
|
||||
cape: (!data.c || data.c === ".") ? null : data.c,
|
||||
time: Number(data.t)
|
||||
};
|
||||
}
|
||||
|
||||
@ -16,11 +16,11 @@ function get_hash(url) {
|
||||
}
|
||||
|
||||
function store_skin(rid, userId, profile, details, callback) {
|
||||
networking.get_skin_url(rid, userId, profile, function(url) {
|
||||
if (url) {
|
||||
networking.get_skin_url(rid, userId, profile, function(err, url) {
|
||||
if (!err && url) {
|
||||
var skin_hash = get_hash(url);
|
||||
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);
|
||||
});
|
||||
} else {
|
||||
@ -55,17 +55,17 @@ function store_skin(rid, userId, profile, details, callback) {
|
||||
});
|
||||
}
|
||||
} else {
|
||||
callback(null, null);
|
||||
callback(err, null);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function store_cape(rid, userId, profile, details, callback) {
|
||||
networking.get_cape_url(rid, userId, profile, function(url) {
|
||||
if (url) {
|
||||
networking.get_cape_url(rid, userId, profile, function(err, url) {
|
||||
if (!err && url) {
|
||||
var cape_hash = get_hash(url);
|
||||
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);
|
||||
});
|
||||
} else {
|
||||
@ -91,7 +91,7 @@ function store_cape(rid, userId, profile, details, callback) {
|
||||
});
|
||||
}
|
||||
} 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);
|
||||
});
|
||||
} else {
|
||||
// an error occured, not caching
|
||||
// an error occured, not caching. we can try in 60 seconds
|
||||
callback_for(userId, type, err, null);
|
||||
}
|
||||
} 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) {
|
||||
if (err && !skin_hash) {
|
||||
// an error occured, not caching. we can try in 60 seconds
|
||||
callback_for(userId, "skin", err, null);
|
||||
} else {
|
||||
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) {
|
||||
cache.save_hash(rid, userId, skin_hash, cape_hash, function(cache_err) {
|
||||
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) {
|
||||
// we might have a cached hash although an error occured
|
||||
// (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 {
|
||||
var status = details && (cached_hash === new_hash) ? 3 : 2;
|
||||
logging.debug(rid + "cached hash: " + (details && cached_hash));
|
||||
|
||||
@ -54,15 +54,18 @@ exp.get_from_options = function(rid, url, options, callback) {
|
||||
encoding: (options.encoding || null),
|
||||
}, function(error, response, body) {
|
||||
// log url + code + description
|
||||
var code = response.statusCode;
|
||||
logfunc = code && code < 405 ? logging.log : logging.warn;
|
||||
var code = response && response.statusCode;
|
||||
if (!error) {
|
||||
var logfunc = code && code < 405 ? logging.log : logging.warn;
|
||||
logfunc(rid + url + " " + code + " " + http_code[code]);
|
||||
}
|
||||
|
||||
// 200 or 301 depending on content type
|
||||
if (!error && (code === 200 || code === 301)) {
|
||||
// response received successfully
|
||||
callback(body, response, null);
|
||||
} else if (error) {
|
||||
logging.error(error);
|
||||
callback(body || null, response, error);
|
||||
} else if (code === 404 || code === 204) {
|
||||
// page does not exist
|
||||
@ -126,16 +129,16 @@ exp.get_profile = function(rid, uuid, callback) {
|
||||
// get the skin URL for +userId+
|
||||
// +profile+ is used if +userId+ is a uuid
|
||||
exp.get_skin_url = function(rid, userId, profile, callback) {
|
||||
get_url(rid, userId, profile, 0, function(url) {
|
||||
callback(url);
|
||||
get_url(rid, userId, profile, 0, function(err, url) {
|
||||
callback(err, url);
|
||||
});
|
||||
};
|
||||
|
||||
// get the cape URL for +userId+
|
||||
// +profile+ is used if +userId+ is a uuid
|
||||
exp.get_cape_url = function(rid, userId, profile, callback) {
|
||||
get_url(rid, userId, profile, 1, function(url) {
|
||||
callback(url);
|
||||
get_url(rid, userId, profile, 1, function(err, url) {
|
||||
callback(err, url);
|
||||
});
|
||||
};
|
||||
|
||||
@ -143,11 +146,11 @@ function get_url(rid, userId, profile, type, callback) {
|
||||
if (userId.length <= 16) {
|
||||
//username
|
||||
exp.get_username_url(rid, userId, type, function(err, url) {
|
||||
callback(url || null);
|
||||
callback(err, url || null);
|
||||
});
|
||||
} else {
|
||||
exp.get_uuid_url(profile, type, function(url) {
|
||||
callback(url || null);
|
||||
callback(null, url || null);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@ -15,7 +15,7 @@ var request = require("request");
|
||||
config.http_timeout *= 3;
|
||||
|
||||
// no spam
|
||||
logging.log = function() {};
|
||||
//logging.log = function() {};
|
||||
|
||||
var uuids = fs.readFileSync("test/uuids.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) {
|
||||
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();
|
||||
});
|
||||
});
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user