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
- Variables
- func FetchFailedError(experimentKey string) error
- type Attribute
- type CacheValue
- type Client
- type ClientOptions
- type Config
- type Decision
- type DefaultCmabClient
- type DefaultCmabService
- type Instance
- type Prediction
- type Request
- type Response
- type RetryConfig
- type Service
- type ServiceOptions
Constants ¶
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 )
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 )
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
const (
// NumLockStripes defines the number of mutexes for lock striping to reduce contention
NumLockStripes = 1000
)
Variables ¶
var CMABPredictionEndpoint = "https://prediction.cmab.optimizely.com/predict/%s"
CMABPredictionEndpoint is the endpoint for CMAB predictions
Functions ¶
func FetchFailedError ¶
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 ¶
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 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