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

feat: add feature flag go sdk #711

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions featureflags/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@

# Feature Flags Package

A Go package for managing feature flags using Flagsmith as the provider.

## Prerequisites

Before using this package, ensure you have set the following environment variable:

```bash
export FLAGSMITH_SERVER_SIDE_ENVIRONMENT_KEY=your-api-key
```

## Usage Example

Here's a basic example of how to use the feature flags package:

```go
package main

import (
"fmt"
"log"

"github.com/rudderlabs/rudder-go-kit/featureflags"
)

func main() {
// Set default traits (optional)
featureflags.SetDefaultTraits(map[string]string{
"exampleTrait": "exampleTraitValue",
})

// Check if a feature is enabled
isEnabled, err := featureflags.IsFeatureEnabled("testWs", "testFeature")
if err != nil {
log.Fatal(err)
}
fmt.Println(isEnabled)
}
```

For more detailed examples and implementation details, refer to the example directory.
43 changes: 43 additions & 0 deletions featureflags/cache/cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package cache

import (
"time"

"github.com/rudderlabs/rudder-go-kit/featureflags/provider"
)

// CacheEntry represents a cached item with its value and last updated timestamp
type CacheEntry struct {
Value map[string]*provider.FeatureValue
LastUpdated time.Time
}

// CacheGetResult extends CacheEntry with staleness information
type CacheGetResult struct {
*CacheEntry
IsStale bool
}

// Cache defines the interface for cache operations
type Cache interface {
Get(key string) (*CacheGetResult, bool)
Set(key string, value map[string]*provider.FeatureValue) (time.Time, error)
Delete(key string) error
Clear() error
IsEnabled() bool
}

// CacheError represents cache-specific errors
type CacheError struct {
Message string
}

func (e *CacheError) Error() string {
return e.Message
}

// CacheConfig holds configuration for the cache
type CacheConfig struct {
Enabled bool
TTLInSeconds int64
}
70 changes: 70 additions & 0 deletions featureflags/cache/cache_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package cache_test

import (
"testing"
"time"

"github.com/rudderlabs/rudder-go-kit/featureflags/cache"
"github.com/rudderlabs/rudder-go-kit/featureflags/provider"
)

func TestMemoryCache(t *testing.T) {
config := cache.CacheConfig{
Enabled: true,
TTLInSeconds: 1,
}

c := cache.NewMemoryCache(config)

testValue := map[string]*provider.FeatureValue{
"key1": {
Value: "value1",
},
}

// Test Set and Get
setTime, err := c.Set("key1", testValue)
if err != nil {
t.Errorf("Failed to set cache: %v", err)
}

result, ok := c.Get("key1")
if !ok {
t.Error("Expected cache hit, got miss")
}
if result.LastUpdated != setTime {
t.Error("LastUpdated time mismatch")
}

// Test staleness
time.Sleep(2 * time.Second)
result, ok = c.Get("key1")

Check failure on line 41 in featureflags/cache/cache_test.go

View workflow job for this annotation

GitHub Actions / lint

SA4006: this value of `result` is never used (staticcheck)
if !ok {
t.Error("Expected stale cache entry")
}

// Test Delete
err = c.Delete("key1")
if err != nil {
t.Errorf("Failed to delete cache: %v", err)
}

result, ok = c.Get("key1")

Check failure on line 52 in featureflags/cache/cache_test.go

View workflow job for this annotation

GitHub Actions / lint

SA4006: this value of `result` is never used (staticcheck)
if ok {
t.Error("Expected cache miss after delete")
}

// Test Clear
c.Set("key1", testValue)

Check failure on line 58 in featureflags/cache/cache_test.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `c.Set` is not checked (errcheck)
c.Set("key2", testValue)

Check failure on line 59 in featureflags/cache/cache_test.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `c.Set` is not checked (errcheck)

err = c.Clear()
if err != nil {
t.Errorf("Failed to clear cache: %v", err)
}

result, ok = c.Get("key1")

Check failure on line 66 in featureflags/cache/cache_test.go

View workflow job for this annotation

GitHub Actions / lint

SA4006: this value of `result` is never used (staticcheck)
if ok {
t.Error("Expected cache miss after clear")
}
}
91 changes: 91 additions & 0 deletions featureflags/cache/memory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package cache

import (
"sync"
"time"

"github.com/rudderlabs/rudder-go-kit/featureflags/provider"
)

type MemoryCache struct {
entries map[string]CacheEntry
config CacheConfig
mu sync.RWMutex
}

func NewMemoryCache(config CacheConfig) *MemoryCache {
return &MemoryCache{
entries: make(map[string]CacheEntry),
config: config,
}
}

func (c *MemoryCache) Get(key string) (*CacheGetResult, bool) {
if !c.IsEnabled() {
return nil, false
}

c.mu.RLock()
defer c.mu.RUnlock()

entry, exists := c.entries[key]
if !exists {
return nil, false
}

isStale := false
if c.config.TTLInSeconds > 0 {
expirationTime := entry.LastUpdated.Add(time.Duration(c.config.TTLInSeconds) * time.Second)
isStale = time.Now().After(expirationTime)
}

return &CacheGetResult{
CacheEntry: &entry,
IsStale: isStale,
}, true
}

func (c *MemoryCache) Set(key string, value map[string]*provider.FeatureValue) (time.Time, error) {
if !c.IsEnabled() {
return time.Time{}, &CacheError{Message: "Cache is disabled"}
}

c.mu.Lock()
defer c.mu.Unlock()

now := time.Now()
c.entries[key] = CacheEntry{
Value: value,
LastUpdated: now,
}

return now, nil
}

func (c *MemoryCache) Delete(key string) error {
if !c.IsEnabled() {
return &CacheError{Message: "Cache is disabled"}
}

c.mu.Lock()
defer c.mu.Unlock()

delete(c.entries, key)
return nil
}

func (c *MemoryCache) Clear() error {
if !c.IsEnabled() {
return &CacheError{Message: "Cache is disabled"}
}

c.mu.Lock()
defer c.mu.Unlock()

c.entries = make(map[string]CacheEntry)
return nil
}

func (c *MemoryCache) IsEnabled() bool {
return c.config.Enabled
}
Loading
Loading