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

grpc_header.go « featureflag « internal - gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e71f1cfe9e350ab82542a978d003356c86359a95 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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)
}