From 4b5dc25010c0732a16b42ba0cc6c0326689701e6 Mon Sep 17 00:00:00 2001 From: Zaid Abdulla Date: Tue, 3 Jan 2017 18:58:57 -0800 Subject: [PATCH] Load distribution in refresh provision --- lib/common.coffee | 9 +++++++++ lib/default-provisions.js | 3 ++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/common.coffee b/lib/common.coffee index 3262db4..1fe2fa0 100644 --- a/lib/common.coffee +++ b/lib/common.coffee @@ -301,6 +301,14 @@ escapeRegExp = (str) -> return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&") +# djb2 hashing algorithm adapted from http://www.cse.yorku.ca/~oz/hash.html +djb2 = (str) -> + hash = 5381 + for i in [0...str.length] + hash = ((hash << 5) + hash) ^ str.charCodeAt(i) + return hash + + exports.UNDEFINED_TYPE = UNDEFINED_TYPE exports.NULL_TYPE = NULL_TYPE exports.NUMBER_TYPE = NUMBER_TYPE @@ -315,3 +323,4 @@ exports.parsePath = parsePath exports.addPathMeta = addPathMeta exports.hammingWeight = hammingWeight exports.escapeRegExp = escapeRegExp +exports.djb2 = djb2 diff --git a/lib/default-provisions.js b/lib/default-provisions.js index e2f9ce7..aa1e7a0 100644 --- a/lib/default-provisions.js +++ b/lib/default-provisions.js @@ -29,7 +29,8 @@ function refresh(sessionData, provision, declarations, startRevision, endRevisio path.length = MAX_DEPTH; path.fill("*", l); let every = 1000 * (provision[2] || 1); - let t = Math.trunc(sessionData.timestamp / every) * every; + let offset = Math.abs(common.djb2(sessionData.deviceId)) % 86400000; + let t = Math.trunc((sessionData.timestamp + offset) / every) * every - offset; for (let i = l; i < path.length; ++ i) declarations.push([path.slice(0, i), t, {object: 1, writable: 1, value: t}]);