ldredis

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 18, 2024 License: Apache-2.0 Imports: 9 Imported by: 0

README

LaunchDarkly Server-side SDK for Go - Redis integration with go-redis

Actions Status Documentation

This library provides a Redis-backed persistence mechanism (data store) for the LaunchDarkly Go SDK, replacing the default in-memory data store.

The Redis client implementation it uses is go-redis. This distinguishes it from the other Go SDK Redis integration, go-server-sdk-redis-redigo, which uses the redigo client (therefore the two projects have somewhat long and repetitive names). The main difference between the two is that go-redis supports cluster mode and redigo does not.

This version of the library requires at least version 7.0.0 of the LaunchDarkly Go SDK.

The minimum Go version is 1.19.

For more information, see also: Using a persistent feature store.

Quick setup

This assumes that you have already installed the LaunchDarkly Go SDK.

  1. Import the LaunchDarkly SDK packages and the package for this library:
import (
    ld "github.com/launchdarkly/go-server-sdk/v7"
    "github.com/launchdarkly/go-server-sdk/v7/ldcomponents"
    ldredis "github.com/launchdarkly/go-server-sdk-redis-go-redis"
)
  1. When configuring your SDK client, add the Redis data store as a PersistentDataStore. You may specify any custom Redis options using the methods of RedisDataStoreBuilder. For instance, to customize the Redis URL:
    var config ld.Config
    config.DataStore = ldcomponents.PersistentDataStore(
        ldredis.DataStore().URL("redis://my-redis-host"),
    )

By default, the store will try to connect to a local Redis instance on port 6379.

To use cluster mode or other advanced go-redis features, use the Options method to pass a complete Redis client configuration:

import (
    goredis "github.com/go-redis/redis/v7"
)

    redisOpts := goredis.UniversalOptions{
        Addrs: []string{ "cluster-host-1:6379", "cluster-host-2:6379" },
    }
    config.DataStore = ldcomponents.PersistentDataStore(
        ldredis.DataStore().Options(redisOpts),
    )

Caching behavior

The LaunchDarkly SDK has a standard caching mechanism for any persistent data store, to reduce database traffic. This is configured through the SDK's PersistentDataStoreBuilder class as described the SDK documentation. For instance, to specify a cache TTL of 5 minutes:

    var config ld.Config
    config.DataStore = ldcomponents.PersistentDataStore(
        ldredis.DataStore(),
    ).CacheMinutes(5)

LaunchDarkly overview

LaunchDarkly is a feature management platform that serves trillions of feature flags daily to help teams build better software, faster. Get started using LaunchDarkly today!

About LaunchDarkly

  • LaunchDarkly is a continuous delivery platform that provides feature flags as a service and allows developers to iterate quickly and safely. We allow you to easily flag your features and manage them from the LaunchDarkly dashboard. With LaunchDarkly, you can:
    • Roll out a new feature to a subset of your users (like a group of users who opt-in to a beta tester group), gathering feedback and bug reports from real-world use cases.
    • Gradually roll out a feature to an increasing percentage of users, and track the effect that the feature has on key metrics (for instance, how likely is a user to complete a purchase if they have feature A versus feature B?).
    • Turn off a feature that you realize is causing performance problems in production, without needing to re-deploy, or even restart the application with a changed configuration file.
    • Grant access to certain features based on user attributes, like payment plan (eg: users on the ‘gold’ plan get access to more features than users in the ‘silver’ plan). Disable parts of your application to facilitate maintenance, without taking everything offline.
  • LaunchDarkly provides feature flag SDKs for a wide variety of languages and technologies. Check out our documentation for a complete list.
  • Explore LaunchDarkly

Documentation

Overview

Package ldredis provides a Redis-backed persistent data store for the LaunchDarkly Go SDK.

For more details about how and why you can use a persistent data store, see: https://docs.launchdarkly.com/sdk/concepts/data-stores

To use the Redis data store with the LaunchDarkly client:

import ldredis "github.com/launchdarkly/go-server-sdk-redis-go-redis"

config := ld.Config{
    DataStore: ldcomponents.PersistentDataStore(ldredis.DataStore()),
}
client, err := ld.MakeCustomClient("sdk-key", config, 5*time.Second)

The default configuration uses an address of localhost:6379. You may customize the configuration by using the methods of the ldredis.DataStoreBuilder returned by ldredis.DataStore():

config := ld.Config{
    DataStore: ldcomponents.PersistentDataStore(
        ldredis.DataStore().URL(myRedisURL),
    ).CacheSeconds(30),
}

Note that CacheSeconds() is not a method of ldredis.DataStoreBuilder, but rather a method of ldcomponents.PersistentDataStore(), because the caching behavior is provided by the SDK for all database integrations.

For advanced customization of the underlying go-redis client, use the Options method with ldredis.DataStore(). Note that some Redis client features can also be specified as part of the URL.

If you are also using Redis for other purposes, the data store can coexist with other data as long as you are not using the same keys. By default, the keys used by the data store will always start with "launchdarkly:"; you can change this to another prefix if desired using the ldredis.DataStoreBuilder's Prefix method.

