Documentation
¶
Overview ¶
Package shipd evaluates shipd flags locally using periodically refreshed snapshots.
Index ¶
- Constants
- Variables
- type Client
- func (c *Client) Close() error
- func (c *Client) DroppedEvents() uint64
- func (c *Client) Evaluate(key string, ctx EvaluationContext) Result
- func (c *Client) Flush(ctx context.Context) (err error)
- func (c *Client) GetJSON(key string, ctx EvaluationContext) any
- func (c *Client) GetNumber(key string, ctx EvaluationContext) float64
- func (c *Client) GetVariant(key string, ctx EvaluationContext) string
- func (c *Client) Identify(identity Identity) error
- func (c *Client) IsEnabled(key string, ctx EvaluationContext) bool
- func (c *Client) LastError() error
- func (c *Client) Ready(ctx context.Context) (err error)
- func (c *Client) Refresh(ctx context.Context) (err error)
- func (c *Client) Shutdown(ctx context.Context) error
- func (c *Client) SnapshotVersion() (version int64, ok bool)
- type EvaluationContext
- type HTTPClient
- type Identity
- type Options
- type Overrides
- type Result
Constants ¶
const ( SnapshotSchemaVersion = 1 // DefaultEndpoint is the edge origin used unless Overrides.UnsafeEndpoint is set. DefaultEndpoint = "https://edge.useshipd.com" SDKVersion = "0.1.0" )
Variables ¶
var ( // ErrMalformedSnapshot reports a snapshot that failed validation. The client // keeps its last valid snapshot. ErrMalformedSnapshot = errors.New("shipd: malformed snapshot ignored") // ErrUnsupportedSchema reports a snapshot schema newer than this SDK. Upgrade // the SDK; the client keeps its last valid snapshot. ErrUnsupportedSchema = errors.New("shipd: unsupported snapshot schema version") )
var ErrClosed = errors.New("shipd: client is closed")
ErrClosed is returned by Ready, Refresh, Flush, and Identify after Close or Shutdown. Evaluation keeps working after close.
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is safe for concurrent use. Keep one client per environment per process.
func New ¶
New starts an initial asynchronous fetch and background polling. Construction errors concern configuration only; network errors are available via LastError.
func (*Client) Close ¶
Close stops polling, cancels requests, and waits for background work without flushing events; use Shutdown to deliver them first. Evaluation after Close still serves the last snapshot/defaults. Close implements io.Closer.
func (*Client) DroppedEvents ¶
DroppedEvents reports how many diagnostic events were discarded because the buffer was full, for example while the edge was unreachable.
func (*Client) Evaluate ¶
func (c *Client) Evaluate(key string, ctx EvaluationContext) Result
Evaluate returns details and records a bounded diagnostic event. Request attributes remain local; evaluation telemetry includes only the user ID.
func (*Client) Flush ¶
Flush sends pending events. Failed batches are retained for a later retry; the buffer retains at most 200 events, dropping the oldest on overflow (see DroppedEvents). The client also flushes once FlushAt events are buffered.
func (*Client) GetJSON ¶
func (c *Client) GetJSON(key string, ctx EvaluationContext) any
GetJSON evaluates a JSON flag for ctx and returns a copy the caller may modify. Objects decode as map[string]any and numbers as float64. It falls back to the configured default, then to an empty object.
func (*Client) GetNumber ¶
func (c *Client) GetNumber(key string, ctx EvaluationContext) float64
GetNumber evaluates a number flag for ctx. JSON numbers are float64. It falls back to the configured default, then to 0.
func (*Client) GetVariant ¶
func (c *Client) GetVariant(key string, ctx EvaluationContext) string
GetVariant evaluates a variant flag for ctx. It falls back to the configured default, then to "control".
func (*Client) Identify ¶
Identify queues an identify event. Flush delivers it. Traits must be scalar JSON values or nil, matching the edge identify contract.
func (*Client) IsEnabled ¶
func (c *Client) IsEnabled(key string, ctx EvaluationContext) bool
IsEnabled evaluates a boolean flag for ctx. It never makes a network request. Before a snapshot loads, for an unknown key, or when the flag serves a non-boolean value, it returns the configured default or false.
func (*Client) LastError ¶
LastError reports the most recent background/request failure. A successful refresh or flush clears it. Evaluation never requires this method to succeed.
func (*Client) Ready ¶
Ready waits for the initial snapshot fetch. It returns nil once a valid snapshot (or Bootstrap) is loaded, or the initial fetch error as soon as that attempt fails; evaluation then serves defaults and polling may still recover. A bounded context is recommended.
func (*Client) Refresh ¶
Refresh fetches a snapshot now. Requests are serialized so an old ETag cannot race a newer snapshot. Failures preserve the complete last valid snapshot.
func (*Client) Shutdown ¶
Shutdown flushes pending events within ctx, then closes the client. It returns the flush error, if any; the client is closed either way. Events recorded after Shutdown starts are not delivered. Use it for graceful shutdown with a bounded context. Repeated calls are safe.
func (*Client) SnapshotVersion ¶
SnapshotVersion reports the loaded snapshot version without recording an evaluation. ok is false until the first valid snapshot arrives.
type EvaluationContext ¶
type EvaluationContext struct {
UserID string
// UserIDPresent distinguishes an explicitly empty UserID from an omitted one.
// Nonempty UserID values are always present. Empty IDs do not provide a bucket.
UserIDPresent bool
Attributes map[string]any
SegmentIDs []string
}
EvaluationContext is request-scoped. Attributes should contain strings, numbers, booleans, or nil. The client never changes the supplied context.
type HTTPClient ¶
HTTPClient is implemented by *http.Client.
type Identity ¶
Identity is sent to shipd for audience insights. It does not set a global evaluation context; server applications pass context separately per request.
type Options ¶
type Options struct {
SDKKey string
Defaults map[string]any
RefreshInterval time.Duration
FetchTimeout time.Duration
DisablePolling bool
DisableEvents bool
// FlushAt flushes events once this many are buffered (default 50, at most 200).
// A negative value flushes on FlushInterval only.
FlushAt int
// FlushInterval flushes buffered events periodically (default 5 seconds).
// A negative value disables timed flushes; events then flush at FlushAt, on
// Flush, or on Shutdown.
FlushInterval time.Duration
// Bootstrap is a snapshot (the JSON body of GET /sdk/v1/snapshot) served until
// a newer one is fetched. It makes the client ready immediately; use it for
// tests and offline starts. Invalid snapshots fail New.
Bootstrap []byte
// Overrides changes where the client connects. Production apps should not set it.
Overrides Overrides
// HTTPClient may supply a custom transport. Every request also has FetchTimeout.
HTTPClient HTTPClient
}
Options configures one client per process. Zero durations select the defaults. Set DisablePolling to fetch only the initial snapshot (or refresh manually).
type Overrides ¶
type Overrides struct {
// UnsafeEndpoint replaces the edge origin (https://edge.useshipd.com) that
// receives your SDK key. Unsafe: for shipd development and tests only.
UnsafeEndpoint string
}
Overrides changes where the client connects.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package config defines shipd flags in Go.
|
Package config defines shipd flags in Go. |
|
examples
|
|
|
basic
command
|
|
|
basic/flags
Code generated by shipd types gen --language go; DO NOT EDIT.
|
Code generated by shipd types gen --language go; DO NOT EDIT. |