Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions pkg/settings/cresettings/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ flowchart
end
%% WorkflowLimit - Deprecated
%% TODO unused
%% FeatureFlags
%% PerOrg.FeatureFlags
%% PerOrg.ZeroBalancePruningTimeout
%% PerOwner.FeatureFlags
%% PerWorkflow.FeatureFlags

subgraph Store.FetchWorkflowArtifacts
PerWorkflow.WASMConfigSizeLimit{{PerWorkflow.WASMConfigSizeLimit}}:::bound
Expand Down
4 changes: 4 additions & 0 deletions pkg/settings/cresettings/defaults.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,18 @@
"VaultIdentifierNamespaceSizeLimit": "64b",
"VaultPluginBatchSizeLimit": "20",
"VaultRequestBatchSizeLimit": "10",
"FeatureFlags": {},
"PerOrg": {
"FeatureFlags": {},
"ZeroBalancePruningTimeout": "24h0m0s"
},
"PerOwner": {
"FeatureFlags": {},
"WorkflowExecutionConcurrencyLimit": "5",
"VaultSecretsLimit": "100"
},
"PerWorkflow": {
"FeatureFlags": {},
"TriggerRegistrationsTimeout": "10s",
"TriggerSubscriptionTimeout": "15s",
"TriggerSubscriptionLimit": "10",
Expand Down
12 changes: 12 additions & 0 deletions pkg/settings/cresettings/defaults.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,22 @@ VaultIdentifierNamespaceSizeLimit = '64b'
VaultPluginBatchSizeLimit = '20'
VaultRequestBatchSizeLimit = '10'

[FeatureFlags]
Flags = []

[PerOrg]
ZeroBalancePruningTimeout = '24h0m0s'

[PerOrg.FeatureFlags]
Flags = []

[PerOwner]
WorkflowExecutionConcurrencyLimit = '5'
VaultSecretsLimit = '100'

[PerOwner.FeatureFlags]
Flags = []

[PerWorkflow]
TriggerRegistrationsTimeout = '10s'
TriggerSubscriptionTimeout = '15s'
Expand All @@ -36,6 +45,9 @@ WASMSecretsSizeLimit = '1mb'
LogLineLimit = '1kb'
LogEventLimit = '1000'

[PerWorkflow.FeatureFlags]
Flags = []

[PerWorkflow.ChainAllowed]
Default = 'false'

Expand Down
5 changes: 5 additions & 0 deletions pkg/settings/cresettings/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,20 +175,25 @@ type Schema struct {
VaultPluginBatchSizeLimit Setting[int] `unit:"{request}"`
VaultRequestBatchSizeLimit Setting[int] `unit:"{request}"`

FeatureFlags FeatureFlags

PerOrg Orgs `scope:"org"`
PerOwner Owners `scope:"owner"`
PerWorkflow Workflows `scope:"workflow"`
}
type Orgs struct {
FeatureFlags FeatureFlags
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A simpler approach would be to just use Setting[time.Time] or similar (e.g. unix ts as int).

Suggested change
FeatureFlags FeatureFlags
FooBarEffectiveDate Setting[time.Time]

This will essentially work without any other code changes. We could additionally add a new limiter data structure, maybe FeatureLimiter, which behaves like a Gate that understands the effective dates. This would be a little more intuitive to use since everything currently behaves as an upper bound, but these are effectively lower bounds. However, it is not strictly necessary, since you can get the value and do the basic enforcement of date comparison yourself.

ZeroBalancePruningTimeout Setting[time.Duration]
}

type Owners struct {
FeatureFlags FeatureFlags
WorkflowExecutionConcurrencyLimit Setting[int] `unit:"{workflow}"`
VaultSecretsLimit Setting[int] `unit:"{secret}"`
}

type Workflows struct {
FeatureFlags FeatureFlags
TriggerRegistrationsTimeout Setting[time.Duration]
TriggerSubscriptionTimeout Setting[time.Duration]
TriggerSubscriptionLimit Setting[int] `unit:"{subscription}"`
Expand Down
126 changes: 126 additions & 0 deletions pkg/settings/feature_flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package settings

import (
"context"
"fmt"
"strconv"
)

// FeatureFlag represents a single feature flag with an activation threshold and optional metadata.
type FeatureFlag struct {
Name string `json:"Name"`
ActivateAt int64 `json:"ActivateAt"`
Metadata map[string]string `json:"Metadata,omitempty"`
}

// FeatureFlags holds a collection of feature flags at a given scope.
// It integrates with InitConfig and follows the same scoped precedence as Setting[T].
//
// Flags defined in the schema serve as defaults. The Getter provides scoped overrides
// using dot-separated keys: <FeatureFlags key>.<flagName>.ActivateAt
//
// Use With(getter) to bind a Getter, so callers of IsActive/GetFlag/GetMetadata
// don't need to pass one explicitly.
type FeatureFlags struct {
Flags []FeatureFlag `json:"Flags,omitempty"`

// key is the settings path prefix assigned by InitConfig (e.g. "PerWorkflow.FeatureFlags").
// It positions this collection in the settings tree for scoped getter lookups.
key string
scope Scope
getter Getter
}

var _ keySetter = &FeatureFlags{}

func (f *FeatureFlags) initSetting(key string, scope Scope, _ *string) error {
f.key = key
f.scope = scope
return nil
}

func (f *FeatureFlags) GetKey() string { return f.key }
func (f *FeatureFlags) GetScope() Scope { return f.scope }

// With returns a copy of FeatureFlags bound to the given Getter.
// The returned value can be used without passing a Getter to each method call.
func (f FeatureFlags) With(g Getter) FeatureFlags {
f.getter = g
return f
}

func (f *FeatureFlags) getDefault(name string) *FeatureFlag {
for i := range f.Flags {
if f.Flags[i].Name == name {
return &f.Flags[i]
}
}
return nil
}

// GetFlag looks up a feature flag by name, checking scoped overrides via the bound Getter first,
// then falling back to the default flags defined in the schema.
// The returned FeatureFlag is a copy; mutating it has no effect on the stored flags.
func (f *FeatureFlags) GetFlag(ctx context.Context, name string) (*FeatureFlag, error) {
if f.getter != nil {
activateAtKey := f.key + "." + name + ".ActivateAt"
str, err := f.getter.GetScoped(ctx, f.scope, activateAtKey)
if err != nil {
return nil, fmt.Errorf("failed to get feature flag %s: %w", name, err)
}
if str != "" {
activateAt, err := strconv.ParseInt(str, 10, 64)
if err != nil {
return nil, fmt.Errorf("invalid ActivateAt for flag %s: %w", name, err)
}
flag := &FeatureFlag{
Name: name,
ActivateAt: activateAt,
}
if def := f.getDefault(name); def != nil && def.Metadata != nil {
flag.Metadata = make(map[string]string, len(def.Metadata))
for k, v := range def.Metadata {
flag.Metadata[k] = v
}
}
return flag, nil
}
}
if def := f.getDefault(name); def != nil {
cp := *def
return &cp, nil
}
return nil, nil
}

// IsActive returns true if the named flag exists and executionTimestamp >= ActivateAt.
func (f *FeatureFlags) IsActive(ctx context.Context, name string, executionTimestamp int64) (bool, error) {
flag, err := f.GetFlag(ctx, name)
if err != nil {
return false, err
}
if flag == nil {
return false, nil
}
return executionTimestamp >= flag.ActivateAt, nil
}

// GetMetadata returns a single metadata value for a flag, checking scoped overrides via
// the bound Getter first (at key <FeatureFlags key>.<flagName>.Metadata.<metaKey>),
// then falling back to the default flag's metadata.
func (f *FeatureFlags) GetMetadata(ctx context.Context, flagName, metaKey string) (string, error) {
if f.getter != nil {
key := f.key + "." + flagName + ".Metadata." + metaKey
str, err := f.getter.GetScoped(ctx, f.scope, key)
if err != nil {
return "", err
}
if str != "" {
return str, nil
}
}
if def := f.getDefault(flagName); def != nil && def.Metadata != nil {
return def.Metadata[metaKey], nil
}
return "", nil
}
Loading
Loading