Support time discretization in scripts

This can be used to ensure parameter refresh times are aligned (as
opposed to using Date.now() - X). It also allows for possible future
optimizations by exploiting the fact that the value of Date.now(X)
remains static for a given amount of time.

The variance value can be used to shift the discretization threshold
points based on the hash of the device ID for load distribution.

The djb2 hashing algorithm has been replaced with MD5 for better
distribution.
This commit is contained in:
Zaid Abdulla committed 2017-11-10 14:34:24 -08:00
1 parent ed873cdd4a
commit 6a9c62b639
6 files changed
+121 -47

No files matched your search

+3 -2
View File
@@ -21,6 +21,7 @@
const config = require("./config");
const common = require("./common");
const device = require("./device");
const scheduling = require("./scheduling");
const MAX_DEPTH = config.get("MAX_DEPTH");
@@ -31,8 +32,8 @@ function refresh(sessionContext, provision, declarations, startRevision, endRevi
path.length = MAX_DEPTH;
path.fill("*", l);
let every = 1000 * (provision[2] || 1);
let offset = Math.abs(common.djb2(sessionContext.deviceId)) % 86400000;
let t = Math.trunc((sessionContext.timestamp + offset) / every) * every - offset;
let offset = scheduling.variance(sessionContext.deviceId, every);
let t = scheduling.interval(sessionContext.timestamp, every, offset);
for (let i = l; i < path.length; ++ i)
declarations.push([path.slice(0, i), t, {object: 1, writable: 1, value: t}]);