Created
April 5, 2024 13:43
-
-
Save phiresky/ed108b45eed2dfee276d55e6c1146b2a to your computer and use it in GitHub Desktop.
lemmy exponential retry simulation
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
function calc(base) { | |
function retryDelaySeconds(retry) { | |
return Math.pow(base, retry); | |
} | |
function timeToStr(sec) { | |
sec = Math.round(sec); | |
if (sec < 60) { | |
return sec + "s"; | |
} | |
if (sec < 3600) { | |
return Math.floor(sec / 60) + "m " + (sec % 60) + "s"; | |
} | |
return Math.floor(sec / 3600) + "h " + Math.floor((sec % 3600) / 60) + "m"; | |
} | |
downtimeSeconds = [ | |
1 * 60, | |
10 * 60, | |
30 * 60, | |
60 * 60, | |
2 * 3600, | |
3 * 3600, | |
6 * 3600, | |
12 * 3600, | |
24 * 3600, | |
48 * 3600, | |
7 * 24 * 3600, | |
]; | |
let table = []; | |
for (downtime of downtimeSeconds) { | |
let retries = 0; | |
let currentTime = 0; | |
while (currentTime < downtime) { | |
currentTime += retryDelaySeconds(retries); | |
retries++; | |
} | |
table.push({ | |
"Instance Downtime": timeToStr(downtime), | |
"Federation Resumed At": timeToStr(currentTime), | |
"Total Retry Attempts": retries, | |
/*retries2: Math.log(downtime)/Math.log(base)*/ | |
}); | |
} | |
return jsonToHtmlTable(table); | |
} | |
function jsonToHtmlTable(json) { | |
// input is an array of objects, the object keys are the column and the values the rows | |
let table = "<table><tr>"; | |
for (let key of Object.keys(json[0])) { | |
table += "<th>" + key + "</th>"; | |
} | |
table += "</tr>"; | |
for (let row of json) { | |
table += "<tr>"; | |
for (let key of Object.keys(row)) { | |
table += "<td>" + row[key] + "</td>"; | |
} | |
table += "</tr>"; | |
} | |
table += "</table>"; | |
return table + "\n"; | |
} | |
copy(calc(2) + calc(1.5) + calc(1.25)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment