Skip to content

Commit

Permalink
feat: constraints limit in a strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
kwasniew committed Jul 8, 2024
1 parent 225d8a9 commit 2008bc1
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/lib/__snapshots__/create-config.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ exports[`should create default config 1`] = `
"actionSetsPerProject": 5,
"apiTokens": 2000,
"constraintValues": 250,
"constraints": 30,
"environments": 50,
"featureEnvironmentStrategies": 30,
"featureFlags": 5000,
Expand Down
4 changes: 4 additions & 0 deletions src/lib/create-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,10 @@ export function createConfig(options: IUnleashOptions): IUnleashConfig {
options?.resourceLimits?.constraintValues || 250,
),
),
constraints: Math.max(
0,
parseEnvVarNumber(process.env.UNLEASH_CONSTRAINTS_LIMIT, 30),
),
environments: parseEnvVarNumber(
process.env.UNLEASH_ENVIRONMENTS_LIMIT,
50,
Expand Down
12 changes: 9 additions & 3 deletions src/lib/features/feature-toggle/feature-toggle-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -390,16 +390,22 @@ class FeatureToggleService {
validateConstraintValuesLimit(updatedConstrains: IConstraint[]) {
if (!this.flagResolver.isEnabled('resourceLimits')) return;

const limit = this.resourceLimits.constraintValues;
const constraintsLimit = this.resourceLimits.constraints;
if (updatedConstrains.length > constraintsLimit) {
throw new ExceedsLimitError(`constraints`, constraintsLimit);
}

const constraintValuesLimit = this.resourceLimits.constraintValues;

const constraintOverLimit = updatedConstrains.find(
(constraint) =>
Array.isArray(constraint.values) &&
constraint.values?.length > limit,
constraint.values?.length > constraintValuesLimit,
);
if (constraintOverLimit) {
throw new ExceedsLimitError(
`content values for ${constraintOverLimit.contextName}`,
limit,
constraintValuesLimit,
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,50 @@ describe('Strategy limits', () => {
);
});

test('Should not allow to exceed constraints limit', async () => {
const LIMIT = 1;
const { featureToggleService, featureToggleStore } =
createFakeFeatureToggleService({
getLogger,
flagResolver: alwaysOnFlagResolver,
resourceLimits: {
constraints: LIMIT,
},
} as unknown as IUnleashConfig);

const addStrategy = (constraints: IConstraint[]) =>
featureToggleService.unprotectedCreateStrategy(
{
name: 'default',
featureName: 'feature',
constraints: constraints,
} as IStrategyConfig,
{ projectId: 'default', featureName: 'feature' } as any,
{} as IAuditUser,
);
await featureToggleStore.create('default', {
name: 'feature',
createdByUserId: 1,
});

await expect(
addStrategy([
{
values: ['1'],
operator: 'IN',
contextName: 'accountId',
},
{
values: ['2'],
operator: 'IN',
contextName: 'accountId',
},
]),
).rejects.toThrow(
"Failed to create constraints. You can't create more than the established limit of 1",
);
});

test('Should not allow to exceed constraint values limit', async () => {
const LIMIT = 3;
const { featureToggleService, featureToggleStore } =
Expand Down
7 changes: 7 additions & 0 deletions src/lib/openapi/spec/resource-limits-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export const resourceLimitsSchema = {
'apiTokens',
'segments',
'featureFlags',
'constraints',
],
additionalProperties: false,
properties: {
Expand Down Expand Up @@ -80,6 +81,12 @@ export const resourceLimitsSchema = {
description:
'The maximum number of values for a single constraint.',
},
constraints: {
type: 'integer',
example: 30,
description:
'The maximum number of constraints in a single strategy.',
},
environments: {
type: 'integer',
minimum: 1,
Expand Down

0 comments on commit 2008bc1

Please sign in to comment.