ldredis

package module
v2.0.1 Latest Latest
Warning

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

Go to latest
Published: Jan 17, 2023 License: Apache-2.0 Imports: 11 Imported by: 0

README

LaunchDarkly Server-side SDK for Go - Redis integration with Redigo client

Circle CI 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 API implementation it uses is Redigo. There are other Redis client implementations for Go; if LaunchDarkly SDK Redis integrations using other Redis clients are released, they will be in separate repositories.

This version of the library requires at least version 6.0.0 of the LaunchDarkly Go SDK; for versions of the library to use with earlier SDK versions, see the changelog.

The minimum Go version is 1.18.

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/v6"
    "github.com/launchdarkly/go-server-sdk/v6/ldcomponents"
    ldredis "github.com/launchdarkly/go-server-sdk-redis-redigo/v2"
)
  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.

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 over 100 billion 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/v2.0/docs/using-a-persistent-feature-store

To use the Redis data store with the LaunchDarkly client:

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

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

The default Redis pool configuration uses an address of localhost:6379, a maximum of 16 concurrent connections, and blocking connection requests. You may customize the configuration by using the methods of the StoreBuilder returned by DataStore:

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

Note that CacheSeconds() is not a method of StoreBuilder, 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 Redigo client, use StoreBuilder methods such as StoreBuilder.DialOptions and StoreBuilder.Pool. Note that some Redis client features can also be specified as part of the URL: Redigo supports the redis:// syntax (https://www.iana.org/assignments/uri-schemes/prov/redis), which can include a password and a database number, as well as rediss:// (https://www.iana.org/assignments/uri-schemes/prov/rediss), which enables TLS.

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.

Index

Constants

