-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Throttler for frequent cleanup & ui db functions (#318)
* Add debouncer module with test * Move Pino bindings and Utils to envio package * Use Pino logger in debouncer * Update test to use logger * Update name from delayMillis to intervalMillis * Use debouncer instead of lazy writer for Benchmark module * Move chainMetaData updates to using a debouncer * Add debouncing for db cleanup functions * Fix debouncer and add more tests * Improve debouncer and add test * Remove unused Debouncer function * Add rescript schema to package.json.tmpl * Rename debouncer to throttler everywhere * Add resi file with function docs * Remove promise from timeout
- Loading branch information
Showing
14 changed files
with
285 additions
and
105 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
type t = { | ||
mutable lastRunTimeMillis: float, | ||
mutable isRunning: bool, | ||
mutable isAwaitingInterval: bool, | ||
mutable scheduled: option<unit => promise<unit>>, | ||
intervalMillis: float, | ||
logger: Pino.t, | ||
} | ||
|
||
let make = (~intervalMillis: int, ~logger) => { | ||
lastRunTimeMillis: 0., | ||
isRunning: false, | ||
isAwaitingInterval: false, | ||
scheduled: None, | ||
intervalMillis: intervalMillis->Belt.Int.toFloat, | ||
logger, | ||
} | ||
|
||
let rec startInternal = async (throttler: t) => { | ||
switch throttler { | ||
| {scheduled: Some(fn), isRunning: false, isAwaitingInterval: false} => | ||
let timeSinceLastRun = Js.Date.now() -. throttler.lastRunTimeMillis | ||
|
||
//Only execute if we are passed the interval | ||
if timeSinceLastRun >= throttler.intervalMillis { | ||
throttler.isRunning = true | ||
throttler.scheduled = None | ||
throttler.lastRunTimeMillis = Js.Date.now() | ||
|
||
switch await fn() { | ||
| exception exn => | ||
throttler.logger->Pino.errorExn( | ||
Pino.createPinoMessageWithError("Scheduled action failed in throttler", exn), | ||
) | ||
| _ => () | ||
} | ||
throttler.isRunning = false | ||
|
||
await throttler->startInternal | ||
} else { | ||
//Store isAwaitingInterval in state so that timers don't continuously get created | ||
throttler.isAwaitingInterval = true | ||
let _ = Js.Global.setTimeout(() => { | ||
throttler.isAwaitingInterval = false | ||
throttler->startInternal->ignore | ||
}, Belt.Int.fromFloat(throttler.intervalMillis -. timeSinceLastRun)) | ||
} | ||
| _ => () | ||
} | ||
} | ||
|
||
let schedule = (throttler: t, fn) => { | ||
throttler.scheduled = Some(fn) | ||
throttler->startInternal->ignore | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/** | ||
Throttles a scheduled function to run at a minimum given interval | ||
|
||
Does NOT queue scheduled functions but rather overwrites them | ||
on each schedule call. | ||
*/ | ||
type t | ||
|
||
/** | ||
Creates a throttler that throttles scheduled functions to run at a minimum | ||
given interval in milliseconds. | ||
|
||
Does NOT queue scheduled functions but rather overwrites them | ||
|
||
The logger will be used to log any errors that occur in the scheduled | ||
functions. | ||
*/ | ||
let make: (~intervalMillis: int, ~logger: Pino.t) => t | ||
|
||
/** | ||
Schedules a function to be run on a throttler, overwriting any | ||
previously scheduled functions. Should only be used for functions | ||
that do not need to be executed if there is a more up to date scheduled | ||
function available. | ||
*/ | ||
let schedule: (t, unit => promise<unit>) => unit |
File renamed without changes.
File renamed without changes.
31 changes: 0 additions & 31 deletions
31
codegenerator/cli/templates/static/codegen/src/AsyncTaskQueue.res
This file was deleted.
Oops, something went wrong.
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
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.