• Stars
    star
    135
  • Rank 269,297 (Top 6 %)
  • Language
    Go
  • License
    Apache License 2.0
  • Created almost 8 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Unleash client SDK for Go

Build Status GoDoc Go Report Card Coverage Status

unleash-client-go

Unleash Client for Go. Read more about the Unleash project

Version 3.x of the client requires unleash-server v3.x or higher.

Go Version

The client is currently tested against Go 1.10.x and 1.13.x. These versions will be updated as new versions of Go are released.

The client may work on older versions of Go as well, but is not actively tested.

Getting started

1. Install unleash-client-go

To install the latest version of the client use:

go get github.com/Unleash/unleash-client-go/v3

If you are still using Unleash Server v2.x.x, then you should use:

go get github.com/Unleash/unleash-client-go

2. Initialize unleash

The easiest way to get started with Unleash is to initialize it early in your application code:

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

func init() {
	unleash.Initialize(
		unleash.WithListener(&unleash.DebugListener{}),
		unleash.WithAppName("my-application"),
		unleash.WithUrl("http://unleash.herokuapp.com/api/"),
		unleash.WithCustomHeaders(http.Header{"Authorization": {"<API token>"}}),
	)
}

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.

Bootstrapping from file

Bootstrapping from file on disk is then done using something similar to:

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

func init() {
	myBootstrap := os.Open("bootstrapfile.json") // or wherever your file is located at runtime
	// BootstrapStorage handles the case where Reader is nil
	unleash.Initialize(
		unleash.WithListener(&unleash.DebugListener{}),
		unleash.WithAppName("my-application"),
		unleash.WithUrl("http://unleash.herokuapp.com/api/"),
		unleash.WithStorage(&BootstrapStorage{Reader: myBootstrap})
	)
}

Bootstrapping from S3

Bootstrapping from S3 is then done by downloading the file using the AWS library and then passing in a Reader to the just downloaded file:

import (
	"github.com/Unleash/unleash-client-go/v3"
	"github.com/aws/aws-sdk-go-v2/aws"
	"github.com/aws/aws-sdk-go-v2/config"
	"github.com/aws/aws-sdk-go-v2/service/s3"
)

func init() {
	// Load the shared AWS config
	cfg, err := config.LoadDefaultConfig(context.TODO())
	if err != nil {
		log.Fatal(err)
	}

	// Create an S3 client
	client := s3.NewFromConfig(cfg)

	obj, err := client.GetObject(context.TODO(), &s3.GetObjectInput{
		Bucket: aws.String("YOURBUCKET"),
		Key:    aws.String("YOURKEY"),
	})

	if err != nil {
		log.Fatal(err)
	}

	reader := obj.Body
	defer reader.Close()

	// BootstrapStorage handles the case where Reader is nil
	unleash.Initialize(
		unleash.WithListener(&unleash.DebugListener{}),
		unleash.WithAppName("YOURAPPNAME"),
		unleash.WithUrl("YOURINSTANCE_URL"),
		unleash.WithStorage(&BootstrapStorage{Reader: reader})
	)
}

Bootstrapping from Google

Since the Google Cloud Storage API returns a Reader, implementing a Bootstrap from GCS is done using something similar to

import (
	"github.com/Unleash/unleash-client-go/v3"
	"cloud.google.com/go/storage"
)

func init() {
	ctx := context.Background() // Configure Google Cloud context
	client, err := storage.NewClient(ctx) // Configure your client
	if err != nil {
		// TODO: Handle error.
	}
	defer client.Close()

	// Fetch the bucket, then object and then create a reader
	reader := client.Bucket(bucketName).Object("my-bootstrap.json").NewReader(ctx)

	// BootstrapStorage handles the case where Reader is nil
	unleash.Initialize(
		unleash.WithListener(&unleash.DebugListener{}),
		unleash.WithAppName("my-application"),
		unleash.WithUrl("http://unleash.herokuapp.com/api/"),
		unleash.WithStorage(&unleash.BootstrapStorage{Reader: reader})
	)
}

3. Use unleash

After you have initialized the unleash-client you can easily check if a feature toggle is enabled or not.

unleash.IsEnabled("app.ToggleX")

4. Stop unleash

To shut down the client (turn off the polling) you can simply call the destroy-method. This is typically not required.

unleash.Close()

Built in activation strategies

The Go client comes with implementations for the built-in activation strategies provided by unleash.

  • DefaultStrategy
  • UserIdStrategy
  • FlexibleRolloutStrategy
  • GradualRolloutUserIdStrategy
  • GradualRolloutSessionIdStrategy
  • GradualRolloutRandomStrategy
  • RemoteAddressStrategy
  • ApplicationHostnameStrategy

Read more about activation strategies in the docs.

Unleash context

In order to use some of the common activation strategies you must provide an unleash-context. This client SDK allows you to send in the unleash context as part of the isEnabled call:

ctx := context.Context{
	UserId:        "123",
	SessionId:     "some-session-id",
	RemoteAddress: "127.0.0.1",
}

unleash.IsEnabled("someToggle", unleash.WithContext(ctx))

Caveat

This client uses go routines to report several events and doesn't drain the channel by default. So you need to either register a listener using WithListener or drain the channel "manually" (demonstrated in this example).

Feature Resolver

FeatureResolver is a FeatureOption used in IsEnabled via the WithResolver.

The FeatureResolver can be used to provide a feature instance in a different way than the client would normally retrieve it. This alternative resolver can be useful if you already have the feature instance and don't want to incur the cost to retrieve it from the repository.

An example of its usage is below:

