Skip to content

Commit

Permalink
feat: automatically generated instanceId (#174)
Browse files Browse the repository at this point in the history
  • Loading branch information
nunogois authored Jul 10, 2024
1 parent 620a0dc commit 7cf2646
Show file tree
Hide file tree
Showing 10 changed files with 13 additions and 79 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ go get github.com/Unleash/unleash-client-go
The easiest way to get started with Unleash is to initialize it early in your application code:

**Asynchronous initialization example:**

```go
import (
"github.com/Unleash/unleash-client-go/v4"
Expand Down Expand Up @@ -70,6 +71,10 @@ func init() {
}
```

#### instanceId

Starting from version 5.0.0, `instanceId` is automatically generated and can no longer be set manually.

#### Preloading feature toggles

If you'd like to prebake your application with feature toggles (maybe you're working without persistent storage, so Unleash's backup isn't available), you can replace the defaultStorage implementation with a BootstrapStorage. This allows you to pass in a reader to where data in the format of `/api/client/features` can be found.
Expand Down Expand Up @@ -273,8 +278,6 @@ you can add the following to your apps `go.mod`:
replace github.com/Unleash/unleash-client-go/v4 => ../unleash-client-go/
```



## Steps to release

- Update the clientVersion in `client.go`
Expand Down
6 changes: 3 additions & 3 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"strings"
"time"

"github.com/google/uuid"

"github.com/Unleash/unleash-client-go/v4/api"
"github.com/Unleash/unleash-client-go/v4/context"
"github.com/Unleash/unleash-client-go/v4/internal/constraints"
Expand Down Expand Up @@ -159,9 +161,7 @@ func NewClient(options ...ConfigOption) (*Client, error) {
return nil, fmt.Errorf("unleash client appName missing")
}

if uc.options.instanceId == "" {
uc.options.instanceId = generateInstanceId()
}
uc.options.instanceId = uuid.New().String()

uc.repository = newRepository(
repositoryOptions{
Expand Down
31 changes: 0 additions & 31 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ func TestClientWithoutListener(t *testing.T) {
client, err := NewClient(
WithUrl(mockerServer),
WithAppName(mockAppName),
WithInstanceId(mockInstanceId),
)
assert.Nil(err, "client should not return an error")

Expand All @@ -46,7 +45,6 @@ func TestClient_WithFallbackFunc(t *testing.T) {
gock.New(mockerServer).
Post("/client/register").
MatchHeader("UNLEASH-APPNAME", mockAppName).
MatchHeader("UNLEASH-INSTANCEID", mockInstanceId).
Reply(200)

gock.New(mockerServer).
Expand All @@ -65,7 +63,6 @@ func TestClient_WithFallbackFunc(t *testing.T) {
client, err := NewClient(
WithUrl(mockerServer),
WithAppName(mockAppName),
WithInstanceId(mockInstanceId),
WithListener(mockListener),
)

Expand All @@ -90,7 +87,6 @@ func TestClient_WithResolver(t *testing.T) {
gock.New(mockerServer).
Post("/client/register").
MatchHeader("UNLEASH-APPNAME", mockAppName).
MatchHeader("UNLEASH-INSTANCEID", mockInstanceId).
Reply(200)

gock.New(mockerServer).
Expand All @@ -109,7 +105,6 @@ func TestClient_WithResolver(t *testing.T) {
client, err := NewClient(
WithUrl(mockerServer),
WithAppName(mockAppName),
WithInstanceId(mockInstanceId),
WithListener(mockListener),
)

Expand Down Expand Up @@ -198,7 +193,6 @@ func TestClient_ListFeatures(t *testing.T) {
client, err := NewClient(
WithUrl(mockerServer),
WithAppName(mockAppName),
WithInstanceId(mockInstanceId),
WithListener(mockListener),
)

Expand Down Expand Up @@ -232,7 +226,6 @@ func TestClientWithProjectName(t *testing.T) {
client, err := NewClient(
WithUrl(mockerServer),
WithAppName(mockAppName),
WithInstanceId(mockInstanceId),
WithProjectName(projectName),
WithListener(mockListener),
)
Expand Down Expand Up @@ -265,7 +258,6 @@ func TestClientWithoutProjectName(t *testing.T) {
client, err := NewClient(
WithUrl(mockerServer),
WithAppName(mockAppName),
WithInstanceId(mockInstanceId),
WithListener(mockListener),
)

Expand Down Expand Up @@ -342,7 +334,6 @@ func TestClientWithVariantContext(t *testing.T) {
client, err := NewClient(
WithUrl(mockerServer),
WithAppName(mockAppName),
WithInstanceId(mockInstanceId),
WithListener(mockListener),
)

Expand Down Expand Up @@ -381,7 +372,6 @@ func TestClient_WithSegment(t *testing.T) {
gock.New(mockerServer).
Post("/client/register").
MatchHeader("UNLEASH-APPNAME", mockAppName).
MatchHeader("UNLEASH-INSTANCEID", mockInstanceId).
Reply(200)

feature := "feature-segment"
Expand Down Expand Up @@ -430,7 +420,6 @@ func TestClient_WithSegment(t *testing.T) {
client, err := NewClient(
WithUrl(mockerServer),
WithAppName(mockAppName),
WithInstanceId(mockInstanceId),
WithListener(mockListener),
)

Expand Down Expand Up @@ -466,7 +455,6 @@ func TestClient_WithNonExistingSegment(t *testing.T) {
gock.New(mockerServer).
Post("/client/register").
MatchHeader("UNLEASH-APPNAME", mockAppName).
MatchHeader("UNLEASH-INSTANCEID", mockInstanceId).
Reply(200)

feature := "feature-segment-non-existing"
Expand Down Expand Up @@ -508,7 +496,6 @@ func TestClient_WithNonExistingSegment(t *testing.T) {
client, err := NewClient(
WithUrl(mockerServer),
WithAppName(mockAppName),
WithInstanceId(mockInstanceId),
WithListener(mockListener),
)

Expand Down Expand Up @@ -545,7 +532,6 @@ func TestClient_WithMultipleSegments(t *testing.T) {
gock.New(mockerServer).
Post("/client/register").
MatchHeader("UNLEASH-APPNAME", mockAppName).
MatchHeader("UNLEASH-INSTANCEID", mockInstanceId).
Reply(200)

feature := "feature-segment-multiple"
Expand Down Expand Up @@ -612,7 +598,6 @@ func TestClient_WithMultipleSegments(t *testing.T) {
client, err := NewClient(
WithUrl(mockerServer),
WithAppName(mockAppName),
WithInstanceId(mockInstanceId),
WithListener(mockListener),
)

Expand Down Expand Up @@ -648,7 +633,6 @@ func TestClient_VariantShouldRespectConstraint(t *testing.T) {
gock.New(mockerServer).
Post("/client/register").
MatchHeader("UNLEASH-APPNAME", mockAppName).
MatchHeader("UNLEASH-INSTANCEID", mockInstanceId).
Reply(200)

feature := "feature-segment-multiple"
Expand Down Expand Up @@ -727,7 +711,6 @@ func TestClient_VariantShouldRespectConstraint(t *testing.T) {
client, err := NewClient(
WithUrl(mockerServer),
WithAppName(mockAppName),
WithInstanceId(mockInstanceId),
WithListener(mockListener),
)

Expand Down Expand Up @@ -767,7 +750,6 @@ func TestClient_VariantShouldFailWhenSegmentConstraintsDontMatch(t *testing.T) {
gock.New(mockerServer).
Post("/client/register").
MatchHeader("UNLEASH-APPNAME", mockAppName).
MatchHeader("UNLEASH-INSTANCEID", mockInstanceId).
Reply(200)

feature := "feature-segment-multiple"
Expand Down Expand Up @@ -846,7 +828,6 @@ func TestClient_VariantShouldFailWhenSegmentConstraintsDontMatch(t *testing.T) {
client, err := NewClient(
WithUrl(mockerServer),
WithAppName(mockAppName),
WithInstanceId(mockInstanceId),
WithListener(mockListener),
)

Expand Down Expand Up @@ -949,7 +930,6 @@ func TestClient_ShouldFavorStrategyVariantOverFeatureVariant(t *testing.T) {
client, err := NewClient(
WithUrl(mockerServer),
WithAppName(mockAppName),
WithInstanceId(mockInstanceId),
WithListener(mockListener),
)

Expand Down Expand Up @@ -1049,7 +1029,6 @@ func TestClient_ShouldReturnOldVariantForNonMatchingStrategyVariant(t *testing.T
client, err := NewClient(
WithUrl(mockerServer),
WithAppName(mockAppName),
WithInstanceId(mockInstanceId),
WithListener(mockListener),
)

Expand All @@ -1075,7 +1054,6 @@ func TestClient_VariantFromEnabledFeatureWithNoVariants(t *testing.T) {
gock.New(mockerServer).
Post("/client/register").
MatchHeader("UNLEASH-APPNAME", mockAppName).
MatchHeader("UNLEASH-INSTANCEID", mockInstanceId).
Reply(200)

feature := "feature-no-variants"
Expand Down Expand Up @@ -1112,7 +1090,6 @@ func TestClient_VariantFromEnabledFeatureWithNoVariants(t *testing.T) {
client, err := NewClient(
WithUrl(mockerServer),
WithAppName(mockAppName),
WithInstanceId(mockInstanceId),
WithListener(mockListener),
)

Expand Down Expand Up @@ -1152,7 +1129,6 @@ func TestGetVariantWithFallbackVariantWhenFeatureDisabled(t *testing.T) {
gock.New(mockerServer).
Post("/client/register").
MatchHeader("UNLEASH-APPNAME", mockAppName).
MatchHeader("UNLEASH-INSTANCEID", mockInstanceId).
Reply(200)

feature := "feature-disabled"
Expand Down Expand Up @@ -1183,7 +1159,6 @@ func TestGetVariantWithFallbackVariantWhenFeatureDisabled(t *testing.T) {
client, err := NewClient(
WithUrl(mockerServer),
WithAppName(mockAppName),
WithInstanceId(mockInstanceId),
WithListener(&NoopListener{}),
)

Expand Down Expand Up @@ -1220,7 +1195,6 @@ func TestGetVariantWithFallbackVariantWhenFeatureEnabledButNoVariants(t *testing
gock.New(mockerServer).
Post("/client/register").
MatchHeader("UNLEASH-APPNAME", mockAppName).
MatchHeader("UNLEASH-INSTANCEID", mockInstanceId).
Reply(200)

feature := "feature-no-variants"
Expand Down Expand Up @@ -1251,7 +1225,6 @@ func TestGetVariantWithFallbackVariantWhenFeatureEnabledButNoVariants(t *testing
client, err := NewClient(
WithUrl(mockerServer),
WithAppName(mockAppName),
WithInstanceId(mockInstanceId),
WithListener(&NoopListener{}),
)

Expand Down Expand Up @@ -1292,7 +1265,6 @@ func TestGetVariantWithFallbackVariantWhenFeatureDoesntExist(t *testing.T) {
gock.New(mockerServer).
Post("/client/register").
MatchHeader("UNLEASH-APPNAME", mockAppName).
MatchHeader("UNLEASH-INSTANCEID", mockInstanceId).
Reply(200)

feature := "feature-no-variants"
Expand All @@ -1309,7 +1281,6 @@ func TestGetVariantWithFallbackVariantWhenFeatureDoesntExist(t *testing.T) {
client, err := NewClient(
WithUrl(mockerServer),
WithAppName(mockAppName),
WithInstanceId(mockInstanceId),
WithListener(&NoopListener{}),
)

Expand Down Expand Up @@ -1346,7 +1317,6 @@ func TestGetVariant_FallbackVariantFeatureEnabledSettingIsLeftUnchanged(t *testi
gock.New(mockerServer).
Post("/client/register").
MatchHeader("UNLEASH-APPNAME", mockAppName).
MatchHeader("UNLEASH-INSTANCEID", mockInstanceId).
Reply(200)

enabledFeatureNoVariants := "enabled-feature"
Expand Down Expand Up @@ -1391,7 +1361,6 @@ func TestGetVariant_FallbackVariantFeatureEnabledSettingIsLeftUnchanged(t *testi
client, err := NewClient(
WithUrl(mockerServer),
WithAppName(mockAppName),
WithInstanceId(mockInstanceId),
WithListener(&NoopListener{}),
)

Expand Down
8 changes: 0 additions & 8 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,6 @@ func WithEnvironment(env string) ConfigOption {
}
}

// WithInstanceId specifies the instance identifier of the current instance. If not provided,
// one will be generated based on various parameters such as current user and hostname.
func WithInstanceId(instanceId string) ConfigOption {
return func(o *configOption) {
o.instanceId = instanceId
}
}

// WithUrl specifies the url of the unleash server the user is connecting to.
func WithUrl(url string) ConfigOption {
return func(o *configOption) {
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/Unleash/unleash-client-go/v4
require (
github.com/Masterminds/semver/v3 v3.1.1
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/google/uuid v1.6.0
github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/objx v0.1.1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ github.com/Masterminds/semver/v3 v3.1.1 h1:hLg3sBzpNErnxhQtUy/mmLR2I9foDujNK030I
github.com/Masterminds/semver/v3 v3.1.1/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32 h1:W6apQkHrMkS0Muv8G/TipAy/FJl/rCYT0+EuS8+Z0z4=
github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32/go.mod h1:9wM+0iRr9ahx58uYLpLIr5fm8diHn0JbqRycJi6w0Ms=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down
Loading

0 comments on commit 7cf2646

Please sign in to comment.