This package is a server-side SDK for applications written in Golang for the Tailslide feature flag framework.
Visit the https://github.com/tailslide-io repository or see Tailslide’s case study page for more information.
Install the Tailslide npm package with go get github.com/tailslide-io/tailslide.go
The FlagManager
struct is the entry point of this SDK. It is responsible for retrieving all the flag rulesets for a given app with its AppId
and creating new Toggler
structs to handle toggling of feature flags within that app. To create a FlagManager
struct, a user must provide a configuration object:
import (
tailslide "github.com/tailslide-io/tailslide.go"
)
func main(){
config := tailslide.FlagManagerConfig{
NatsServer: "nats://localhost:4222",
NatsStream: "flags_ruleset",
AppId: "1",
UserContext: "375d39e6-9c3f-4f58-80bd-e5960b710295",
SdkKey: "myToken",
RedisHost: "http://localhost",
RedisPort: "6379",
}
manager := tailslide.NewFlagManager(config)
manager.InitializeFlags()
}
NatsServer
is the NATS JetStream serveraddress:port
NatsStream
is the NATS JetStream’s stream name that stores all the apps and their flag rulesetsAppId
is the ID number of the app the user wants to retrieve its flag ruleset fromUserContext
is the UUID string that identifies the current userSdkKey
is the SDK key for the Tailslide, it is used as a password for NATS JetStream token authenticationRedisHost
is the address to the Redis databaseRedisPort
is the port number that the Redis database runs on
After instantiating a FlagManager
, invoke the initialize
method. This method connects the FlagManager
instance to both NATS JetStream and Redis Timeseries, and asynchronously retrieves the latest and any new flag ruleset data.
Once the FlagManager
is created, it can create a Toggler
, with the NewToggler
method, for each feature flag that the developer wants to wrap the new and old features in. A Toggler
’s IsFlagActive
method checks whether the flag with its FlagName
is active or not based on the flag ruleset. A Toggler
’s IsFlagActive
method returns a boolean value, which can be used to evaluate whether a new feature should be used or not.
flagConfig := tailslide.TogglerConfig{
FlagName: "App 1 Flag 1",
}
flagToggler, err := manager.NewToggler(flagConfig)
if flagToggler.IsFlagActive() {
// call new feature here
} else {
// call old feature here
}
To use a Toggler
instance to record successful or failed operations, call its EmitSuccess
or EmitFailure
methods:
if successCondition {
flagToggler.EmitSuccess()
} else {
flagToggler.EmitFailure()
}
The FlagManager
struct is the entry point of the SDK. A new FlagManager
object will need to be created for each app.
Parameters:
options
: An object with the following keysNatsServer
is the NATS JetStream serveraddress:port
NatsStream
is the NATS JetStream’s stream name that stores all the apps and their flag rulesetsAppId
a number representing the application the microservice belongs toSdkKey
a string generated via the Tower front-end for NATS JetStream authenticationUserContext
a string representing the user’s UUIDRedisHost
a string that represents the url of the Redis serverRedisPort
a number that represents the port number of the Redis server
Return Value:
- A pointer to a
FlagManager
struct
Asynchronously initialize flagmanager
connections to NATS JetStream and Redis database
Parameters:
nil
Return Value:
nil
Set the current user's context for the flagmanager
Parameters:
newUserContext
: A UUID string that represents the current active user
Return Value:
nil
Returns the current user context
Parameters:
nil
Return Value:
- The UUID string that represents the current active user
Creates a new toggler to check for a feature flag's status from the current app's flag ruleset by the flag's name.
Parameters:
options
: An object with key ofFlagName
and a string value representing the name of the feature flag for the new toggler to check whether the new feature is enabled
Return Value:
- A pointer to a
Toggler
object
Asynchronously disconnects the FlagManager
instance from NATS JetStream and Redis database
Parameters:
nil
Return Value:
nil
The Toggler structs provide methods that determine whether or not new feature code is run and handles success/failure emissions. Each toggler handles one feature flag, and is created by FlagManager.NewToggler()
.
Checks for flag status, whitelisted users, and rollout percentage in that order to determine whether the new feature is enabled.
- If the flag's active status is false, the function returns
false
- If current user's UUID is in the whitelist of users, the function returns
true
- If current user's UUID hashes to a value within user rollout percentage, the function returns
true
- If current user's UUID hashes to a value outside user rollout percentage, the function returns
false
Parameters:
nil
Return Value
true
orfalse
depending on whether the feature flag is active
Records a successful operation to the Redis Timeseries database, with key flagId:success
and value of current timestamp
Parameters:
nil
Return Value
nil
Records a failure operation to the Redis Timeseries database, with key flagId:success
and value of current timestamp
Parameters:
nil
Return Value
nil