ctx := context.Context{
	UserId:        "123",
	SessionId:     "some-session-id",
	RemoteAddress: "127.0.0.1",
}

// the FeatureResolver function that will be passed into WithResolver
resolver := func(featureName string) *api.Feature {
    if featureName == "someToggle" {
        // Feature being created in place for sake of example, but it is preferable an existing feature instance is used
        return &api.Feature{
            Name:        "someToggle",
            Description: "Example of someToggle",
            Enabled:     true,
            Strategies: []api.Strategy{
                {
                    Id:   1,
                    Name: "default",
                },
            },
            CreatedAt:  time.Time{},
            Strategy:   "default-strategy",
        }
    } else {
        // it shouldn't reach this block because the name will match above "someToggle" for this example
        return nil
    }
}

// This would return true because the matched strategy is default and the feature is Enabled 
unleash.IsEnabled("someToggle", unleash.WithContext(ctx), unleash.WithResolver(resolver))

Development

Steps to release

  • Update the clientVersion in client.go
  • Tag the repository with the new tag

Adding client specifications

In order to make sure the unleash clients uphold their contract, we have defined a set of client specifications that define this contract. These are used to make sure that each unleash client at any time adhere to the contract, and define a set of functionality that is core to unleash. You can view the client specifications here.

In order to make the tests run please do the following steps.

// in repository root
// testdata is gitignored
mkdir testdata
cd testdata
git clone https://github.com/Unleash/client-specification.git

Requirements:

  • make
  • golint (go get -u golang.org/x/lint/golint)

Run tests:

make

Run lint check:

make lint

Run code-style checks:(currently failing)

make strict-check

Run race-tests:

make test-race

More Repositories

1

unleash

Open-source feature management solution built for developers.
TypeScript
10,934
star
2

unleash-client-node

Unleash client SDK for Node.js
TypeScript
212
star
3

unleash-client-java

Unleash client SDK for Java
Java
118
star
4

unleash-docker

Docker container for unleash
Shell
95
star
5

unleash-client-python

Unleash client SDK for Python πŸ’‘πŸ’‘πŸ’‘
Python
83
star
6

unleash-client-dotnet

Unleash client SDK for .NET
C#
79
star
7

unleash-client-ruby

Unleash client SDK for Ruby
Ruby
58
star
8

unleash-client-php

Unleash client SDK for PHP
PHP
55
star
9

unleash-proxy

Unleash Proxy is used to safely integrate frontend application with Unleash in a secure and scaleable way.
TypeScript
48
star
10

unleash-edge

Rust
45
star
11

unleash-proxy-client-js

A browser client that can be used together with the unleash-proxy.
TypeScript
45
star
12

proxy-client-react

TypeScript
42
star
13

helm-charts

Contains helm-charts for Unleash
Smarty
40
star
14

unleash-client-symfony

Symfony bundle implementing the Unleash server protocol
PHP
32
star
15

unleash-frontend

Unleash Admin UI
TypeScript
30
star
16

unleash-client-nextjs

Unleash SDK for Next.js
TypeScript
27
star
17

unleash-client-rust

Unleash client SDK for Rust language projects
Rust
23
star
18

unleash_proxy_client_flutter

Unleash is a open-source feature flag solution
Dart
15
star
19

yggdrasil

Rust
15
star
20

client-specification

A collection of test specifications to guide client implementations
JavaScript
12
star
21

unleash-proxy-client-swift

Swift
12
star
22

unleash-client-php-wip

PHP client for Unleash
PHP
11
star
23

unleash-examples

Concrete examples of using Unleash
JavaScript
11
star
24

next-unleash

Ease integration with next.js
JavaScript
10
star
25

proxy-client-vue

Vue interface for working with Unleash
TypeScript
8
star
26

unleash-android-proxy-sdk

Kotlin
7
star
27

unleash-docker-community

This is a collection of community maintained extensions to the Unleash Docker containers to make integrating SSO providers easier
Dockerfile
7
star
28

unleash-express

Unleash helper for Express.js
JavaScript
6
star
29

proxy-client-svelte

Svelte interface for working with Unleash
TypeScript
6
star
30

terraform-provider-unleash

Terraform provider for unleash, the Open-source feature management solution
Go
6
star
31

Flask-Unleash

Flask extension to make using Unleash that much easier! 🚦🚦🚦
Python
6
star
32

sdk-integration-tester

Runs integration tests against SDK test servers.
TypeScript
3
star
33

unleash-mini-ws

A small Java-based Workshop to help users get started using Unleash!
Java
2
star
34

unleash-sdk-examples

Example applications using Unleash
TypeScript
2
star
35

actix-template

Template repo for writing web-apps with Actix inside Unleash. See https://github.com/bricks-software/unleash-cloud-operator for an example which does both actix and k8s operator
Rust
2
star
36

unleash-server-api-go

Go client implementation of our admin server API
Go
2
star
37

unleash-react-proxy-sdk

A React SDK to be used together with the Unleash Proxy
2
star
38

unleash-android

Android library to connect to the Unleash frontend API
Kotlin
2
star
39

unleash-event-validator

TypeScript
1
star
40

unleash-spring-boot-starter

Java
1
star
41

actix-middleware-etag

ETag middleware for Actix-web >4
Rust
1
star
42

community-content

Community Content Program for users to be able to contribute to Unleash.
1
star
43

unleash-demo-app

This project is used as a demo app that is runtime-controlled (feature toggled) by Unleash.
TypeScript
1
star
44

unleash-weighter

Python
1
star
45

unleash-client-java-examples

Java
1
star
46

.github

Unleash ✨special✨ files
1
star
47

unleash-action

Unleash integration for GitHub workflows
TypeScript
1
star