cmab

package
v2.2.1 Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2025 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Overview

Package cmab provides contextual multi-armed bandit functionality

Package cmab provides contextual multi-armed bandit functionality

Package cmab to define cmab errors//

Package cmab //

Package cmab provides functionality for Contextual Multi-Armed Bandit (CMAB) decision-making, including client and service implementations for making and handling CMAB requests and responses.

Index

Constants

View Source
const (
	// DefaultMaxRetries is the default number of retries for CMAB requests
	DefaultMaxRetries = 1
	// DefaultInitialBackoff is the default initial backoff duration
	DefaultInitialBackoff = 100 * time.Millisecond
	// DefaultMaxBackoff is the default maximum backoff duration
	DefaultMaxBackoff = 10 * time.Second
	// DefaultBackoffMultiplier is the default multiplier for exponential backoff
	DefaultBackoffMultiplier = 2.0
)
View Source
const (
	// DefaultCacheSize is the default size for CMAB cache (aligned with ODP segments cache)
	DefaultCacheSize = 10000

	// DefaultCacheTTL is the default TTL for CMAB cache (30 minutes to match agent)
	DefaultCacheTTL = 30 * time.Minute

	// DefaultHTTPTimeout is the default HTTP timeout for CMAB requests
	DefaultHTTPTimeout = 10 * time.Second
)
View Source
const CmabFetchFailed = "Failed to fetch CMAB data for experiment %s." //nolint:ST1005 // Required exact format for FSC test compatibility

CmabFetchFailed is the error message format for CMAB fetch failures Format required for FSC test compatibility - capitalized and with period

View Source
const (
	// NumLockStripes defines the number of mutexes for lock striping to reduce contention
	NumLockStripes = 1000
)

Variables

View Source
var CMABPredictionEndpoint = "https://prediction.cmab.optimizely.com/predict/%s"

CMABPredictionEndpoint is the endpoint for CMAB predictions

Functions

func FetchFailedError

func FetchFailedError(experimentKey string) error

FetchFailedError creates a new CMAB fetch failed error with FSC-compatible formatting

Types

type Attribute

type Attribute struct {
	ID    string      `json:"id"`
	Value interface{} `json:"value"`
	Type  string      `json:"type"`
}

Attribute represents an attribute in a CMAB request

type CacheValue

type CacheValue struct {
	AttributesHash string
	VariationID    string
	CmabUUID       string
}

CacheValue represents a cached CMAB decision with attribute hash

type Client

type Client interface {
	// FetchDecision fetches a decision from the CMAB API
	FetchDecision(
		ruleID string,
		userID string,
		attributes map[string]interface{},
		cmabUUID string,
	) (string, error)
}

Client defines the interface for CMAB API clients

type ClientOptions

type ClientOptions struct {
	HTTPClient  *http.Client
	RetryConfig *RetryConfig
	Logger      logging.OptimizelyLogProducer
}

ClientOptions defines options for creating a CMAB client

type Config

type Config struct {
	CacheSize   int
	CacheTTL    time.Duration
	HTTPTimeout time.Duration
	RetryConfig *RetryConfig
	Cache       cache.CacheWithRemove // Custom cache implementation (Redis, etc.)
}

Config holds CMAB configuration options

func NewDefaultConfig

func NewDefaultConfig() Config

NewDefaultConfig creates a Config with default values

type Decision

type Decision struct {
	VariationID string
	CmabUUID    string
	Reasons     []string
}

Decision represents a decision from the CMAB service

type DefaultCmabClient

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

DefaultCmabClient implements the CmabClient interface

func NewDefaultCmabClient

func NewDefaultCmabClient(options ClientOptions) *DefaultCmabClient

NewDefaultCmabClient creates a new instance of DefaultCmabClient

func (*DefaultCmabClient) FetchDecision

func (c *DefaultCmabClient) FetchDecision(
	ruleID string,
	userID string,
	attributes map[string]interface{},
	cmabUUID string,
) (string, error)

FetchDecision fetches a decision from the CMAB API

type DefaultCmabService

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

DefaultCmabService implements the CmabService interface

func NewDefaultCmabService

func NewDefaultCmabService(options ServiceOptions) *DefaultCmabService

NewDefaultCmabService creates a new instance of DefaultCmabService

func (*DefaultCmabService) GetDecision

func (s *DefaultCmabService) GetDecision(
	projectConfig config.ProjectConfig,
	userContext entities.UserContext,
	ruleID string,
	options *decide.Options,
) (Decision, error)

GetDecision returns a CMAB decision for the given rule and user context

type Instance

type Instance struct {
	VisitorID    string      `json:"visitorId"`
	ExperimentID string      `json:"experimentId"`
	Attributes   []Attribute `json:"attributes"`
	CmabUUID     string      `json:"cmabUUID"`
}

Instance represents an instance in a CMAB request

type Prediction

type Prediction struct {
	VariationID string `json:"variation_id"`
}

Prediction represents a prediction in a CMAB response

type Request

type Request struct {
	Instances []Instance `json:"instances"`
}

Request represents a request to the CMAB API

type Response

type Response struct {
	Predictions []Prediction `json:"predictions"`
}

Response represents a response from the CMAB API

type RetryConfig

type RetryConfig struct {
	// MaxRetries is the maximum number of retry attempts
	MaxRetries int
	// InitialBackoff is the initial backoff duration
	InitialBackoff time.Duration
	// MaxBackoff is the maximum backoff duration
	MaxBackoff time.Duration
	// BackoffMultiplier is the multiplier for exponential backoff
	BackoffMultiplier float64
}

RetryConfig defines configuration for retry behavior

type Service

type Service interface {
	// GetDecision returns a CMAB decision for the given rule and user context
	GetDecision(
		projectConfig config.ProjectConfig,
		userContext entities.UserContext,
		ruleID string,
		options *decide.Options,
	) (Decision, error)
}

Service defines the interface for CMAB decision services

type ServiceOptions

type ServiceOptions struct {
	Logger     logging.OptimizelyLogProducer
	CmabCache  cache.CacheWithRemove
	CmabClient Client
}

ServiceOptions defines options for creating a CMAB service

Jump to

Keyboard shortcuts

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