Index

Constants

View Source
const (
	// DefaultPrefix is a string that is prepended (along with a colon) to all Redis keys used
	// by the data store. You can change this value with the Prefix() option for
	// NewRedisDataStoreWithDefaults, or with the "prefix" parameter to the other constructors.
	//
	// See also DefaultClusterPrefix.
	DefaultPrefix = "launchdarkly"

	// DefaultClusterPrefix is an additional string of "{ld}." that is added before the
	// configured prefix (or DefaultPrefix) if you are connecting to a Redis cluster, if the
	// prefix does not already include curly braces.
	//
	// For instance, if you set the prefix to "app1", and you are using a single Redis node,
	// all keys will start with "app1:", but if you are using a cluster, they will start with
	// "{ld}.app1:". But if you set the prefix to "{xyz}app1", then the keys will start with
	// "{xyz}app1:" regardless of whether you are using a cluster or not.
	//
	// The reason for this is that in a Redis cluster, keys that begin with the same string in
	// curly braces are grouped together into one hash slot in the cluster. That allows
	// operations on those keys to be atomic, which the LaunchDarkly SDK Redis integration
	// relies on.
	//
	// When using a single Redis node rather than a cluster, there is no special meaning for
	// braces-- they are treated like any other characters in keys.
	DefaultClusterPrefix = "{ld}."
)

Variables

This section is empty.

Functions

This section is empty.

Types

type DataStoreBuilder

type DataStoreBuilder struct {
	// contains filtered or unexported fields
}

DataStoreBuilder is a builder for configuring the Redis-based persistent data store.

Obtain an instance of this type by calling DataStore(). After calling its methods to specify any desired custom settings, wrap it in a PersistentDataStoreBuilder by calling ldcomponents.PersistentDataStore(), and then store this in the SDK configuration's DataStore field.

Builder calls can be chained, for example:

config.DataStore = ldredis.DataStore().URL("redis://hostname").Prefix("prefix")

You do not need to call the builder's Build() method yourself to build the actual data store; that will be done by the SDK.

func DataStore

func DataStore() *DataStoreBuilder

DataStore returns a configurable builder for a Redis-backed data store.

func (*DataStoreBuilder) Addresses

func (b *DataStoreBuilder) Addresses(addresses ...string) *DataStoreBuilder

Addresses specifies Redis host addresses. This is a shortcut for setting the Addrs field with Options.

If multiple addresses are given, and a Master has been set, this is treated as a list of Redis Sentinel nodes.

If multiple addresses are given, and no Master has been set, it is treated as a list of cluster nodes.

If no addresses are given, the default address of localhost:6379 will be used.

Calling Addresses overwrites any addresses previously set with HostAndPort or Options.

func (*DataStoreBuilder) Build

Build is called internally by the SDK to create the data store implementation object.

func (*DataStoreBuilder) CheckOnStartup

func (b *DataStoreBuilder) CheckOnStartup(value bool) *DataStoreBuilder

CheckOnStartup sets whether the data store should check the availability of the Redis server when the SDK is initialized. If so, the SDK will refuse to start unless the server is available. This is true by default.

func (*DataStoreBuilder) DescribeConfiguration

func (b *DataStoreBuilder) DescribeConfiguration() ldvalue.Value

DescribeConfiguration is used internally by the SDK to inspect the configuration.

func (*DataStoreBuilder) HostAndPort

func (b *DataStoreBuilder) HostAndPort(host string, port int) *DataStoreBuilder

HostAndPort is a shortcut for specifying the Redis host address as a hostname and port.

To use multiple Redis hosts in cluster mode, use Addresses; or, use Options and set the Addrs field.

Calling HostAndPort overwrites any addresses previously set with Addresses or Options.

func (*DataStoreBuilder) Master

func (b *DataStoreBuilder) Master(masterName string) *DataStoreBuilder

Master sets the master hostname, when using Redis Sentinel.

func (*DataStoreBuilder) Options

func (b *DataStoreBuilder) Options(options redis.UniversalOptions) *DataStoreBuilder

Options sets all the parameters supported by the go-redis UniversalOptions type.

This overwrites any previous setting of HostAndPort or Addresses.

func (*DataStoreBuilder) Prefix

func (b *DataStoreBuilder) Prefix(prefix string) *DataStoreBuilder

Prefix specifies a string that should be prepended to all Redis keys used by the data store. A colon will be added to this automatically. If this is unspecified or empty, DefaultPrefix will be used.

func (*DataStoreBuilder) URL

URL specifies the Redis host URL. If not specified, the default value is DefaultURL.

Note that some Redis client features can be specified either as part of the URL or with Options. For instance, the Password and DB fields in Options can be part of a "redis://" URL (https://www.iana.org/assignments/uri-schemes/prov/redis), and TLS can be enabled either by setting the TLSConfig in Options or by using a "rediss://" URL (https://www.iana.org/assignments/uri-schemes/prov/rediss).

To use multiple Redis hosts in cluster mode, use Options and set the Addrs field.

Specifying an invalid URL will cause an error when the SDK is started.

Jump to

Keyboard shortcuts

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