[PM-6394] remove policy evaluator cache (#9807)

This commit is contained in:
✨ Audrey ✨ 2024-07-01 10:25:21 -04:00 committed by GitHub
parent 33c985e00b
commit cb400a1ba6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 4 additions and 52 deletions

View File

@ -1,4 +1,4 @@
import { firstValueFrom, share, timer, ReplaySubject, Observable } from "rxjs"; import { firstValueFrom, Observable } from "rxjs";
// FIXME: use index.ts imports once policy abstractions and models // FIXME: use index.ts imports once policy abstractions and models
// implement ADR-0002 // implement ADR-0002
@ -7,13 +7,6 @@ import { UserId } from "../../types/guid";
import { GeneratorStrategy, GeneratorService, PolicyEvaluator } from "./abstractions"; import { GeneratorStrategy, GeneratorService, PolicyEvaluator } from "./abstractions";
type DefaultGeneratorServiceTuning = {
/* amount of time to keep the most recent policy after a subscription ends. Once the
* cache expires, the ignoreQty and timeoutMs settings apply to the next lookup.
*/
policyCacheMs: number;
};
/** {@link GeneratorServiceAbstraction} */ /** {@link GeneratorServiceAbstraction} */
export class DefaultGeneratorService<Options, Policy> implements GeneratorService<Options, Policy> { export class DefaultGeneratorService<Options, Policy> implements GeneratorService<Options, Policy> {
/** Instantiates the generator service /** Instantiates the generator service
@ -24,18 +17,8 @@ export class DefaultGeneratorService<Options, Policy> implements GeneratorServic
constructor( constructor(
private strategy: GeneratorStrategy<Options, Policy>, private strategy: GeneratorStrategy<Options, Policy>,
private policy: PolicyService, private policy: PolicyService,
tuning: Partial<DefaultGeneratorServiceTuning> = {}, ) {}
) {
this.tuning = Object.assign(
{
// a minute
policyCacheMs: 60000,
},
tuning,
);
}
private tuning: DefaultGeneratorServiceTuning;
private _evaluators$ = new Map<UserId, Observable<PolicyEvaluator<Policy, Options>>>(); private _evaluators$ = new Map<UserId, Observable<PolicyEvaluator<Policy, Options>>>();
/** {@link GeneratorService.options$} */ /** {@link GeneratorService.options$} */
@ -69,13 +52,6 @@ export class DefaultGeneratorService<Options, Policy> implements GeneratorServic
const evaluator$ = this.policy.getAll$(this.strategy.policy, userId).pipe( const evaluator$ = this.policy.getAll$(this.strategy.policy, userId).pipe(
// create the evaluator from the policies // create the evaluator from the policies
this.strategy.toEvaluator(), this.strategy.toEvaluator(),
// cache evaluator in a replay subject to amortize creation cost
// and reduce GC pressure.
share({
connector: () => new ReplaySubject(1),
resetOnRefCountZero: () => timer(this.tuning.policyCacheMs),
}),
); );
return evaluator$; return evaluator$;

View File

@ -1,4 +1,4 @@
import { firstValueFrom, share, timer, ReplaySubject, Observable } from "rxjs"; import { firstValueFrom, Observable } from "rxjs";
// FIXME: use index.ts imports once policy abstractions and models // FIXME: use index.ts imports once policy abstractions and models
// implement ADR-0002 // implement ADR-0002
@ -7,13 +7,6 @@ import { UserId } from "@bitwarden/common/types/guid";
import { GeneratorStrategy, GeneratorService, PolicyEvaluator } from "../abstractions"; import { GeneratorStrategy, GeneratorService, PolicyEvaluator } from "../abstractions";
type DefaultGeneratorServiceTuning = {
/* amount of time to keep the most recent policy after a subscription ends. Once the
* cache expires, the ignoreQty and timeoutMs settings apply to the next lookup.
*/
policyCacheMs: number;
};
/** {@link GeneratorServiceAbstraction} */ /** {@link GeneratorServiceAbstraction} */
export class DefaultGeneratorService<Options, Policy> implements GeneratorService<Options, Policy> { export class DefaultGeneratorService<Options, Policy> implements GeneratorService<Options, Policy> {
/** Instantiates the generator service /** Instantiates the generator service
@ -24,18 +17,8 @@ export class DefaultGeneratorService<Options, Policy> implements GeneratorServic
constructor( constructor(
private strategy: GeneratorStrategy<Options, Policy>, private strategy: GeneratorStrategy<Options, Policy>,
private policy: PolicyService, private policy: PolicyService,
tuning: Partial<DefaultGeneratorServiceTuning> = {}, ) {}
) {
this.tuning = Object.assign(
{
// a minute
policyCacheMs: 60000,
},
tuning,
);
}
private tuning: DefaultGeneratorServiceTuning;
private _evaluators$ = new Map<UserId, Observable<PolicyEvaluator<Policy, Options>>>(); private _evaluators$ = new Map<UserId, Observable<PolicyEvaluator<Policy, Options>>>();
/** {@link GeneratorService.options$} */ /** {@link GeneratorService.options$} */
@ -69,13 +52,6 @@ export class DefaultGeneratorService<Options, Policy> implements GeneratorServic
const evaluator$ = this.policy.getAll$(this.strategy.policy, userId).pipe( const evaluator$ = this.policy.getAll$(this.strategy.policy, userId).pipe(
// create the evaluator from the policies // create the evaluator from the policies
this.strategy.toEvaluator(), this.strategy.toEvaluator(),
// cache evaluator in a replay subject to amortize creation cost
// and reduce GC pressure.
share({
connector: () => new ReplaySubject(1),
resetOnRefCountZero: () => timer(this.tuning.policyCacheMs),
}),
); );
return evaluator$; return evaluator$;