Created
October 10, 2018 08:22
-
-
Save luke/46481e0496b03eec1a60227c4fc8ae8b to your computer and use it in GitHub Desktop.
quick function to help with staggered rollout of features or updates
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// example staggering rollout over 24 hours, timeToStart is date timestamp ms. | |
// if(staggeredRollout(timeToStart,timeToStart+(1000*60*60*24), 24, customerId)){ someNewThing() } | |
function staggeredRollout(startAt, endAt, stages, input, now){ | |
now = now || (new Date()).getTime(); | |
if( now < startAt ) return false; | |
if( now >= endAt ) return true; | |
var hash = strToHashCode(input); | |
var period = (endAt - startAt) / stages; | |
var currentStage = Math.floor( (now - startAt) / period ); | |
var inputStage = (hash % stages); | |
var result = inputStage <= currentStage; | |
console.log("" + currentStage +' : '+ inputStage +' => '+ result) | |
return result; | |
} | |
function strToHashCode(str) { | |
var hash = 0, i, chr; | |
if (str.length === 0) return hash; | |
for (i = 0; i < str.length; i++) { | |
chr = str.charCodeAt(i); | |
hash = ((hash << 5) - hash) + chr; | |
hash |= 0; | |
} | |
return hash | |
} | |
function test(){ | |
var now = (new Date()).getTime(); | |
var startAt = now; | |
var endAt = now + (10*1000); | |
var stages = 5; | |
console.log("test"); | |
staggeredRollout(startAt, endAt, stages, "foo", now-1); | |
staggeredRollout(startAt, endAt, stages, "foo0", now); | |
staggeredRollout(startAt, endAt, stages, "fodd1", now+1000); | |
staggeredRollout(startAt, endAt, stages, "fdo2", now+2000); | |
staggeredRollout(startAt, endAt, stages, "aoo3", now+3000); | |
staggeredRollout(startAt, endAt, stages, "foo4", now+4000); | |
staggeredRollout(startAt, endAt, stages, "aoc5", now+5000); | |
staggeredRollout(startAt, endAt, stages, "foo6", now+6000); | |
staggeredRollout(startAt, endAt, stages, "bo7", now+7000); | |
staggeredRollout(startAt, endAt, stages, "foo8", now+8000); | |
staggeredRollout(startAt, endAt, stages, "foo9", now+9000); | |
staggeredRollout(startAt, endAt, stages, "foo10", now+10000); | |
staggeredRollout(startAt, endAt, stages, "foo11", now+11000); | |
} | |
test(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment