Documentation
¶
Overview ¶
Package flaglite provides a Go SDK for the FlagLite feature flag service.
The SDK supports:
- Simple boolean flag evaluation
- Percentage rollouts with user ID targeting
- Thread-safe caching with configurable TTL
- Fail-closed behavior (returns false on errors)
Basic usage:
client := flaglite.New() // Uses FLAGLITE_API_KEY env var
if client.Enabled("new-checkout") {
showNewCheckout()
}
With user targeting for percentage rollouts:
if client.Enabled("new-checkout", flaglite.WithUserID("user-123")) {
showNewCheckout()
}
Index ¶
Constants ¶
const ( // DefaultBaseURL is the production FlagLite API endpoint. DefaultBaseURL = "https://api.flaglite.dev/v1" // DefaultCacheTTL is the default cache duration for flag evaluations. DefaultCacheTTL = 30 * time.Second // DefaultTimeout is the default HTTP request timeout. DefaultTimeout = 5 * time.Second // EnvAPIKey is the environment variable name for the API key. EnvAPIKey = "FLAGLITE_API_KEY" )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is the FlagLite SDK client for evaluating feature flags. It is safe for concurrent use by multiple goroutines.
func New ¶
func New(opts ...ClientOption) *Client
New creates a new FlagLite client.
By default, it reads the API key from the FLAGLITE_API_KEY environment variable. Use WithAPIKey to override this.
Example:
client := flaglite.New()
client := flaglite.New(flaglite.WithAPIKey("ffl_env_..."))
client := flaglite.New(flaglite.WithCacheTTL(1 * time.Minute))
func (*Client) ClearCache ¶
func (c *Client) ClearCache()
ClearCache removes all cached flag evaluations. This can be useful when you know flags have changed.
func (*Client) Enabled ¶
func (c *Client) Enabled(key string, opts ...EvalOption) bool
Enabled checks if a feature flag is enabled.
This method is thread-safe and uses caching to minimize API calls. On any error (network, invalid key, etc.), it returns false (fail-closed).
Example:
if client.Enabled("new-checkout") {
showNewCheckout()
}
// With user targeting for percentage rollouts
if client.Enabled("new-checkout", flaglite.WithUserID("user-123")) {
showNewCheckout()
}
func (*Client) EnabledWithContext ¶
func (c *Client) EnabledWithContext(ctx context.Context, key string, opts ...EvalOption) (bool, error)
EnabledWithContext checks if a feature flag is enabled with context support.
The context can be used for cancellation and deadline control.
Example:
ctx, cancel := context.WithTimeout(ctx, 2*time.Second) defer cancel() enabled, err := client.EnabledWithContext(ctx, "new-checkout")
func (*Client) EnabledWithError ¶
func (c *Client) EnabledWithError(key string, opts ...EvalOption) (bool, error)
EnabledWithError checks if a feature flag is enabled and returns any error.
Unlike Enabled(), this method returns the error for debugging purposes. On error, the first return value is always false (fail-closed).
Example:
enabled, err := client.EnabledWithError("new-checkout")
if err != nil {
log.Printf("flag evaluation failed: %v", err)
}
type ClientOption ¶
type ClientOption func(*Client)
ClientOption configures a Client.
func WithAPIKey ¶
func WithAPIKey(key string) ClientOption
WithAPIKey sets the API key for the client.
func WithBaseURL ¶
func WithBaseURL(baseURL string) ClientOption
WithBaseURL sets a custom base URL (useful for testing or self-hosted).
func WithCacheTTL ¶
func WithCacheTTL(ttl time.Duration) ClientOption
WithCacheTTL sets the cache TTL duration. Set to 0 to disable caching.
func WithHTTPClient ¶
func WithHTTPClient(httpClient *http.Client) ClientOption
WithHTTPClient sets a custom HTTP client.
func WithTimeout ¶
func WithTimeout(timeout time.Duration) ClientOption
WithTimeout sets the HTTP request timeout.
type EvalOption ¶
type EvalOption func(*evalConfig)
EvalOption configures a single flag evaluation.
func WithUserID ¶
func WithUserID(userID string) EvalOption
WithUserID specifies the user ID for percentage rollout targeting. This enables sticky bucketing - the same user always gets the same result.