Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FFM-4572 - Update Go version, allow custom cache, add initalLoad flag, standardise segment and flags being set #98

Merged
merged 8 commits into from
Oct 5, 2022
4 changes: 2 additions & 2 deletions .drone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ name: default

steps:
- name: check
image: golang:1.16.2
image: golang:1.18.6
commands:
- make check
volumes:
- name: gopath
path: /go
- name: test
image: golang:1.16.2
image: golang:1.18.6
commands:
- make test
volumes:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.16.2
go-version: 1.18.6

- name: Check
run: make check
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ sec: $(GOPATH)/bin/gosec

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

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

$(GOPATH)/bin/oapi-codegen:
@echo "🔘 Installing oapicodegen ... (`date '+%H:%M:%S'`)"
@go get github.com/deepmap/oapi-codegen/cmd/oapi-codegen@v1.11.0
@go install github.com/deepmap/oapi-codegen/cmd/oapi-codegen@v1.11.0

PHONY+= tools
tools: $(GOPATH)/bin/golangci-lint $(GOPATH)/bin/golint $(GOPATH)/bin/gosec $(GOPATH)/bin/goimports $(GOPATH)/bin/oapi-codegen
Expand Down
12 changes: 10 additions & 2 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,19 @@ func NewCfClient(sdkKey string, options ...ConfigOption) (*CfClient, error) {
if sdkKey == "" {
return client, types.ErrSdkCantBeEmpty
}

var err error

lruCache, err := repository.NewLruCache(10000)
if err != nil {
return nil, err
}
client.repository = repository.New(lruCache)

if client.config != nil {
client.repository = repository.New(config.Cache)
}

client.evaluator, err = evaluation.NewEvaluator(client.repository, client, config.Logger)
if err != nil {
return nil, err
Expand Down Expand Up @@ -359,7 +367,7 @@ func (c *CfClient) retrieveFlags(ctx context.Context) error {
}

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

for _, segment := range *segments.JSON200 {
c.repository.SetSegment(segment)
c.repository.SetSegment(segment, true)
}
c.config.Logger.Info("Retrieving segments finished")
return nil
Expand Down
6 changes: 5 additions & 1 deletion client/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ func MakeStringFeatureConfigs(name, defaultVariation, offVariation, state string

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

return featureConfig
Expand Down
2 changes: 1 addition & 1 deletion docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.16-alpine as builder
FROM golang:1.18.6-alpine as builder
RUN apk update && apk add --no-cache make gcc musl-dev git ca-certificates && update-ca-certificates
WORKDIR /app

Expand Down
21 changes: 19 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/harness/ff-golang-server-sdk

go 1.16
go 1.18

require (
github.com/deepmap/oapi-codegen v1.11.0
Expand All @@ -16,7 +16,24 @@ require (
github.com/r3labs/sse v0.0.0-20201126193848-34e640891548
github.com/spaolacci/murmur3 v1.1.0
github.com/stretchr/testify v1.7.1
go.uber.org/multierr v1.6.0 // indirect
go.uber.org/zap v1.16.0
gopkg.in/cenkalti/backoff.v1 v1.1.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/ghodss/yaml v1.0.0 // indirect
github.com/go-openapi/jsonpointer v0.19.5 // indirect
github.com/go-openapi/swag v0.21.1 // indirect
github.com/hashicorp/go-cleanhttp v0.5.1 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
golang.org/x/net v0.0.0-20220513224357-95641704303c // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
)
24 changes: 14 additions & 10 deletions pkg/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ type Repository interface {
GetFlag(identifier string) (rest.FeatureConfig, error)
GetSegment(identifier string) (rest.Segment, error)

SetFlag(featureConfig rest.FeatureConfig)
SetSegment(segment rest.Segment)
SetFlag(featureConfig rest.FeatureConfig, initialLoad bool)
SetSegment(segment rest.Segment, initialLoad bool)

DeleteFlag(identifier string)
DeleteSegment(identifier string)
Expand Down Expand Up @@ -107,9 +107,11 @@ func (r FFRepository) GetSegment(identifier string) (rest.Segment, error) {
}

// SetFlag places a flag in the repository with the new value
func (r FFRepository) SetFlag(featureConfig rest.FeatureConfig) {
if r.isFlagOutdated(featureConfig) {
return
func (r FFRepository) SetFlag(featureConfig rest.FeatureConfig, initialLoad bool) {
if !initialLoad {
if r.isFlagOutdated(featureConfig) {
return
}
}
flagKey := formatFlagKey(featureConfig.Feature)
if r.storage != nil {
Expand All @@ -127,9 +129,11 @@ func (r FFRepository) SetFlag(featureConfig rest.FeatureConfig) {
}

// SetSegment places a segment in the repository with the new value
func (r FFRepository) SetSegment(segment rest.Segment) {
if r.isSegmentOutdated(segment) {
return
func (r FFRepository) SetSegment(segment rest.Segment, initialLoad bool) {
if !initialLoad {
if r.isSegmentOutdated(segment) {
return
}
}
segmentKey := formatSegmentKey(segment.Identifier)
if r.storage != nil {
Expand Down Expand Up @@ -202,9 +206,9 @@ func (r FFRepository) Close() {
}

func formatFlagKey(identifier string) string {
return "flags/" + identifier
return "flag/" + identifier
conormurray95 marked this conversation as resolved.
Show resolved Hide resolved
}

func formatSegmentKey(identifier string) string {
return "segments/" + identifier
return "target-segment/" + identifier
}
4 changes: 2 additions & 2 deletions stream/sse.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func (c *SSEClient) handleEvent(event Event) {
}

if response.JSON200 != nil {
c.repository.SetFlag(*response.JSON200)
c.repository.SetFlag(*response.JSON200, false)
}
}

Expand All @@ -159,7 +159,7 @@ func (c *SSEClient) handleEvent(event Event) {
return
}
if response.JSON200 != nil {
c.repository.SetSegment(*response.JSON200)
c.repository.SetSegment(*response.JSON200, false)
}
}
updateWithTimeout()
Expand Down
2 changes: 1 addition & 1 deletion test_wrapper/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/harness/ff-golang-server-sdk/test_wrapper

go 1.16
go 1.18

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

Expand Down
4 changes: 2 additions & 2 deletions tests/evaluator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ func TestEvaluator(t *testing.T) {
t.Error(err)
}
for _, flag := range fixture.Flags {
repo.SetFlag(flag)
repo.SetFlag(flag, false)
}
for _, segment := range fixture.Segments {
repo.SetSegment(segment)
repo.SetSegment(segment, false)
}

for _, testCase := range fixture.Tests {
Expand Down