-
-
Notifications
You must be signed in to change notification settings - Fork 204
fix: throttle PROVIDERSTATUS server events #2974
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dirkwa
wants to merge
1
commit into
SignalK:master
Choose a base branch
from
dirkwa:fix-providerstatus-throttle
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,49 @@ | ||
| /** | ||
| * Coalesces PROVIDERSTATUS serverevents. | ||
| * | ||
| * setPluginStatus / setProviderStatus each carry the full status list, and | ||
| * serverevents are written to every admin-UI WebSocket without | ||
| * backpressure, so a plugin calling them in a tight loop can push megabytes | ||
| * per second into each socket. request() emits at most once per | ||
| * PROVIDER_STATUS_MIN_INTERVAL_MS: the first request after a quiet interval | ||
| * emits promptly, and every further request before the interval has passed | ||
| * folds into a single emit scheduled for when it does. The admin UI thus | ||
| * sees the latest status within one interval however chatty the caller is. | ||
| */ | ||
|
|
||
| export const PROVIDER_STATUS_MIN_INTERVAL_MS = 1000 | ||
|
|
||
| export class ProviderStatusEmitter { | ||
| private timer: NodeJS.Timeout | undefined | ||
| private lastEmitAt = 0 | ||
|
|
||
| constructor( | ||
| private readonly emit: () => void, | ||
| private readonly minIntervalMs = PROVIDER_STATUS_MIN_INTERVAL_MS | ||
| ) {} | ||
|
|
||
| request(): void { | ||
| if (this.timer) return | ||
| const wait = Math.max(0, this.lastEmitAt + this.minIntervalMs - Date.now()) | ||
| this.timer = setTimeout(() => { | ||
| this.timer = undefined | ||
| this.emitNow() | ||
| }, wait) | ||
| // A pending status refresh must never keep a stopping server alive. | ||
| this.timer.unref?.() | ||
| } | ||
|
|
||
| /** Emit immediately, dropping any pending request it would duplicate. */ | ||
| emitNow(): void { | ||
| this.cancel() | ||
| this.lastEmitAt = Date.now() | ||
| this.emit() | ||
| } | ||
|
|
||
| cancel(): void { | ||
| if (this.timer) { | ||
| clearTimeout(this.timer) | ||
| this.timer = undefined | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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,74 @@ | ||
| import { expect } from 'chai' | ||
| import { ProviderStatusEmitter } from '../dist/providerStatusEmitter' | ||
|
|
||
| const INTERVAL_MS = 40 | ||
| // Long enough for a zero-delay timer to fire, short against INTERVAL_MS. | ||
| const SETTLE_MS = 5 | ||
| const BURST_SIZE = 1000 | ||
|
|
||
| const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms)) | ||
|
|
||
| function burst(emitter: ProviderStatusEmitter) { | ||
| for (let i = 0; i < BURST_SIZE; i++) { | ||
| emitter.request() | ||
| } | ||
| } | ||
|
|
||
| describe('ProviderStatusEmitter', function () { | ||
| it('folds a burst into one prompt emit and one more when the interval expires', async function () { | ||
| let emits = 0 | ||
| const emitter = new ProviderStatusEmitter(() => emits++, INTERVAL_MS) | ||
|
|
||
| burst(emitter) | ||
| await sleep(SETTLE_MS) | ||
| expect(emits).to.equal(1) | ||
|
|
||
| burst(emitter) | ||
| await sleep(INTERVAL_MS / 2) | ||
| expect(emits).to.equal(1) | ||
| await sleep(INTERVAL_MS) | ||
| expect(emits).to.equal(2) | ||
|
|
||
| emitter.cancel() | ||
| }) | ||
|
|
||
| it('emits promptly when the last emit is older than the interval', async function () { | ||
| let emits = 0 | ||
| const emitter = new ProviderStatusEmitter(() => emits++, INTERVAL_MS) | ||
|
|
||
| emitter.request() | ||
| await sleep(SETTLE_MS) | ||
| expect(emits).to.equal(1) | ||
|
|
||
| await sleep(INTERVAL_MS + SETTLE_MS) | ||
| emitter.request() | ||
| await sleep(SETTLE_MS) | ||
| expect(emits).to.equal(2) | ||
|
|
||
| emitter.cancel() | ||
| }) | ||
|
|
||
| it('emitNow drops a pending request instead of emitting twice', async function () { | ||
| let emits = 0 | ||
| const emitter = new ProviderStatusEmitter(() => emits++, INTERVAL_MS) | ||
|
|
||
| emitter.emitNow() | ||
| emitter.request() | ||
| expect(emits).to.equal(1) | ||
| emitter.emitNow() | ||
| expect(emits).to.equal(2) | ||
| await sleep(INTERVAL_MS + SETTLE_MS) | ||
| expect(emits).to.equal(2) | ||
| }) | ||
|
|
||
| it('cancel discards a pending request', async function () { | ||
| let emits = 0 | ||
| const emitter = new ProviderStatusEmitter(() => emits++, INTERVAL_MS) | ||
|
|
||
| emitter.emitNow() | ||
| emitter.request() | ||
| emitter.cancel() | ||
| await sleep(INTERVAL_MS + SETTLE_MS) | ||
| expect(emits).to.equal(1) | ||
| }) | ||
| }) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is not a ProviderStatusEmitter - it does not emit provider statuses, but is used to emit them. to me it is ThrottledCaller - it calls the given function but throttles the calls as described. imho naming should reflect that and min interval come from callee.