-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf(node): [NET-1113] Optimize operators query in `checkOperatorValu…
…eBreach` (#2721) The `checkOperatorValueBreach` functionality uses cached list of staked operators instead of querying random operator address on each invocation. ## Changes - `OperatorPlugin` creates a cache of staked operators, gives that as an to the `checkOperatorValueBreach` call - added `Cache` utility to `@streamr/utils` package - renamed `Operator#getOperatorAddresses` -> `getStakedOperators`, it no longer uses `updateRequiredBlockNumber()` functionality --------- Co-authored-by: Eric Andrews <[email protected]>
- Loading branch information
Showing
8 changed files
with
78 additions
and
34 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
6 changes: 4 additions & 2 deletions
6
packages/node/src/plugins/operator/checkOperatorValueBreach.ts
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
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,21 @@ | ||
export class Cache<V> { | ||
|
||
private value?: V | ||
private valueTimestamp?: number | ||
private readonly valueFactory: () => Promise<V> | ||
private readonly maxAgeInMilliseconds: number | ||
|
||
constructor(valueFactory: () => Promise<V>, maxAgeInMilliseconds: number) { | ||
this.valueFactory = valueFactory | ||
this.maxAgeInMilliseconds = maxAgeInMilliseconds | ||
} | ||
|
||
async get(): Promise<V> { | ||
const now = Date.now() | ||
if ((this.valueTimestamp === undefined) || (now > (this.valueTimestamp + this.maxAgeInMilliseconds))) { | ||
this.value = await this.valueFactory() | ||
this.valueTimestamp = now | ||
} | ||
return this.value! | ||
} | ||
} |
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,24 @@ | ||
import { Cache } from '../src/Cache' | ||
import { wait } from '../src/wait' | ||
|
||
const MAX_AGE = 100 | ||
const JITTER_FACTOR = 10 | ||
|
||
describe('Cache', () => { | ||
|
||
it('happy path', async () => { | ||
let plainValue = 'foo' | ||
const valueFactory = jest.fn().mockImplementation(async () => plainValue) | ||
const cache = new Cache(valueFactory, MAX_AGE) | ||
expect(await cache.get()).toEqual('foo') | ||
expect(valueFactory).toHaveBeenCalledTimes(1) | ||
plainValue = 'bar' | ||
// should not change yet | ||
expect(await cache.get()).toEqual('foo') | ||
expect(valueFactory).toHaveBeenCalledTimes(1) | ||
// changes after max age elapsed | ||
await wait(MAX_AGE + JITTER_FACTOR) | ||
expect(await cache.get()).toEqual('bar') | ||
expect(valueFactory).toHaveBeenCalledTimes(2) | ||
}) | ||
}) |