View Source
const (
	// DefaultURL is the default value for StoreBuilder.URL.
	DefaultURL = "redis://localhost:6379"
	// DefaultPrefix is the default value for StoreBuilder.Prefix.
	DefaultPrefix = "launchdarkly"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Pool

type Pool interface {
	// Get obtains a Redis connection.
	//
	// See: https://pkg.go.dev/github.com/gomodule/redigo/redis#Pool.Get
	Get() r.Conn

	// Close releases the resources used by the pool.
	//
	// See: https://pkg.go.dev/github.com/gomodule/redigo/redis#Pool.Close
	Close() error
}

Pool is an interface representing a Redis connection pool.

The methods of this interface are the same as the basic methods of the Pool type in the Redigo client. Any type implementing the interface can be passed to StoreBuilder.PoolInterface to provide custom connection behavior.

type StoreBuilder

type StoreBuilder[T any] struct {
	// contains filtered or unexported fields
}

StoreBuilder is a builder for configuring the Redis-based persistent data store and/or Big Segment store.

Both DataStore and BigSegmentStore return instances of this type. You can use methods of the builder to specify any ny non-default Redis options you may want, before passing the builder to either github.com/launchdarkly/go-server-sdk/v6/ldcomponents.PersistentDataStore or github.com/launchdarkly/go-server-sdk/v6/ldcomponents.BigSegments as appropriate. The two types of stores are independent of each other; you do not need a Big Segment store if you are not using the Big Segments feature, and you do not need to use the same database for both.

In this example, the main data store uses a Redis host called "host1", and the Big Segment store uses a Redis host called "host2":

config.DataStore = ldcomponents.PersistentDataStore(
    ldredis.DataStore().URL("redis://host1:6379")
config.BigSegments = ldcomponents.BigSegments(
    ldredis.DataStore().URL("redis://host2:6379")

Note that the SDK also has its own options related to data storage that are configured at a different level, because they are independent of what database is being used. For instance, the builder returned by github.com/launchdarkly/go-server-sdk/v6/ldcomponents.PersistentDataStore has options for caching:

config.DataStore = ldcomponents.PersistentDataStore(
	ldredis.DataStore().HostAndPort("host1", 6379),
).CacheSeconds(15)

func BigSegmentStore

func BigSegmentStore() *StoreBuilder[subsystems.BigSegmentStore]

BigSegmentStore returns a configurable builder for a Redis-backed Big Segment store.

You can use methods of the builder to specify any non-default Redis options you may want, before passing the builder to github.com/launchdarkly/go-server-sdk/v6/ldcomponents.BigSegments. In this example, the store is configured to use a Redis host called "host2":

config.BigSegments = ldcomponents.BigSegments(
	ldredis.BigSegmentStore().HostAndPort("host2", 6379))

Note that the SDK also has its own options related to Big Segments that are configured at a different level, because they are independent of what database is being used. For instance, the builder returned by github.com/launchdarkly/go-server-sdk/v6/ldcomponents.BigSegments has an option for the status polling interval:

config.BigSegments = ldcomponents.BigSegments(
	ldredis.BigSegmentStore().HostAndPort("host2", 6379),
).StatusPollInterval(time.Second * 30)

func DataStore

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

This is for the main data store that holds feature flag data. To configure a data store for Big Segments, use BigSegmentStore instead.

You can use methods of the builder to specify any non-default Redis options you may want, before passing the builder to github.com/launchdarkly/go-server-sdk/v6/ldcomponents.PersistentDataStore. In this example, the store is configured to use a Redis host called "host1":

config.DataStore = ldcomponents.PersistentDataStore(
	ldredis.DataStore().HostAndPort("host1", 6379))

Note that the SDK also has its own options related to data storage that are configured at a different level, because they are independent of what database is being used. For instance, the builder returned by github.com/launchdarkly/go-server-sdk/v6/ldcomponents.PersistentDataStore has options for caching:

config.DataStore = ldcomponents.PersistentDataStore(
	ldredis.DataStore().HostAndPort("host1", 6379),
).CacheSeconds(15)

func (*StoreBuilder[T]) Build

func (b *StoreBuilder[T]) Build(context subsystems.ClientContext) (T, error)

Build is called internally by the SDK.

func (*StoreBuilder[T]) DescribeConfiguration

func (b *StoreBuilder[T]) DescribeConfiguration() ldvalue.Value

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

func (*StoreBuilder[T]) DialOptions

func (b *StoreBuilder[T]) DialOptions(options ...r.DialOption) *StoreBuilder[T]

DialOptions specifies any of the advanced Redis connection options supported by Redigo, such as DialPassword.

import (
    redigo "github.com/garyburd/redigo/redis"
    ldredis "github.com/launchdarkly/go-server-sdk-redis-redigo/v2"
)
config.DataSource = ldcomponents.PersistentDataStore(
    ldredis.DataStore().DialOptions(redigo.DialPassword("verysecure123")),
)

Note that some Redis client features can also be specified as part of the URL: see URL().

func (*StoreBuilder[T]) HostAndPort

func (b *StoreBuilder[T]) HostAndPort(host string, port int) *StoreBuilder[T]

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

func (*StoreBuilder[T]) Pool

func (b *StoreBuilder[T]) Pool(pool *r.Pool) *StoreBuilder[T]

Pool specifies that the data store should use a specific connection pool configuration. If not specified, it will create a default configuration (see package description). Specifying this option will cause any address specified with URL or HostAndPort to be ignored.

If you only need to change basic connection options such as providing a password, it is simpler to use DialOptions.

Use PoolInterface if you want to provide your own implementation of a connection pool.

func (*StoreBuilder[T]) PoolInterface

func (b *StoreBuilder[T]) PoolInterface(pool Pool) *StoreBuilder[T]

PoolInterface is equivalent to Pool, but uses an interface type rather than a concrete implementation type. This allows implementation of custom behaviors for connection management.

func (*StoreBuilder[T]) Prefix

func (b *StoreBuilder[T]) Prefix(prefix string) *StoreBuilder[T]

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 (*StoreBuilder[T]) URL

func (b *StoreBuilder[T]) URL(url string) *StoreBuilder[T]

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

Note that some Redis client features can also be specified as part of the URL: Redigo supports the redis:// syntax (https://www.iana.org/assignments/uri-schemes/prov/redis), which can include a password and a database number, as well as rediss:// (https://www.iana.org/assignments/uri-schemes/prov/rediss), which enables TLS.

Jump to

Keyboard shortcuts

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