-
Notifications
You must be signed in to change notification settings - Fork 0
/
service_grpc.go
159 lines (130 loc) · 3.74 KB
/
service_grpc.go
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package provider
import (
"context"
"errors"
"log"
"github.com/redis/go-redis/v9"
"github.com/waduhek/flagger/proto/providerpb"
"github.com/waduhek/flagger/internal/project"
)
// FlagProviderServer is an implementation of the FlagProvider service.
type FlagProviderServer struct {
providerpb.UnimplementedFlagProviderServer
providerDataRepo DataRepository
providerCacheRepo CacheRepository
}
func (s *FlagProviderServer) GetFlag(
ctx context.Context,
req *providerpb.GetFlagRequest,
) (*providerpb.GetFlagResponse, error) {
projectKey, ok := project.KeyFromContext(ctx)
if !ok {
log.Printf("could not find project key in request")
return nil, project.ErrProjectKeyNotFound
}
// Check if the flag status has been cached previously and return it.
isFlagStatusCached, isCachedErr :=
s.checkIfFlagStatusIsCached(ctx, projectKey, req)
if isCachedErr != nil {
return nil, isCachedErr
}
if isFlagStatusCached {
log.Printf("found cached value for flag status")
cachedStatus, cachedStatusErr := s.getCachedFlagStatus(ctx, projectKey, req)
if cachedStatusErr != nil {
return nil, cachedStatusErr
}
response := &providerpb.GetFlagResponse{
Status: cachedStatus,
}
return response, nil
}
environmentName := req.GetEnvironment()
flagName := req.GetFlagName()
flagDetails, err := s.providerDataRepo.GetFlagDetailsByProjectKey(
ctx,
projectKey,
environmentName,
flagName,
)
if err != nil {
log.Printf("error while fetching details of the flag: %v", err)
return nil, ErrFetchFlagDetails
}
if len(flagDetails) != 1 {
log.Printf("found %d responses of flag details", len(flagDetails))
return nil, ErrIncorrectFlagDetailCount
}
flagDetail := flagDetails[0]
status := flagDetail.FlagSetting.IsActive
// Cache the result of this flag status for the next time.
cacheParams := cacheParameters{
ProjectKey: projectKey,
EnvironmentName: environmentName,
FlagName: flagName,
}
cacheErr := s.providerCacheRepo.CacheFlagStatus(ctx, &cacheParams, status)
if cacheErr != nil {
log.Printf("could not cache flag status: %v. ignoring error", cacheErr)
}
response := &providerpb.GetFlagResponse{
Status: status,
}
return response, nil
}
// checkIfFlagStatusIsCached checks if the requested flag's status has already
// been cached.
func (s *FlagProviderServer) checkIfFlagStatusIsCached(
ctx context.Context,
projectKey string,
req *providerpb.GetFlagRequest,
) (bool, error) {
environmentName := req.GetEnvironment()
flagName := req.GetFlagName()
cacheParams := cacheParameters{
ProjectKey: projectKey,
EnvironmentName: environmentName,
FlagName: flagName,
}
statusExists, err := s.providerCacheRepo.IsFlagStatusCached(ctx, &cacheParams)
if err != nil {
log.Printf(
"error occurred while checking if flag status is cached: %v",
err,
)
return false, ErrStatusCache
}
return statusExists, nil
}
// getCachedFlagStatus gets the value of the cached flag status.
func (s *FlagProviderServer) getCachedFlagStatus(
ctx context.Context,
projectKey string,
req *providerpb.GetFlagRequest,
) (bool, error) {
environmentName := req.GetEnvironment()
flagName := req.GetFlagName()
cacheParams := cacheParameters{
ProjectKey: projectKey,
EnvironmentName: environmentName,
FlagName: flagName,
}
cachedStatus, err := s.providerCacheRepo.GetFlagStatus(ctx, &cacheParams)
if err != nil {
if errors.Is(err, redis.Nil) {
log.Print("Could not find cached flag status")
return false, nil
}
return false, err
}
return cachedStatus, nil
}
func NewFlagProviderServer(
providerDataRepo DataRepository,
providerCacheRepo CacheRepository,
) *FlagProviderServer {
return &FlagProviderServer{
providerDataRepo: providerDataRepo,
providerCacheRepo: providerCacheRepo,
}
}