Skip to content

Commit

Permalink
feat: use atomic pointer for global feature flag client (#332)
Browse files Browse the repository at this point in the history
  • Loading branch information
cbosss authored May 30, 2022
1 parent 5059af0 commit 2b6a48f
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 10 deletions.
20 changes: 10 additions & 10 deletions featureflag/global.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
package featureflag

import (
"sync"

"github.com/sirupsen/logrus"
"go.uber.org/atomic"
"unsafe"
)

var globalLock sync.Mutex
var globalClient Client = MockClient{}
// See https://blog.dubbelboer.com/2015/08/23/rwmutex-vs-atomicvalue-vs-unsafepointer.html
var (
defaultClient Client = MockClient{}
globalClient = atomic.NewUnsafePointer(unsafe.Pointer(&defaultClient))
)

func SetGlobalClient(client Client) {
if client == nil {
return
}
globalLock.Lock()
globalClient = client
globalLock.Unlock()
globalClient.Store(unsafe.Pointer(&client))
}

func GetGlobalClient() Client {
globalLock.Lock()
defer globalLock.Unlock()
return globalClient
c := (*Client)(globalClient.Load())
return *c
}

// Init will initialize global client with a launch darkly client
Expand Down
16 changes: 16 additions & 0 deletions featureflag/global_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package featureflag

import (
"github.com/stretchr/testify/require"
"testing"
)

func TestGlobalAccess(t *testing.T) {
// initial value should be default
require.Equal(t, defaultClient, GetGlobalClient())

// setting new global should be reflected
n := &ldClient{}
SetGlobalClient(n)
require.Equal(t, n, GetGlobalClient())
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ require (
github.com/tidwall/pretty v1.0.1 // indirect
github.com/xtgo/uuid v0.0.0-20140804021211-a0b114877d4c // indirect
go.mongodb.org/mongo-driver v1.9.0
go.uber.org/atomic v1.9.0
golang.org/x/net v0.0.0-20200813134508-3edf25e44fcc
gopkg.in/DataDog/dd-trace-go.v1 v1.34.0
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,8 @@ go.mongodb.org/mongo-driver v1.9.0/go.mod h1:0sQWfOeY63QTntERDJJ/0SuKK0T1uVSgKCu
go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=
go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8=
go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE=
go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0=
go.uber.org/zap v1.10.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
Expand Down

0 comments on commit 2b6a48f

Please sign in to comment.