Skip to content

Commit 5fc787c

Browse files
refactor: optimize rule-based segment matcher
1 parent 3046752 commit 5fc787c

File tree

2 files changed

+22
-20
lines changed

2 files changed

+22
-20
lines changed

src/dtos/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ export interface ISplitCondition {
199199
conditionType?: 'ROLLOUT' | 'WHITELIST'
200200
}
201201

202-
export interface IExcludedSegments {
202+
export interface IExcludedSegment {
203203
type: 'standard' | 'large' | 'rule-based',
204204
name: string,
205205
}
@@ -211,7 +211,7 @@ export interface IRBSegment {
211211
conditions?: ISplitCondition[],
212212
excluded?: {
213213
keys?: string[],
214-
segments?: IExcludedSegments[]
214+
segments?: IExcludedSegment[]
215215
}
216216
}
217217

src/evaluator/matchers/rbsegment.ts

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { IRBSegment, MaybeThenable } from '../../dtos/types';
1+
import { IExcludedSegment, IRBSegment, MaybeThenable } from '../../dtos/types';
22
import { IStorageAsync, IStorageSync } from '../../storages/types';
33
import { ILogger } from '../../logger/types';
44
import { IDependencyMatcherValue, ISplitEvaluator } from '../types';
@@ -11,6 +11,7 @@ import { STANDARD_SEGMENT, RULE_BASED_SEGMENT, LARGE_SEGMENT } from '../../utils
1111
export function ruleBasedSegmentMatcherContext(segmentName: string, storage: IStorageSync | IStorageAsync, log: ILogger) {
1212

1313
return function ruleBasedSegmentMatcher({ key, attributes }: IDependencyMatcherValue, splitEvaluator: ISplitEvaluator): MaybeThenable<boolean> {
14+
const matchingKey = getMatching(key);
1415

1516
function matchConditions(rbsegment: IRBSegment) {
1617
const conditions = rbsegment.conditions || [];
@@ -30,28 +31,29 @@ export function ruleBasedSegmentMatcherContext(segmentName: string, storage: ISt
3031
evaluation ? true : false;
3132
}
3233

34+
function isInExcludedSegment({ type, name }: IExcludedSegment) {
35+
return type === STANDARD_SEGMENT ?
36+
storage.segments.isInSegment(name, matchingKey) :
37+
type === RULE_BASED_SEGMENT ?
38+
ruleBasedSegmentMatcherContext(name, storage, log)({ key, attributes }, splitEvaluator) :
39+
type === LARGE_SEGMENT && (storage as IStorageSync).largeSegments ?
40+
(storage as IStorageSync).largeSegments!.isInSegment(name, matchingKey) :
41+
false;
42+
}
43+
3344
function isExcluded(rbSegment: IRBSegment) {
34-
const matchingKey = getMatching(key);
3545
const excluded = rbSegment.excluded || {};
3646

3747
if (excluded.keys && excluded.keys.indexOf(matchingKey) !== -1) return true;
3848

39-
const isInSegment = (excluded.segments || []).map(({ type, name }) => {
40-
return type === STANDARD_SEGMENT ?
41-
storage.segments.isInSegment(name, matchingKey) :
42-
type === RULE_BASED_SEGMENT ?
43-
ruleBasedSegmentMatcherContext(name, storage, log)({ key, attributes }, splitEvaluator) :
44-
type === LARGE_SEGMENT && (storage as IStorageSync).largeSegments ?
45-
(storage as IStorageSync).largeSegments!.isInSegment(name, matchingKey) :
46-
false;
47-
});
48-
49-
return isInSegment.length && thenable(isInSegment[0]) ?
50-
Promise.all(isInSegment).then(results => results.some(result => result)) :
51-
isInSegment.some(result => result);
49+
return (excluded.segments || []).reduce<MaybeThenable<boolean>>((result, excludedSegment) => {
50+
return thenable(result) ?
51+
result.then(result => result || isInExcludedSegment(excludedSegment)) :
52+
result || isInExcludedSegment(excludedSegment);
53+
}, false);
5254
}
5355

54-
function isInSegment(rbSegment: IRBSegment | null) {
56+
function isInRBSegment(rbSegment: IRBSegment | null) {
5557
if (!rbSegment) return false;
5658
const excluded = isExcluded(rbSegment);
5759

@@ -63,7 +65,7 @@ export function ruleBasedSegmentMatcherContext(segmentName: string, storage: ISt
6365
const rbSegment = storage.rbSegments.get(segmentName);
6466

6567
return thenable(rbSegment) ?
66-
rbSegment.then(isInSegment) :
67-
isInSegment(rbSegment);
68+
rbSegment.then(isInRBSegment) :
69+
isInRBSegment(rbSegment);
6870
};
6971
}

0 commit comments

Comments
 (0)