common

package
v1.8.5 Latest Latest
Warning

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

Go to latest
Published: Aug 21, 2026 License: Apache-2.0 Imports: 22 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FindFreePort

func FindFreePort() (int, error)

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

func GetBFETotalBytesBodyBuffer(monitorPort int) (int64, error)

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

func RewriteBFEPorts(path string, httpPort, httpsPort, monitorPort int) error

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

func RewriteBFETotalBodyBufferSize(path string, size int64) error

RewriteBFETotalBodyBufferSize rewrites the totalBodyBufferSize line in bfe.conf.

func WaitForTCP

func WaitForTCP(addr string, timeout time.Duration) error

WaitForTCP waits until the given TCP address is reachable.

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
}

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) AuthHeaders

func (b *MockBackend) AuthHeaders() []string

AuthHeaders returns a deep copy of all observed Authorization headers.

func (*MockBackend) Close

func (b *MockBackend) Close()

Close shuts down the mock backend.

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.

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
	CreateTime  int64
	ExpiredTime int64
	Quota       int64
	ResetMode   int
	Unit        string
}

QuotaPlan is the JSON representation of a quota plan.

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) Close

func (s *RedisServer) Close()

Close stops the redis server.

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        int                   `json:"enabled"`
	Status         int                   `json:"status"`
	UpdateTime     int64                 `json:"update_time"`
	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.

type TokenRuleData

type TokenRuleData struct {
	Version    string
	QuotaPlans map[string][]QuotaPlan
	Tokens     map[string]map[string]TokenFile
	Config     map[string][]TokenRule
}

TokenRuleData holds the content of mod_ai_token_auth/token_rule.data.

Jump to

Keyboard shortcuts

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