Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'internal/featureflag/grpc_header.go')
-rw-r--r--internal/featureflag/grpc_header.go38
1 files changed, 38 insertions, 0 deletions
diff --git a/internal/featureflag/grpc_header.go b/internal/featureflag/grpc_header.go
new file mode 100644
index 000000000..e71f1cfe9
--- /dev/null
+++ b/internal/featureflag/grpc_header.go
@@ -0,0 +1,38 @@
+package featureflag
+
+import (
+ "context"
+ "fmt"
+
+ "google.golang.org/grpc/metadata"
+)
+
+// IsEnabled checks if the feature flag is enabled for the passed context.
+// Only return true if the metadata for the feature flag is set to "true"
+func IsEnabled(ctx context.Context, flag string) bool {
+ if flag == "" {
+ return false
+ }
+
+ md, ok := metadata.FromIncomingContext(ctx)
+ if !ok {
+ return false
+ }
+
+ val, ok := md[HeaderKey(flag)]
+ if !ok {
+ return false
+ }
+
+ return len(val) > 0 && val[0] == "true"
+}
+
+// IsDisabled is the inverse operation of IsEnabled
+func IsDisabled(ctx context.Context, flag string) bool {
+ return !IsEnabled(ctx, flag)
+}
+
+// HeaderKey returns the feature flag key to be used in the metadata map
+func HeaderKey(flag string) string {
+ return fmt.Sprintf("gitaly-feature-%s", flag)
+}