Documentation
¶
Index ¶
- func FindFreePort() (int, error)
- func FormatAccessLogError(reqLog *bfe_access_pb.RequestLog) string
- func GetBFETotalBytesBodyBuffer(monitorPort int) (int64, error)
- func ParseAccessLog(logDir string, timeout time.Duration) ([]*bfe_access_pb.RequestLog, error)
- func ParseAccessLogAfterStop(logDir string) ([]*bfe_access_pb.RequestLog, error)
- func RewriteBFEPorts(path string, httpPort, httpsPort, monitorPort int) error
- func RewriteBFETotalBodyBufferSize(path string, size int64) error
- func WaitForTCP(addr string, timeout time.Duration) error
- type ActionFile
- type BFEConfigBuilder
- type MockBackend
- func (b *MockBackend) Addr() string
- func (b *MockBackend) AnthropicVersions() []string
- func (b *MockBackend) AuthHeaders() []string
- func (b *MockBackend) Close()
- func (b *MockBackend) Hits() int
- func (b *MockBackend) HostPort() (string, int)
- func (b *MockBackend) Models() []string
- func (b *MockBackend) RequestBodies() [][]byte
- func (b *MockBackend) XApiKeyHeaders() []string
- type ProcessEnv
- type QuotaPlan
- type RateLimitPolicy
- type RateLimitPolicyData
- type RateLimitProductRule
- type RateLimitRule
- type RedisServer
- type TokenFile
- type TokenRule
- type TokenRuleData
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func FindFreePort ¶
FindFreePort returns a free TCP port on localhost.
func FormatAccessLogError ¶
func FormatAccessLogError(reqLog *bfe_access_pb.RequestLog) string
FormatAccessLogError returns a string with all request log fields for debugging test failures.
func GetBFETotalBytesBodyBuffer ¶
GetBFETotalBytesBodyBuffer queries the BFE monitor endpoint for the current total bytes_body buffer size.
func ParseAccessLog ¶
func ParseAccessLog(logDir string, timeout time.Duration) ([]*bfe_access_pb.RequestLog, error)
ParseAccessLog reads the b2log file written by mod_access_pb3 and returns all decoded RequestLog records. It polls briefly if the file is not yet populated.
func ParseAccessLogAfterStop ¶
func ParseAccessLogAfterStop(logDir string) ([]*bfe_access_pb.RequestLog, error)
ParseAccessLogAfterStop reads the b2log file after BFE has been stopped, ensuring all buffered logs are flushed.
func RewriteBFEPorts ¶
RewriteBFEPorts rewrites the httpPort, httpsPort and monitorPort lines in bfe.conf and ensures HTTP/HTTPS/monitor listeners are bound to the loopback interface only.
func RewriteBFETotalBodyBufferSize ¶
RewriteBFETotalBodyBufferSize rewrites the totalBodyBufferSize line in bfe.conf.
Types ¶
type ActionFile ¶
type ActionFile struct {
Cmd string
}
ActionFile is the JSON representation of an action.
type BFEConfigBuilder ¶
type BFEConfigBuilder struct {
// TemplateDir contains static BFE data files (bfe.conf, cluster_conf, mod_ai_route, etc.).
TemplateDir string
// TargetConfDir is the directory where the final BFE config will be written.
TargetConfDir string
// Backends maps cluster names to mock backends.
Backends map[string]*MockBackend
// AIConfs optionally injects AIConf into cluster_conf.data for specific clusters.
AIConfs map[string]*cluster_conf.AIConf
// TotalBodyBufferSize overrides the totalBodyBufferSize value in bfe.conf.
// A value of 0 keeps the template value.
TotalBodyBufferSize int64
// RedisAddr is the address of the redis server used by mod_ai_token_auth.
// If empty, mod_ai_token_auth.conf is not rewritten.
RedisAddr string
// TokenRuleData optionally generates mod_ai_token_auth/token_rule.data.
TokenRuleData *TokenRuleData
// RateLimitPolicyData optionally generates mod_ai_rate_limit/ai_rate_limit.data.
RateLimitPolicyData *RateLimitPolicyData
}
BFEConfigBuilder builds a temporary BFE configuration directory from a template.
func (*BFEConfigBuilder) Build ¶
func (b *BFEConfigBuilder) Build() error
Build prepares the BFE configuration directory.
type MockBackend ¶
type MockBackend struct {
ClusterName string
Response int
Body string
// ReadBeforeClose, if greater than 0, causes the handler to read this
// many bytes from the request body and then close the connection without
// sending an HTTP response. This is useful for simulating a backend that
// fails mid-stream.
ReadBeforeClose int
// DelayResponse sleeps for the given duration after reading the body and
// before writing the response. It can be used to keep the request (and
// any allocated body buffer) alive for a period of time.
DelayResponse time.Duration
// ReadNotify, if non-nil, is closed the first time the handler starts
// reading the request body. This can be used to synchronize with the
// allocation of body buffers inside BFE.
ReadNotify chan struct{}
// HoldResponse, if non-nil, blocks the handler after the request body has
// been fully read and before the response is written. The response is only
// sent after the channel is closed. This is useful for keeping a request
// alive while another request is being processed.
HoldResponse <-chan struct{}
// HoldBeforeRead, if non-nil, blocks the handler after the request headers
// have been received and before the body is read. This can be used to keep
// BFE from closing the request body while another request is processed.
HoldBeforeRead <-chan struct{}
// ResponseFunc, if non-nil, overrides Response/Body and is called for each
// request to determine the response status and body.
ResponseFunc func(r *http.Request, count int) (int, string)
// ResponseHeaders, if non-nil, is written to the response before the status code.
ResponseHeaders map[string]string
// contains filtered or unexported fields
}
MockBackend wraps an httptest.Server and records request metadata.
func NewMockBackend ¶
func NewMockBackend(clusterName string, response int, body string) *MockBackend
NewMockBackend starts a local HTTP server that returns the given status code.
func (*MockBackend) Addr ¶
func (b *MockBackend) Addr() string
Addr returns the host:port of the mock backend.
func (*MockBackend) AnthropicVersions ¶ added in v1.8.6
func (b *MockBackend) AnthropicVersions() []string
AnthropicVersions returns a deep copy of all observed anthropic-version headers.
func (*MockBackend) AuthHeaders ¶
func (b *MockBackend) AuthHeaders() []string
AuthHeaders returns a deep copy of all observed Authorization headers.
func (*MockBackend) Hits ¶
func (b *MockBackend) Hits() int
Hits returns the number of requests received.
func (*MockBackend) HostPort ¶
func (b *MockBackend) HostPort() (string, int)
HostPort returns the host and port of the mock backend.
func (*MockBackend) Models ¶
func (b *MockBackend) Models() []string
Models returns the list of model values observed in request bodies.
func (*MockBackend) RequestBodies ¶
func (b *MockBackend) RequestBodies() [][]byte
RequestBodies returns a deep copy of all observed request bodies.
func (*MockBackend) XApiKeyHeaders ¶ added in v1.8.6
func (b *MockBackend) XApiKeyHeaders() []string
XApiKeyHeaders returns a deep copy of all observed x-api-key headers.
type ProcessEnv ¶
type ProcessEnv struct {
// contains filtered or unexported fields
}
ProcessEnv manages building and running a real BFE process for integration tests.
func NewProcessEnv ¶
func NewProcessEnv(t *testing.T) *ProcessEnv
NewProcessEnv creates a ProcessEnv for the current test.
func (*ProcessEnv) Build ¶
func (p *ProcessEnv) Build()
Build compiles the BFE binary if not already cached. The binary path includes the current git commit hash so that switching commits forces a rebuild. Within the same commit, source mtime is checked to catch uncommitted local edits.
func (*ProcessEnv) SourceRoot ¶
func (p *ProcessEnv) SourceRoot() string
SourceRoot returns the absolute path to the bfe source root.
func (*ProcessEnv) StartBFE ¶
func (p *ProcessEnv) StartBFE(confDir, logDir string) (int, int, func())
StartBFE starts a real BFE process with the given conf root and log dir. It returns the HTTP port, the monitor port and a teardown function.
func (*ProcessEnv) WorkDir ¶
func (p *ProcessEnv) WorkDir() string
WorkDir returns the per-test temporary directory.
type QuotaPlan ¶
type QuotaPlan struct {
Id string
Unlimited bool
PassNoQuota bool
RedisKey string
ExpiredTime int64
Quota int64
Unit string
}
QuotaPlan is the JSON representation of a quota plan.
type RateLimitPolicy ¶ added in v1.8.6
type RateLimitPolicy struct {
Name string `json:"name"`
Enabled bool `json:"enabled"`
Rules struct {
TPM []RateLimitRule `json:"tpm,omitempty"`
RPM []RateLimitRule `json:"rpm,omitempty"`
MaxConcurrency *int64 `json:"max_concurrency,omitempty"`
} `json:"rules"`
}
RateLimitPolicy is the JSON representation of a rate limit policy in ai_rate_limit.data.
type RateLimitPolicyData ¶ added in v1.8.6
type RateLimitPolicyData struct {
Version string `json:"Version"`
Config map[string][]RateLimitProductRule `json:"Config"`
RateLimitPolicies map[string]RateLimitPolicy `json:"RateLimitPolicies"`
ApikeyRateLimitPolicyBindings map[string][]string `json:"ApikeyRateLimitPolicyBindings"`
}
RateLimitPolicyData holds the content of mod_ai_rate_limit/ai_rate_limit.data.
type RateLimitProductRule ¶ added in v1.8.6
type RateLimitProductRule struct {
Cond string `json:"cond"`
HitAction struct {
Cmd string `json:"cmd"`
Params []string `json:"params,omitempty"`
} `json:"hit_action"`
}
RateLimitProductRule is the JSON representation of a product rule in ai_rate_limit.data.
type RateLimitRule ¶ added in v1.8.6
type RateLimitRule struct {
Name string `json:"name"`
WindowMinutes int `json:"window_minutes"`
MaxTokens int64 `json:"max_tokens,omitempty"`
MaxRequests int64 `json:"max_requests,omitempty"`
StepMinutes int `json:"step_minutes,omitempty"`
Burst int64 `json:"burst,omitempty"`
Models []string `json:"models,omitempty"`
RedisKey string `json:"redis_key,omitempty"`
}
RateLimitRule is the JSON representation of a TPM/RPM rule in ai_rate_limit.data.
type RedisServer ¶
type RedisServer struct {
// contains filtered or unexported fields
}
RedisServer wraps miniredis for integration tests.
func NewRedisServer ¶
func NewRedisServer(t *testing.T) *RedisServer
NewRedisServer starts a new embedded redis server.
func (*RedisServer) Addr ¶
func (s *RedisServer) Addr() string
Addr returns the redis server address in "host:port" format.
func (*RedisServer) Exists ¶ added in v1.8.6
func (s *RedisServer) Exists(key string) bool
Exists reports whether the given key is present in redis.
func (*RedisServer) GetQuota ¶
func (s *RedisServer) GetQuota(key string) int64
GetQuota returns the current integer quota value for the given key.
func (*RedisServer) SetQuota ¶
func (s *RedisServer) SetQuota(key string, value int64)
SetQuota sets an integer quota value for the given key.
type TokenFile ¶
type TokenFile struct {
Key string `json:"key"`
KeyId string `json:"key_id"`
Enabled bool `json:"enabled"`
ExpiredTime int64 `json:"expired_time"`
UnlimitedQuota bool `json:"unlimited_quota"`
Models *string `json:"allow_models"`
BlockModels *string `json:"block_models"`
Subnet *string `json:"subnet"`
Tags []bfe_basic.ApikeyTag `json:"tags"`
QuotaPlans []string `json:"quota_plans"`
}
TokenFile is the JSON representation of a token file.
type TokenRule ¶
type TokenRule struct {
Cond string
Action ActionFile
}
TokenRule is the JSON representation of a token rule.