flagship

package module
v0.0.14 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 24, 2022 License: MIT Imports: 12 Imported by: 0

README

flagship - Easily use feature-flags to ship code, with DynamoDB.

GoDoc

Install

go get github.com/joerdav/flagship@latest

Example

package main

import "github.com/joerdav/flagship"

func main() {
		s, err := flagship.New(context.Background())
		if err != nil {
			t.Errorf("unexpected error got %v", err)
		}
		if s.Bool(context.Background(), "newfeature") {
			log.Println("a new feature")
		} else {
			log.Println("old code")
		}
}

Documentation

Overview

A package for retreiving feature flags from a dynamo document.

Retrieving a boolean flag:

s, err := flagship.New(context.Background(), flagship.WithTableName(tableName))
if err != nil {
	t.Errorf("unexpected error got %v", err)
}
if s.Bool(context.Background(), "newfeature") {
	// New Code
} else {
	// Old code
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetHash added in v0.0.7

func GetHash(ctx context.Context, key string, hashKey io.Reader) uint

Types

type BoolFeatureStore added in v0.0.3

type BoolFeatureStore interface {
	// Bool returns the state of the feature flag with the key of `key`:
	// If the feature is missing from the table then always returns false.
	// Example:
	// {
	//     "features": {
	//         "newFeature": true
	//     }
	// }
	//	if s.Bool(context.Background(), "newfeature") {
	//		// New Code
	//	} else {
	//		// Old code
	//	}
	Bool(ctx context.Context, key string) bool
	// All returns the state containing all feature flags
	AllBools(ctx context.Context) map[string]bool
}

BoolFeatureStore defines the interface for accessing boolean typed feature flags from some source.

type FeatureStore

type FeatureStore interface {
	BoolFeatureStore
	ThrottleFeatureStore
}

FeatureStore is an aggregate interface for accessing all supported types of feature flag.

func New

func New(ctx context.Context, opts ...Option) (FeatureStore, error)

New constructs a new instance of the feature store client. Optionally accepts Option types as a variadic parameter:

s, err := flagship.New(context.Background(), flagship.WithClient(client))

type Option

type Option func(*featureStoreConfig)

Option is a function that can modify internal config.

func WithClient

func WithClient(client *dynamodb.Client) Option

WithClient allows modification of the dynamo client used. The default value is constructed using default credentials.

s, err := flagship.New(context.Background(), flagship.WithClient(client))

func WithClock

func WithClock(clock func() time.Time) Option

WithClock allows the overriding of the function used to get current time. The default value will be `time.Now`.

s, err := flagship.New(context.Background(), flagship.WithClock(func() time.Time { return time.Time{} }))

func WithLogger added in v0.0.14

func WithLogger(logger *log.Logger) Option

WithLogger allows the logging of flagship internals The default value is nil

s, err := flagship.New(context.Background(), flagship.WithLogger(logger))

func WithRecordName

func WithRecordName(recordName string) Option

WithRecordName allows modification of the AWS DynamoDB partition key. The default value is "features".

s, err := flagship.New(context.Background(), flagship.WithRecordName("features1"))

func WithRegion

func WithRegion(region string) Option

WithRegion allows modification of the AWS Region in which the dynamo table resides. The default value will rely on the AWS Go SDK to find the correct region.

s, err := flagship.New(context.Background(), flagship.WithRegion("eu-west-1"))

func WithTTL

func WithTTL(ttl time.Duration) Option

WithTTL allows modification of the cache expiry for features. The default value is 30 seconds.

s, err := flagship.New(context.Background(), flagship.WithTTL(1 * time.Hour))

func WithTableName

func WithTableName(tableName string) Option

WithTableName allows modification of the AWS DynamoDB table name. The default value is "featureFlagStore".

s, err := flagship.New(context.Background(), flagship.WithTableName("feature-table"))

type ThrottleFeatureStore added in v0.0.3

type ThrottleFeatureStore interface {
	// ThrottleAllow returns whether a given hash key is bucketed.
	// If the feature is missing from the table then always returns false.
	// Example:
	// {
	//     "throttles": {
	//         "newThrottleFeature": {
	//             // whitelist is an optional list of hashes that will always be bucketed.
	//             "whitelist":[10, 3321],
	//             // probability is the likelihood that a hash is bucketed as a percentage.
	//             // value is truncated to 2dp
	//             "probability": 2.5
	//             // forceRejectAll is an optional flag to force the rejection of all the traffic
	//             "forceRejectAll": true
	//         }
	//     }
	// }
	//	if s.ThrottleAllow(context.Background(), "newThrottleFeature", strings.NewReader("some hash")) {
	//		// New Code
	//	} else {
	//		// Old code
	//	}
	ThrottleAllow(ctx context.Context, key string, hashKey io.Reader) bool
	// GetHash returns the hash that would be bucketed in ThrottleAllow:
	//	h := s.GetHash(context.Background(), "newThrottleFeature", strings.NewReader("some hash")) {
	GetHash(ctx context.Context, key string, hashKey io.Reader) uint
}

ThrottleFeatureStore defines the interface for accessing a feature flag that needs bucketing.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL