diff options
author | Patrick Steinhardt <psteinhardt@gitlab.com> | 2022-06-30 11:16:51 +0300 |
---|---|---|
committer | Patrick Steinhardt <psteinhardt@gitlab.com> | 2022-07-06 09:09:26 +0300 |
commit | 80376f70acfb8a962dff1b12c78c8ae12cc01a46 (patch) | |
tree | 76425d266b96a06338b56721e6f59f2e8e798b75 | |
parent | 4bf7e3b0e4181e68bcce7e7243765b836193e4ac (diff) |
featureflag: Move context-related functions into "context.go"
Move functions whihc inject feature flags into the context into
"context.go" to group together related functionality.
-rw-r--r-- | internal/metadata/featureflag/context.go | 25 | ||||
-rw-r--r-- | internal/metadata/featureflag/featureflag.go | 23 |
2 files changed, 25 insertions, 23 deletions
diff --git a/internal/metadata/featureflag/context.go b/internal/metadata/featureflag/context.go index 9483abf25..5f27112b5 100644 --- a/internal/metadata/featureflag/context.go +++ b/internal/metadata/featureflag/context.go @@ -15,8 +15,20 @@ const ( // ffPrefix is the prefix used for Gitaly-scoped feature flags. ffPrefix = "gitaly-feature-" + + // explicitFeatureFlagKey is used by ContextWithExplicitFeatureFlags to mark a context as + // requiring all feature flags to have been explicitly defined. + explicitFeatureFlagKey = "require_explicit_feature_flag_checks" ) +// ContextWithExplicitFeatureFlags marks the context such that all feature flags which are checked +// must have been explicitly set in that context. If a feature flag wasn't set to an explicit value, +// then checking this feature flag will panic. This is not for use in production systems, but is +// intended for tests to verify that we test each feature flag properly. +func ContextWithExplicitFeatureFlags(ctx context.Context) context.Context { + return injectIntoIncomingAndOutgoingContext(ctx, explicitFeatureFlagKey, true) +} + // ContextWithFeatureFlag sets the feature flag in both the incoming and outgoing context. func ContextWithFeatureFlag(ctx context.Context, flag FeatureFlag, enabled bool) context.Context { return injectIntoIncomingAndOutgoingContext(ctx, flag.MetadataKey(), enabled) @@ -68,6 +80,19 @@ func incomingCtxWithFeatureFlag(ctx context.Context, key string, enabled bool) c return metadata.NewIncomingContext(ctx, md) } +func injectIntoIncomingAndOutgoingContext(ctx context.Context, key string, enabled bool) context.Context { + incomingMD, ok := metadata.FromIncomingContext(ctx) + if !ok { + incomingMD = metadata.New(map[string]string{}) + } + + incomingMD.Set(key, strconv.FormatBool(enabled)) + + ctx = metadata.NewIncomingContext(ctx, incomingMD) + + return metadata.AppendToOutgoingContext(ctx, key, strconv.FormatBool(enabled)) +} + // AllFlags returns all feature flags with their value that use the Gitaly metadata // prefix. Note: results will not be sorted. func AllFlags(ctx context.Context) []string { diff --git a/internal/metadata/featureflag/featureflag.go b/internal/metadata/featureflag/featureflag.go index 70f90d2d9..0644fa424 100644 --- a/internal/metadata/featureflag/featureflag.go +++ b/internal/metadata/featureflag/featureflag.go @@ -36,29 +36,6 @@ var ( All = []FeatureFlag{} ) -const explicitFeatureFlagKey = "require_explicit_feature_flag_checks" - -func injectIntoIncomingAndOutgoingContext(ctx context.Context, key string, enabled bool) context.Context { - incomingMD, ok := metadata.FromIncomingContext(ctx) - if !ok { - incomingMD = metadata.New(map[string]string{}) - } - - incomingMD.Set(key, strconv.FormatBool(enabled)) - - ctx = metadata.NewIncomingContext(ctx, incomingMD) - - return metadata.AppendToOutgoingContext(ctx, key, strconv.FormatBool(enabled)) -} - -// ContextWithExplicitFeatureFlags marks the context such that all feature flags which are checked -// must have been explicitly set in that context. If a feature flag wasn't set to an explicit value, -// then checking this feature flag will panic. This is not for use in production systems, but is -// intended for tests to verify that we test each feature flag properly. -func ContextWithExplicitFeatureFlags(ctx context.Context) context.Context { - return injectIntoIncomingAndOutgoingContext(ctx, explicitFeatureFlagKey, true) -} - // FeatureFlag gates the implementation of new or changed functionality. type FeatureFlag struct { // Name is the name of the feature flag. |