Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/packages/util/failing-to-save.ts
Views: 687
/*1* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45/*6If we are having repeated hash mismatches when trying to save a particular path,7then this function will return true. This can be used by the caller to8reconnect and fix everything. This is basically a stupid workaround for a subtle9but very annoying bug that I don't know how to reproduce or fix...10*/1112// Trigger a failure if there have been THRESHOLD.fails during13// the last THRESHOLD.interval_s seconds.14const THRESHOLD = { fails: 3, interval_s: 60 };1516interface PathState {17path: string;18failures: number[];19}2021function is_failing(x: PathState): boolean {22const cutoff = new Date().getTime() - THRESHOLD.interval_s * 1000;23const failures: number[] = [];24let t: number;25for (t of x.failures) {26if (t >= cutoff) {27failures.push(t);28}29}30if (failures.length >= THRESHOLD.fails) {31x.failures = [];32return true;33} else {34x.failures = failures;35return false;36}37}3839const state: { [key: string]: PathState } = {};4041export function failing_to_save(42path: string,43hash: number,44expected_hash?: number45): boolean {46if (expected_hash == null) {47return false;48}49if (!state[path]) {50state[path] = { path: path, failures: [] };51}5253if (hash != expected_hash) {54state[path].failures.push(new Date().getTime());55return is_failing(state[path]);56}5758// definitely NOT failing.59return false;60}616263