Skip to content

Commit

Permalink
event: adds feature flagger check with user ID context
Browse files Browse the repository at this point in the history
  • Loading branch information
Seklfreak committed Aug 23, 2019
1 parent bf46b06 commit 7d8c713
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 5 deletions.
2 changes: 2 additions & 0 deletions events/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/go-redis/redis"
"github.com/jinzhu/gorm"
"gitlab.com/Cacophony/go-kit/discord"
"gitlab.com/Cacophony/go-kit/featureflag"
"gitlab.com/Cacophony/go-kit/interfaces"
"gitlab.com/Cacophony/go-kit/paginator"
"gitlab.com/Cacophony/go-kit/state"
Expand Down Expand Up @@ -83,6 +84,7 @@ type Event struct {
httpClient *http.Client
questionnaire *Questionnaire
storage *Storage
featureFlagger *featureflag.FeatureFlagger
}

// BucketType specifies the scope of a bucket
Expand Down
9 changes: 9 additions & 0 deletions events/event_featureflag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package events

func (e *Event) IsEnabled(key string, fallback bool) bool {
if e.FeatureFlagger() == nil {
return fallback
}

return e.FeatureFlagger().IsEnabledFor(key, fallback, e.UserID)
}
9 changes: 9 additions & 0 deletions events/event_values.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net/http"

"github.com/jinzhu/gorm"
"gitlab.com/Cacophony/go-kit/featureflag"

"github.com/go-redis/redis"

Expand Down Expand Up @@ -141,3 +142,11 @@ func (e *Event) WithQuestionnaire(questionnaire *Questionnaire) {
func (e *Event) Questionnaire() *Questionnaire {
return e.questionnaire
}

func (e *Event) WithFeatureFlagger(featureFlagger *featureflag.FeatureFlagger) {
e.featureFlagger = featureFlagger
}

func (e *Event) FeatureFlagger() *featureflag.FeatureFlagger {
return e.featureFlagger
}
16 changes: 14 additions & 2 deletions featureflag/featureflag.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import (
"net/http"
"time"

unleash "github.com/Unleash/unleash-client-go"
unleash "github.com/Unleash/unleash-client-go/v3"
"github.com/Unleash/unleash-client-go/v3/context"
)

// Config represents a Feature Flagger Configuration
Expand Down Expand Up @@ -44,7 +45,7 @@ func New(config *Config) (*FeatureFlagger, error) {
}, nil
}

// IsEnabled checks if a feature flag is enabled
// IsEnabled checks if a feature flag is enabled globally
func (ff *FeatureFlagger) IsEnabled(key string, fallback bool) bool {
if ff.unleashClient == nil {
return fallback
Expand All @@ -53,6 +54,17 @@ func (ff *FeatureFlagger) IsEnabled(key string, fallback bool) bool {
return ff.unleashClient.IsEnabled(key, unleash.WithFallback(fallback))
}

// IsEnabled checks if a feature flag is enabled for a specific UserID
func (ff *FeatureFlagger) IsEnabledFor(key string, fallback bool, userID string) bool {
if ff.unleashClient == nil {
return fallback
}

return ff.unleashClient.IsEnabled(key, unleash.WithFallback(fallback), unleash.WithContext(context.Context{
UserId: userID,
}))
}

// UnleashListener is our listener for Unleash events
type UnleashListener struct{}

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/Masterminds/semver v1.4.2 // indirect
github.com/Masterminds/sprig v2.20.0+incompatible
github.com/Seklfreak/polr-go v0.0.0-20190324143256-a87fed130937
github.com/Unleash/unleash-client-go v0.0.0-20190225211619-9febc6ff26f4
github.com/Unleash/unleash-client-go/v3 v3.0.0
github.com/aws/aws-sdk-go v1.19.46 // indirect
github.com/bwmarrin/discordgo v0.16.1-0.20190608205439-347a4f69b0b5
github.com/certifi/gocertifi v0.0.0-20190506164543-d2eda7129713 // indirect
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ github.com/Seklfreak/polr-go v0.0.0-20190324143256-a87fed130937 h1:i7uyK5VEr5L/Z
github.com/Seklfreak/polr-go v0.0.0-20190324143256-a87fed130937/go.mod h1:eoGl/sGHUD/1hNQEHPvNunsRryc3c4Im57OavEzSxiw=
github.com/Shopify/sarama v1.19.0/go.mod h1:FVkBWblsNy7DGZRfXLU0O9RCGt5g3g3yEuWXgklEdEo=
github.com/Shopify/toxiproxy v2.1.4+incompatible/go.mod h1:OXgGpZ6Cli1/URJOF1DMxUHB2q5Ap20/P/eIdh4G0pI=
github.com/Unleash/unleash-client-go v0.0.0-20190225211619-9febc6ff26f4 h1:ZxgumCXfW2XHYgZQD/iwl7qCy6uziWsSJDEdQ05hTXE=
github.com/Unleash/unleash-client-go v0.0.0-20190225211619-9febc6ff26f4/go.mod h1:89lPmFeGRU6Xv5kYZnN1ByXulB4vHW+UepUYSm8s8ws=
github.com/Unleash/unleash-client-go/v3 v3.0.0 h1:J2Ak/0/9atePwIzUccNsZRmZNLyyB1sD5DP4C10hRdk=
github.com/Unleash/unleash-client-go/v3 v3.0.0/go.mod h1:qOPteDX5tImaQlhQOrDtFX3B7WTJuZz53Py88tEV7v8=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/apache/thrift v0.12.0/go.mod h1:cp2SuWMxlEZw2r+iP2GNCdIi4C1qmUzdZFSVb+bacwQ=
Expand Down Expand Up @@ -207,6 +207,8 @@ github.com/prometheus/procfs v0.0.0-20190117184657-bf6a532e95b1/go.mod h1:c3At6R
github.com/rcrowley/go-metrics v0.0.0-20181016184325-3113b8401b8a/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4=
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72 h1:qLC7fQah7D6K1B0ujays3HV9gkFtllcxhzImRR7ArPQ=
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
github.com/streadway/amqp v0.0.0-20190404075320-75d898a42a94 h1:0ngsPmuP6XIjiFRNFYlvKwSr5zff2v+uPHaffZ6/M4k=
github.com/streadway/amqp v0.0.0-20190404075320-75d898a42a94/go.mod h1:AZpEONHx3DKn8O/DFsRAY58/XVQiIPMTMB1SddzLXVw=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down

0 comments on commit 7d8c713

Please sign in to comment.