Skip to content

Commit 9254d03

Browse files
stephenmcconkeyDave Johnston
authored and
Dave Johnston
committed
FFM-4572 - Update Go version, allow custom cache, add initalLoad flag, standardise segment and flags being set (harness#98)
* Update Go version in SDK * Add logic to allow user defined cache to be populated * Add flag for initial load so that oldflag check doesn't occur on first load * Update to use more recent ff-test-cases submodule * Pin version of Go lint as per golangci/golangci-lint-action#535
1 parent 6003b24 commit 9254d03

File tree

12 files changed

+60
-27
lines changed

12 files changed

+60
-27
lines changed

.drone.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ name: default
55

66
steps:
77
- name: check
8-
image: golang:1.16.2
8+
image: golang:1.18.6
99
commands:
1010
- make check
1111
volumes:
1212
- name: gopath
1313
path: /go
1414
- name: test
15-
image: golang:1.16.2
15+
image: golang:1.18.6
1616
commands:
1717
- make test
1818
volumes:

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
- name: Set up Go
1919
uses: actions/setup-go@v2
2020
with:
21-
go-version: 1.16.2
21+
go-version: 1.18.6
2222

2323
- name: Check
2424
run: make check

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ sec: $(GOPATH)/bin/gosec
7878

7979
$(GOPATH)/bin/golangci-lint:
8080
@echo "🔘 Installing golangci-lint... (`date '+%H:%M:%S'`)"
81-
@curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(GOPATH)/bin
81+
@curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(GOPATH)/bin v1.47.3
8282

8383
$(GOPATH)/bin/golint:
8484
@echo "🔘 Installing golint ... (`date '+%H:%M:%S'`)"
@@ -95,7 +95,7 @@ $(GOPATH)/bin/gosec:
9595

9696
$(GOPATH)/bin/oapi-codegen:
9797
@echo "🔘 Installing oapicodegen ... (`date '+%H:%M:%S'`)"
98-
@go get github.com/deepmap/oapi-codegen/cmd/[email protected]
98+
@go install github.com/deepmap/oapi-codegen/cmd/[email protected]
9999

100100
PHONY+= tools
101101
tools: $(GOPATH)/bin/golangci-lint $(GOPATH)/bin/golint $(GOPATH)/bin/gosec $(GOPATH)/bin/goimports $(GOPATH)/bin/oapi-codegen

client/client.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,19 @@ func NewCfClient(sdkKey string, options ...ConfigOption) (*CfClient, error) {
8686
if sdkKey == "" {
8787
return client, types.ErrSdkCantBeEmpty
8888
}
89+
90+
var err error
91+
8992
lruCache, err := repository.NewLruCache(10000)
9093
if err != nil {
9194
return nil, err
9295
}
9396
client.repository = repository.New(lruCache)
97+
98+
if client.config != nil {
99+
client.repository = repository.New(config.Cache)
100+
}
101+
94102
client.evaluator, err = evaluation.NewEvaluator(client.repository, client, config.Logger)
95103
if err != nil {
96104
return nil, err
@@ -359,7 +367,7 @@ func (c *CfClient) retrieveFlags(ctx context.Context) error {
359367
}
360368

361369
for _, flag := range *flags.JSON200 {
362-
c.repository.SetFlag(flag)
370+
c.repository.SetFlag(flag, true)
363371
}
364372
c.config.Logger.Info("Retrieving flags finished")
365373
return nil
@@ -383,7 +391,7 @@ func (c *CfClient) retrieveSegments(ctx context.Context) error {
383391
}
384392

385393
for _, segment := range *segments.JSON200 {
386-
c.repository.SetSegment(segment)
394+
c.repository.SetSegment(segment, true)
387395
}
388396
c.config.Logger.Info("Retrieving segments finished")
389397
return nil

client/helpers_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,11 @@ func MakeStringFeatureConfigs(name, defaultVariation, offVariation, state string
5353

5454
// If there are any PreReqs then we need to store them as flags as well.
5555
for _, x := range preReqs {
56-
featureConfig = append(featureConfig, MakeBoolFeatureConfig(x.Feature, "true", "false", x.Variations[0], nil))
56+
state := "off"
57+
if x.Variations[0] == "true" {
58+
state = "on"
59+
}
60+
featureConfig = append(featureConfig, MakeBoolFeatureConfig(x.Feature, "true", "false", state, nil))
5761
}
5862

5963
return featureConfig

docker/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM golang:1.16-alpine as builder
1+
FROM golang:1.18.6-alpine as builder
22
RUN apk update && apk add --no-cache make gcc musl-dev git ca-certificates && update-ca-certificates
33
WORKDIR /app
44

go.mod

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/harness/ff-golang-server-sdk
22

3-
go 1.16
3+
go 1.18
44

55
require (
66
github.com/deepmap/oapi-codegen v1.11.0
@@ -16,7 +16,24 @@ require (
1616
github.com/r3labs/sse v0.0.0-20201126193848-34e640891548
1717
github.com/spaolacci/murmur3 v1.1.0
1818
github.com/stretchr/testify v1.7.1
19-
go.uber.org/multierr v1.6.0 // indirect
2019
go.uber.org/zap v1.16.0
2120
gopkg.in/cenkalti/backoff.v1 v1.1.0
2221
)
22+
23+
require (
24+
github.com/davecgh/go-spew v1.1.1 // indirect
25+
github.com/ghodss/yaml v1.0.0 // indirect
26+
github.com/go-openapi/jsonpointer v0.19.5 // indirect
27+
github.com/go-openapi/swag v0.21.1 // indirect
28+
github.com/hashicorp/go-cleanhttp v0.5.1 // indirect
29+
github.com/josharian/intern v1.0.0 // indirect
30+
github.com/mailru/easyjson v0.7.7 // indirect
31+
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
32+
github.com/modern-go/reflect2 v1.0.2 // indirect
33+
github.com/pmezard/go-difflib v1.0.0 // indirect
34+
go.uber.org/atomic v1.7.0 // indirect
35+
go.uber.org/multierr v1.6.0 // indirect
36+
golang.org/x/net v0.0.0-20220513224357-95641704303c // indirect
37+
gopkg.in/yaml.v2 v2.4.0 // indirect
38+
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
39+
)

pkg/repository/repository.go

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ type Repository interface {
1313
GetFlag(identifier string) (rest.FeatureConfig, error)
1414
GetSegment(identifier string) (rest.Segment, error)
1515

16-
SetFlag(featureConfig rest.FeatureConfig)
17-
SetSegment(segment rest.Segment)
16+
SetFlag(featureConfig rest.FeatureConfig, initialLoad bool)
17+
SetSegment(segment rest.Segment, initialLoad bool)
1818

1919
DeleteFlag(identifier string)
2020
DeleteSegment(identifier string)
@@ -107,9 +107,11 @@ func (r FFRepository) GetSegment(identifier string) (rest.Segment, error) {
107107
}
108108

109109
// SetFlag places a flag in the repository with the new value
110-
func (r FFRepository) SetFlag(featureConfig rest.FeatureConfig) {
111-
if r.isFlagOutdated(featureConfig) {
112-
return
110+
func (r FFRepository) SetFlag(featureConfig rest.FeatureConfig, initialLoad bool) {
111+
if !initialLoad {
112+
if r.isFlagOutdated(featureConfig) {
113+
return
114+
}
113115
}
114116
flagKey := formatFlagKey(featureConfig.Feature)
115117
if r.storage != nil {
@@ -127,9 +129,11 @@ func (r FFRepository) SetFlag(featureConfig rest.FeatureConfig) {
127129
}
128130

129131
// SetSegment places a segment in the repository with the new value
130-
func (r FFRepository) SetSegment(segment rest.Segment) {
131-
if r.isSegmentOutdated(segment) {
132-
return
132+
func (r FFRepository) SetSegment(segment rest.Segment, initialLoad bool) {
133+
if !initialLoad {
134+
if r.isSegmentOutdated(segment) {
135+
return
136+
}
133137
}
134138
segmentKey := formatSegmentKey(segment.Identifier)
135139
if r.storage != nil {
@@ -202,9 +206,9 @@ func (r FFRepository) Close() {
202206
}
203207

204208
func formatFlagKey(identifier string) string {
205-
return "flags/" + identifier
209+
return "flag/" + identifier
206210
}
207211

208212
func formatSegmentKey(identifier string) string {
209-
return "segments/" + identifier
213+
return "target-segment/" + identifier
210214
}

stream/sse.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ func (c *SSEClient) handleEvent(event Event) {
134134
}
135135

136136
if response.JSON200 != nil {
137-
c.repository.SetFlag(*response.JSON200)
137+
c.repository.SetFlag(*response.JSON200, false)
138138
}
139139
}
140140

@@ -159,7 +159,7 @@ func (c *SSEClient) handleEvent(event Event) {
159159
return
160160
}
161161
if response.JSON200 != nil {
162-
c.repository.SetSegment(*response.JSON200)
162+
c.repository.SetSegment(*response.JSON200, false)
163163
}
164164
}
165165
updateWithTimeout()

test_wrapper/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/harness/ff-golang-server-sdk/test_wrapper
22

3-
go 1.16
3+
go 1.18
44

55
replace github.com/harness/ff-golang-server-sdk => ../
66

tests/evaluator_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,10 @@ func TestEvaluator(t *testing.T) {
8484
t.Error(err)
8585
}
8686
for _, flag := range fixture.Flags {
87-
repo.SetFlag(flag)
87+
repo.SetFlag(flag, false)
8888
}
8989
for _, segment := range fixture.Segments {
90-
repo.SetSegment(segment)
90+
repo.SetSegment(segment, false)
9191
}
9292

9393
for _, testCase := range fixture.Tests {

0 commit comments

Comments
 (0)