| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162 |
- package service
- import (
- "context"
- "errors"
- "fmt"
- "math"
- "net/http"
- "sort"
- "strconv"
- "strings"
- "sync"
- "sync/atomic"
- "time"
- "github.com/Wei-Shaw/sub2api/internal/config"
- "github.com/Wei-Shaw/sub2api/internal/pkg/tlsfingerprint"
- "golang.org/x/sync/errgroup"
- )
- const (
- openAIWSConnMaxAge = 60 * time.Minute
- openAIWSConnHealthCheckIdle = 90 * time.Second
- // coder/websocket cannot consume pong frames without a reader. Recycle
- // unsupported idle sockets before the upstream keepalive window expires.
- openAIWSConnIdleRecycleAfter = 90 * time.Second
- openAIWSConnHealthCheckTO = 2 * time.Second
- openAIWSConnPrewarmExtraDelay = 2 * time.Second
- openAIWSAcquireCleanupInterval = 3 * time.Second
- openAIWSBackgroundPingInterval = 30 * time.Second
- openAIWSBackgroundSweepTicker = 30 * time.Second
- openAIWSPrewarmFailureWindow = 30 * time.Second
- openAIWSPrewarmFailureSuppress = 2
- )
- var (
- errOpenAIWSConnClosed = errors.New("openai ws connection closed")
- errOpenAIWSConnQueueFull = errors.New("openai ws connection queue full")
- errOpenAIWSPreferredConnUnavailable = errors.New("openai ws preferred connection unavailable")
- )
- type openAIWSDialError struct {
- StatusCode int
- ResponseHeaders http.Header
- ResponseBody []byte
- Err error
- }
- func (e *openAIWSDialError) Error() string {
- if e == nil {
- return ""
- }
- if e.StatusCode > 0 {
- return fmt.Sprintf("openai ws dial failed: status=%d err=%v", e.StatusCode, e.Err)
- }
- return fmt.Sprintf("openai ws dial failed: %v", e.Err)
- }
- func (e *openAIWSDialError) Unwrap() error {
- if e == nil {
- return nil
- }
- return e.Err
- }
- type openAIWSAcquireRequest struct {
- Account *Account
- WSURL string
- Headers http.Header
- // HeadersFactory is evaluated inside dialConn. It exists so credentials
- // whose authorization is per-dial (Agent Identity) are never cached in
- // lastAcquire or delayed prewarm state.
- HeadersFactory func(context.Context, http.Header) (http.Header, error)
- ProxyURL string
- PreferredConnID string
- // ForceNewConn: 强制本次获取新连接(避免复用导致连接内续链状态互相污染)。
- ForceNewConn bool
- // ForcePreferredConn: 强制本次只使用 PreferredConnID,禁止漂移到其它连接。
- ForcePreferredConn bool
- // Snapshot retained for delayed prewarm; never silently re-resolve a new
- // template under an old handshake compatibility key.
- tlsProfile *tlsfingerprint.Profile
- }
- type openAIWSHandshakeCompatibilityKey struct {
- betaFeatures string
- codexInstallationID string
- sessionIDHyphen string
- sessionIDUnderscore string
- threadID string
- clientRequestID string
- codexWindowID string
- conversationID string
- transportKey string
- }
- type openAIWSConnLease struct {
- pool *openAIWSConnPool
- accountID int64
- conn *openAIWSConn
- queueWait time.Duration
- connPick time.Duration
- reused bool
- released atomic.Bool
- }
- func (l *openAIWSConnLease) activeConn() (*openAIWSConn, error) {
- if l == nil || l.conn == nil {
- return nil, errOpenAIWSConnClosed
- }
- if l.released.Load() {
- return nil, errOpenAIWSConnClosed
- }
- return l.conn, nil
- }
- func (l *openAIWSConnLease) ConnID() string {
- if l == nil || l.conn == nil {
- return ""
- }
- return l.conn.id
- }
- func (l *openAIWSConnLease) QueueWaitDuration() time.Duration {
- if l == nil {
- return 0
- }
- return l.queueWait
- }
- func (l *openAIWSConnLease) ConnPickDuration() time.Duration {
- if l == nil {
- return 0
- }
- return l.connPick
- }
- func (l *openAIWSConnLease) Reused() bool {
- if l == nil {
- return false
- }
- return l.reused
- }
- func (l *openAIWSConnLease) HandshakeHeader(name string) string {
- if l == nil || l.conn == nil {
- return ""
- }
- return l.conn.handshakeHeader(name)
- }
- func (l *openAIWSConnLease) HandshakeHeaders() http.Header {
- if l == nil || l.conn == nil {
- return nil
- }
- return cloneHeader(l.conn.handshakeHeaders)
- }
- func (l *openAIWSConnLease) IsPrewarmed() bool {
- if l == nil || l.conn == nil {
- return false
- }
- return l.conn.isPrewarmed()
- }
- func (l *openAIWSConnLease) MarkPrewarmed() {
- if l == nil || l.conn == nil {
- return
- }
- l.conn.markPrewarmed()
- }
- func (l *openAIWSConnLease) WriteJSON(value any, timeout time.Duration) error {
- conn, err := l.activeConn()
- if err != nil {
- return err
- }
- return conn.writeJSONWithTimeout(context.Background(), value, timeout)
- }
- func (l *openAIWSConnLease) WriteJSONWithContextTimeout(ctx context.Context, value any, timeout time.Duration) error {
- conn, err := l.activeConn()
- if err != nil {
- return err
- }
- return conn.writeJSONWithTimeout(ctx, value, timeout)
- }
- func (l *openAIWSConnLease) WriteJSONContext(ctx context.Context, value any) error {
- conn, err := l.activeConn()
- if err != nil {
- return err
- }
- return conn.writeJSON(value, ctx)
- }
- func (l *openAIWSConnLease) ReadMessage(timeout time.Duration) ([]byte, error) {
- conn, err := l.activeConn()
- if err != nil {
- return nil, err
- }
- return conn.readMessageWithTimeout(timeout)
- }
- func (l *openAIWSConnLease) ReadMessageContext(ctx context.Context) ([]byte, error) {
- conn, err := l.activeConn()
- if err != nil {
- return nil, err
- }
- return conn.readMessage(ctx)
- }
- func (l *openAIWSConnLease) ReadMessageWithContextTimeout(ctx context.Context, timeout time.Duration) ([]byte, error) {
- conn, err := l.activeConn()
- if err != nil {
- return nil, err
- }
- return conn.readMessageWithContextTimeout(ctx, timeout)
- }
- func (l *openAIWSConnLease) PingWithTimeout(timeout time.Duration) error {
- conn, err := l.activeConn()
- if err != nil {
- return err
- }
- return conn.pingWithTimeout(timeout)
- }
- func (l *openAIWSConnLease) SupportsIdlePingWithoutReader() bool {
- conn, err := l.activeConn()
- if err != nil {
- return false
- }
- return conn.supportsIdlePingWithoutReader()
- }
- func (l *openAIWSConnLease) MarkBroken() {
- if l == nil || l.pool == nil || l.conn == nil || l.released.Load() {
- return
- }
- l.pool.evictConn(l.accountID, l.conn.id)
- }
- func (l *openAIWSConnLease) Release() {
- if l == nil || l.conn == nil {
- return
- }
- if !l.released.CompareAndSwap(false, true) {
- return
- }
- l.conn.release()
- if l.pool != nil {
- l.pool.notifyAccountPoolChanged(l.accountID)
- }
- }
- type openAIWSConn struct {
- id string
- ws openAIWSClientConn
- handshakeHeaders http.Header
- handshakeCompatibility openAIWSHandshakeCompatibilityKey
- routingAffinity string
- leaseCh chan struct{}
- closedCh chan struct{}
- closeOnce sync.Once
- readMu sync.Mutex
- writeMu sync.Mutex
- waiters atomic.Int32
- createdAtNano atomic.Int64
- lastUsedNano atomic.Int64
- prewarmed atomic.Bool
- }
- func newOpenAIWSConn(id string, _ int64, ws openAIWSClientConn, handshakeHeaders http.Header) *openAIWSConn {
- now := time.Now()
- conn := &openAIWSConn{
- id: id,
- ws: ws,
- handshakeHeaders: cloneHeader(handshakeHeaders),
- leaseCh: make(chan struct{}, 1),
- closedCh: make(chan struct{}),
- }
- conn.leaseCh <- struct{}{}
- conn.createdAtNano.Store(now.UnixNano())
- conn.lastUsedNano.Store(now.UnixNano())
- return conn
- }
- func (c *openAIWSConn) tryAcquire() bool {
- if c == nil {
- return false
- }
- select {
- case <-c.closedCh:
- return false
- default:
- }
- select {
- case <-c.leaseCh:
- select {
- case <-c.closedCh:
- c.release()
- return false
- default:
- }
- return true
- default:
- return false
- }
- }
- func (c *openAIWSConn) acquire(ctx context.Context) error {
- if c == nil {
- return errOpenAIWSConnClosed
- }
- for {
- select {
- case <-ctx.Done():
- return ctx.Err()
- case <-c.closedCh:
- return errOpenAIWSConnClosed
- case <-c.leaseCh:
- // A cancellation and a lease delivery can become ready together. Once
- // the semaphore token has been consumed, check the context again and
- // return it before reporting cancellation so a canceled waiter cannot
- // strand a pooled connection.
- if err := ctx.Err(); err != nil {
- c.release()
- return err
- }
- select {
- case <-c.closedCh:
- c.release()
- return errOpenAIWSConnClosed
- default:
- }
- return nil
- }
- }
- }
- func (c *openAIWSConn) release() {
- if c == nil {
- return
- }
- select {
- case c.leaseCh <- struct{}{}:
- default:
- }
- c.touch()
- }
- func (c *openAIWSConn) close() {
- if c == nil {
- return
- }
- c.closeOnce.Do(func() {
- close(c.closedCh)
- if c.ws != nil {
- _ = c.ws.Close()
- }
- select {
- case c.leaseCh <- struct{}{}:
- default:
- }
- })
- }
- func (c *openAIWSConn) writeJSONWithTimeout(parent context.Context, value any, timeout time.Duration) error {
- if c == nil {
- return errOpenAIWSConnClosed
- }
- select {
- case <-c.closedCh:
- return errOpenAIWSConnClosed
- default:
- }
- writeCtx := parent
- if writeCtx == nil {
- writeCtx = context.Background()
- }
- if timeout <= 0 {
- return c.writeJSON(value, writeCtx)
- }
- var cancel context.CancelFunc
- writeCtx, cancel = context.WithTimeout(writeCtx, timeout)
- defer cancel()
- return c.writeJSON(value, writeCtx)
- }
- func (c *openAIWSConn) writeJSON(value any, writeCtx context.Context) error {
- c.writeMu.Lock()
- defer c.writeMu.Unlock()
- if c.ws == nil {
- return errOpenAIWSConnClosed
- }
- if writeCtx == nil {
- writeCtx = context.Background()
- }
- if err := c.ws.WriteJSON(writeCtx, value); err != nil {
- return err
- }
- c.touch()
- return nil
- }
- func (c *openAIWSConn) readMessageWithTimeout(timeout time.Duration) ([]byte, error) {
- return c.readMessageWithContextTimeout(context.Background(), timeout)
- }
- func (c *openAIWSConn) readMessageWithContextTimeout(parent context.Context, timeout time.Duration) ([]byte, error) {
- if c == nil {
- return nil, errOpenAIWSConnClosed
- }
- select {
- case <-c.closedCh:
- return nil, errOpenAIWSConnClosed
- default:
- }
- if parent == nil {
- parent = context.Background()
- }
- if timeout <= 0 {
- return c.readMessage(parent)
- }
- readCtx, cancel := context.WithTimeout(parent, timeout)
- defer cancel()
- return c.readMessage(readCtx)
- }
- func (c *openAIWSConn) readMessage(readCtx context.Context) ([]byte, error) {
- c.readMu.Lock()
- defer c.readMu.Unlock()
- if c.ws == nil {
- return nil, errOpenAIWSConnClosed
- }
- if readCtx == nil {
- readCtx = context.Background()
- }
- payload, err := c.ws.ReadMessage(readCtx)
- if err != nil {
- return nil, err
- }
- c.touch()
- return payload, nil
- }
- func (c *openAIWSConn) pingWithTimeout(timeout time.Duration) error {
- if c == nil {
- return errOpenAIWSConnClosed
- }
- select {
- case <-c.closedCh:
- return errOpenAIWSConnClosed
- default:
- }
- c.writeMu.Lock()
- defer c.writeMu.Unlock()
- if c.ws == nil {
- return errOpenAIWSConnClosed
- }
- if timeout <= 0 {
- timeout = openAIWSConnHealthCheckTO
- }
- pingCtx, cancel := context.WithTimeout(context.Background(), timeout)
- defer cancel()
- if err := c.ws.Ping(pingCtx); err != nil {
- return err
- }
- return nil
- }
- func (c *openAIWSConn) supportsIdlePingWithoutReader() bool {
- if c == nil || c.ws == nil {
- return false
- }
- capable, ok := c.ws.(openAIWSIdlePingCapable)
- // Test and alternate implementations keep the historical probe behavior
- // unless they explicitly declare it unsafe.
- return !ok || capable.SupportsIdlePingWithoutReader()
- }
- func (c *openAIWSConn) touch() {
- if c == nil {
- return
- }
- c.lastUsedNano.Store(time.Now().UnixNano())
- }
- func (c *openAIWSConn) createdAt() time.Time {
- if c == nil {
- return time.Time{}
- }
- nano := c.createdAtNano.Load()
- if nano <= 0 {
- return time.Time{}
- }
- return time.Unix(0, nano)
- }
- func (c *openAIWSConn) lastUsedAt() time.Time {
- if c == nil {
- return time.Time{}
- }
- nano := c.lastUsedNano.Load()
- if nano <= 0 {
- return time.Time{}
- }
- return time.Unix(0, nano)
- }
- func (c *openAIWSConn) idleDuration(now time.Time) time.Duration {
- if c == nil {
- return 0
- }
- last := c.lastUsedAt()
- if last.IsZero() {
- return 0
- }
- return now.Sub(last)
- }
- func (c *openAIWSConn) age(now time.Time) time.Duration {
- if c == nil {
- return 0
- }
- created := c.createdAt()
- if created.IsZero() {
- return 0
- }
- return now.Sub(created)
- }
- func (c *openAIWSConn) isLeased() bool {
- if c == nil {
- return false
- }
- return len(c.leaseCh) == 0
- }
- func (c *openAIWSConn) handshakeHeader(name string) string {
- if c == nil || c.handshakeHeaders == nil {
- return ""
- }
- return strings.TrimSpace(c.handshakeHeaders.Get(strings.TrimSpace(name)))
- }
- func (c *openAIWSConn) matchesHandshakeCompatibility(compatibility openAIWSHandshakeCompatibilityKey) bool {
- return c != nil && c.handshakeCompatibility == compatibility
- }
- func (c *openAIWSConn) matchesRoutingAffinity(routingAffinity string) bool {
- return c != nil && c.routingAffinity == routingAffinity
- }
- func (c *openAIWSConn) isPrewarmed() bool {
- if c == nil {
- return false
- }
- return c.prewarmed.Load()
- }
- func (c *openAIWSConn) markPrewarmed() {
- if c == nil {
- return
- }
- c.prewarmed.Store(true)
- }
- type openAIWSAccountPool struct {
- mu sync.Mutex
- conns map[string]*openAIWSConn
- pinnedConns map[string]int
- changedCh chan struct{}
- creating int
- generation uint64
- lastCleanupAt time.Time
- lastAcquire *openAIWSAcquireRequest
- prewarmActive bool
- prewarmUntil time.Time
- prewarmFails int
- prewarmFailAt time.Time
- }
- func (ap *openAIWSAccountPool) changeChannelLocked() chan struct{} {
- if ap.changedCh == nil {
- ap.changedCh = make(chan struct{})
- }
- return ap.changedCh
- }
- func (ap *openAIWSAccountPool) signalChangedLocked() {
- if ap == nil {
- return
- }
- if ap.changedCh != nil {
- close(ap.changedCh)
- }
- ap.changedCh = make(chan struct{})
- }
- type OpenAIWSPoolMetricsSnapshot struct {
- AcquireTotal int64
- AcquireReuseTotal int64
- AcquireCreateTotal int64
- AcquireQueueWaitTotal int64
- AcquireQueueWaitMsTotal int64
- ConnPickTotal int64
- ConnPickMsTotal int64
- ScaleUpTotal int64
- ScaleDownTotal int64
- }
- type openAIWSPoolMetrics struct {
- acquireTotal atomic.Int64
- acquireReuseTotal atomic.Int64
- acquireCreateTotal atomic.Int64
- acquireQueueWaitTotal atomic.Int64
- acquireQueueWaitMs atomic.Int64
- connPickTotal atomic.Int64
- connPickMs atomic.Int64
- scaleUpTotal atomic.Int64
- scaleDownTotal atomic.Int64
- }
- type openAIWSConnPool struct {
- cfg *config.Config
- // 通过接口解耦底层 WS 客户端实现,默认使用 coder/websocket。
- clientDialer openAIWSClientDialer
- accounts sync.Map // key: int64(accountID), value: *openAIWSAccountPool
- seq atomic.Uint64
- metrics openAIWSPoolMetrics
- workerStopCh chan struct{}
- workerWg sync.WaitGroup
- closeOnce sync.Once
- }
- func newOpenAIWSConnPool(cfg *config.Config) *openAIWSConnPool {
- pool := &openAIWSConnPool{
- cfg: cfg,
- clientDialer: newDefaultOpenAIWSClientDialer(),
- workerStopCh: make(chan struct{}),
- }
- pool.startBackgroundWorkers()
- return pool
- }
- func (p *openAIWSConnPool) SnapshotMetrics() OpenAIWSPoolMetricsSnapshot {
- if p == nil {
- return OpenAIWSPoolMetricsSnapshot{}
- }
- return OpenAIWSPoolMetricsSnapshot{
- AcquireTotal: p.metrics.acquireTotal.Load(),
- AcquireReuseTotal: p.metrics.acquireReuseTotal.Load(),
- AcquireCreateTotal: p.metrics.acquireCreateTotal.Load(),
- AcquireQueueWaitTotal: p.metrics.acquireQueueWaitTotal.Load(),
- AcquireQueueWaitMsTotal: p.metrics.acquireQueueWaitMs.Load(),
- ConnPickTotal: p.metrics.connPickTotal.Load(),
- ConnPickMsTotal: p.metrics.connPickMs.Load(),
- ScaleUpTotal: p.metrics.scaleUpTotal.Load(),
- ScaleDownTotal: p.metrics.scaleDownTotal.Load(),
- }
- }
- func (p *openAIWSConnPool) SnapshotTransportMetrics() OpenAIWSTransportMetricsSnapshot {
- if p == nil {
- return OpenAIWSTransportMetricsSnapshot{}
- }
- if dialer, ok := p.clientDialer.(openAIWSTransportMetricsDialer); ok {
- return dialer.SnapshotTransportMetrics()
- }
- return OpenAIWSTransportMetricsSnapshot{}
- }
- func (p *openAIWSConnPool) setClientDialerForTest(dialer openAIWSClientDialer) {
- if p == nil || dialer == nil {
- return
- }
- p.clientDialer = dialer
- }
- // Close 停止后台 worker 并关闭所有空闲连接,应在优雅关闭时调用。
- func (p *openAIWSConnPool) Close() {
- if p == nil {
- return
- }
- p.closeOnce.Do(func() {
- if p.workerStopCh != nil {
- close(p.workerStopCh)
- }
- p.workerWg.Wait()
- // 遍历所有账户池,关闭全部空闲连接。
- p.accounts.Range(func(key, value any) bool {
- ap, ok := value.(*openAIWSAccountPool)
- if !ok || ap == nil {
- return true
- }
- ap.mu.Lock()
- for _, conn := range ap.conns {
- if conn != nil && !conn.isLeased() {
- conn.close()
- }
- }
- ap.mu.Unlock()
- return true
- })
- })
- }
- func (p *openAIWSConnPool) startBackgroundWorkers() {
- if p == nil || p.workerStopCh == nil {
- return
- }
- p.workerWg.Add(2)
- go func() {
- defer p.workerWg.Done()
- p.runBackgroundPingWorker()
- }()
- go func() {
- defer p.workerWg.Done()
- p.runBackgroundCleanupWorker()
- }()
- }
- type openAIWSIdlePingCandidate struct {
- accountID int64
- conn *openAIWSConn
- }
- func (p *openAIWSConnPool) runBackgroundPingWorker() {
- if p == nil {
- return
- }
- ticker := time.NewTicker(openAIWSBackgroundPingInterval)
- defer ticker.Stop()
- for {
- select {
- case <-ticker.C:
- p.runBackgroundPingSweep()
- case <-p.workerStopCh:
- return
- }
- }
- }
- func (p *openAIWSConnPool) runBackgroundPingSweep() {
- if p == nil {
- return
- }
- candidates := p.snapshotIdleConnsForPing()
- var g errgroup.Group
- g.SetLimit(10)
- for _, item := range candidates {
- item := item
- if item.conn == nil || item.conn.isLeased() || item.conn.waiters.Load() > 0 || !item.conn.supportsIdlePingWithoutReader() {
- continue
- }
- g.Go(func() error {
- if err := item.conn.pingWithTimeout(openAIWSConnHealthCheckTO); err != nil {
- p.evictConn(item.accountID, item.conn.id)
- }
- return nil
- })
- }
- _ = g.Wait()
- }
- func (p *openAIWSConnPool) snapshotIdleConnsForPing() []openAIWSIdlePingCandidate {
- if p == nil {
- return nil
- }
- candidates := make([]openAIWSIdlePingCandidate, 0)
- p.accounts.Range(func(key, value any) bool {
- accountID, ok := key.(int64)
- if !ok || accountID <= 0 {
- return true
- }
- ap, ok := value.(*openAIWSAccountPool)
- if !ok || ap == nil {
- return true
- }
- ap.mu.Lock()
- for _, conn := range ap.conns {
- if conn == nil || conn.isLeased() || conn.waiters.Load() > 0 {
- continue
- }
- candidates = append(candidates, openAIWSIdlePingCandidate{
- accountID: accountID,
- conn: conn,
- })
- }
- ap.mu.Unlock()
- return true
- })
- return candidates
- }
- func (p *openAIWSConnPool) runBackgroundCleanupWorker() {
- if p == nil {
- return
- }
- ticker := time.NewTicker(openAIWSBackgroundSweepTicker)
- defer ticker.Stop()
- for {
- select {
- case <-ticker.C:
- p.runBackgroundCleanupSweep(time.Now())
- case <-p.workerStopCh:
- return
- }
- }
- }
- func (p *openAIWSConnPool) runBackgroundCleanupSweep(now time.Time) {
- if p == nil {
- return
- }
- type cleanupResult struct {
- evicted []*openAIWSConn
- }
- results := make([]cleanupResult, 0)
- p.accounts.Range(func(_ any, value any) bool {
- ap, ok := value.(*openAIWSAccountPool)
- if !ok || ap == nil {
- return true
- }
- maxConns := p.maxConnsHardCap()
- ap.mu.Lock()
- if ap.lastAcquire != nil && ap.lastAcquire.Account != nil {
- maxConns = p.effectiveMaxConnsByAccount(ap.lastAcquire.Account)
- }
- evicted := p.cleanupAccountLocked(ap, now, maxConns)
- ap.lastCleanupAt = now
- ap.mu.Unlock()
- if len(evicted) > 0 {
- results = append(results, cleanupResult{evicted: evicted})
- }
- return true
- })
- for _, result := range results {
- closeOpenAIWSConns(result.evicted)
- }
- }
- func (p *openAIWSConnPool) Acquire(ctx context.Context, req openAIWSAcquireRequest) (*openAIWSConnLease, error) {
- if p != nil {
- p.metrics.acquireTotal.Add(1)
- }
- profile, err := resolveMode1TLSProfile(req.Account)
- if err != nil {
- return nil, err
- }
- if profile != nil && p != nil && p.cfg != nil && !p.cfg.Gateway.TLSFingerprint.Enabled {
- profile = nil
- }
- req.tlsProfile = profile
- return p.acquire(ctx, cloneOpenAIWSAcquireRequest(req), 0)
- }
- func (p *openAIWSConnPool) acquire(ctx context.Context, req openAIWSAcquireRequest, retry int) (*openAIWSConnLease, error) {
- if p == nil || req.Account == nil || req.Account.ID <= 0 {
- return nil, errors.New("invalid ws acquire request")
- }
- if stringsTrim(req.WSURL) == "" {
- return nil, errors.New("ws url is empty")
- }
- retryAcquire:
- accountID := req.Account.ID
- compatibility := openAIWSAcquireCompatibility(req)
- routingAffinity := normalizeOpenAIWSRoutingAffinity(req.Headers)
- effectiveMaxConns := p.effectiveMaxConnsByAccount(req.Account)
- if effectiveMaxConns <= 0 {
- return nil, errOpenAIWSConnQueueFull
- }
- var evicted []*openAIWSConn
- ap := p.getOrCreateAccountPool(accountID)
- ap.mu.Lock()
- acquireGeneration := ap.generation
- now := time.Now()
- if ap.lastCleanupAt.IsZero() || now.Sub(ap.lastCleanupAt) >= openAIWSAcquireCleanupInterval {
- evicted = p.cleanupAccountLocked(ap, now, effectiveMaxConns)
- ap.lastCleanupAt = now
- }
- pickStartedAt := time.Now()
- allowReuse := !req.ForceNewConn
- preferredConnID := stringsTrim(req.PreferredConnID)
- forcePreferredConn := allowReuse && req.ForcePreferredConn
- if allowReuse {
- if forcePreferredConn {
- if preferredConnID == "" {
- p.recordConnPickDuration(time.Since(pickStartedAt))
- ap.mu.Unlock()
- closeOpenAIWSConns(evicted)
- return nil, errOpenAIWSPreferredConnUnavailable
- }
- preferredConn, ok := ap.conns[preferredConnID]
- if !ok || !preferredConn.matchesHandshakeCompatibility(compatibility) {
- p.recordConnPickDuration(time.Since(pickStartedAt))
- ap.mu.Unlock()
- closeOpenAIWSConns(evicted)
- return nil, errOpenAIWSPreferredConnUnavailable
- }
- if preferredConn.tryAcquire() {
- connPick := time.Since(pickStartedAt)
- p.recordConnPickDuration(connPick)
- ap.mu.Unlock()
- closeOpenAIWSConns(evicted)
- if p.shouldHealthCheckConn(preferredConn) {
- if err := preferredConn.pingWithTimeout(openAIWSConnHealthCheckTO); err != nil {
- preferredConn.close()
- p.evictConn(accountID, preferredConn.id)
- if retry < 1 {
- return p.acquire(ctx, req, retry+1)
- }
- return nil, err
- }
- }
- lease := &openAIWSConnLease{
- pool: p,
- accountID: accountID,
- conn: preferredConn,
- connPick: connPick,
- reused: true,
- }
- p.metrics.acquireReuseTotal.Add(1)
- p.recordLastSuccessfulAcquire(accountID, acquireGeneration, req)
- p.ensureTargetIdleAsync(accountID)
- return lease, nil
- }
- connPick := time.Since(pickStartedAt)
- p.recordConnPickDuration(connPick)
- if int(preferredConn.waiters.Load()) >= p.queueLimitPerConn() {
- ap.mu.Unlock()
- closeOpenAIWSConns(evicted)
- return nil, errOpenAIWSConnQueueFull
- }
- preferredConn.waiters.Add(1)
- ap.mu.Unlock()
- closeOpenAIWSConns(evicted)
- defer preferredConn.waiters.Add(-1)
- waitStart := time.Now()
- p.metrics.acquireQueueWaitTotal.Add(1)
- if err := preferredConn.acquire(ctx); err != nil {
- if errors.Is(err, errOpenAIWSConnClosed) && retry < 1 {
- return p.acquire(ctx, req, retry+1)
- }
- return nil, err
- }
- if p.shouldHealthCheckConn(preferredConn) {
- if err := preferredConn.pingWithTimeout(openAIWSConnHealthCheckTO); err != nil {
- preferredConn.release()
- preferredConn.close()
- p.evictConn(accountID, preferredConn.id)
- if retry < 1 {
- return p.acquire(ctx, req, retry+1)
- }
- return nil, err
- }
- }
- queueWait := time.Since(waitStart)
- p.metrics.acquireQueueWaitMs.Add(queueWait.Milliseconds())
- lease := &openAIWSConnLease{
- pool: p,
- accountID: accountID,
- conn: preferredConn,
- queueWait: queueWait,
- connPick: connPick,
- reused: true,
- }
- p.metrics.acquireReuseTotal.Add(1)
- p.recordLastSuccessfulAcquire(accountID, acquireGeneration, req)
- p.ensureTargetIdleAsync(accountID)
- return lease, nil
- }
- if preferredConnID != "" {
- if conn, ok := ap.conns[preferredConnID]; ok && conn.matchesHandshakeCompatibility(compatibility) && conn.tryAcquire() {
- connPick := time.Since(pickStartedAt)
- p.recordConnPickDuration(connPick)
- ap.mu.Unlock()
- closeOpenAIWSConns(evicted)
- if p.shouldHealthCheckConn(conn) {
- if err := conn.pingWithTimeout(openAIWSConnHealthCheckTO); err != nil {
- conn.close()
- p.evictConn(accountID, conn.id)
- if retry < 1 {
- return p.acquire(ctx, req, retry+1)
- }
- return nil, err
- }
- }
- lease := &openAIWSConnLease{pool: p, accountID: accountID, conn: conn, connPick: connPick, reused: true}
- p.metrics.acquireReuseTotal.Add(1)
- p.recordLastSuccessfulAcquire(accountID, acquireGeneration, req)
- p.ensureTargetIdleAsync(accountID)
- return lease, nil
- }
- }
- // A routing hint is advisory at WebSocket dial time. Prefer a pooled
- // connection whose handshake used the same hint, but do not make that
- // preference a continuation compatibility requirement.
- best := p.pickLeastBusyConnWithRoutingAffinityLocked(ap, compatibility, routingAffinity)
- if best != nil && best.tryAcquire() {
- connPick := time.Since(pickStartedAt)
- p.recordConnPickDuration(connPick)
- ap.mu.Unlock()
- closeOpenAIWSConns(evicted)
- if p.shouldHealthCheckConn(best) {
- if err := best.pingWithTimeout(openAIWSConnHealthCheckTO); err != nil {
- best.close()
- p.evictConn(accountID, best.id)
- if retry < 1 {
- return p.acquire(ctx, req, retry+1)
- }
- return nil, err
- }
- }
- lease := &openAIWSConnLease{pool: p, accountID: accountID, conn: best, connPick: connPick, reused: true}
- p.metrics.acquireReuseTotal.Add(1)
- p.recordLastSuccessfulAcquire(accountID, acquireGeneration, req)
- p.ensureTargetIdleAsync(accountID)
- return lease, nil
- }
- if routingAffinity == "" || len(ap.conns)+ap.creating >= effectiveMaxConns {
- for _, conn := range ap.conns {
- if conn == nil || conn == best || !conn.matchesHandshakeCompatibility(compatibility) {
- continue
- }
- if conn.tryAcquire() {
- connPick := time.Since(pickStartedAt)
- p.recordConnPickDuration(connPick)
- ap.mu.Unlock()
- closeOpenAIWSConns(evicted)
- if p.shouldHealthCheckConn(conn) {
- if err := conn.pingWithTimeout(openAIWSConnHealthCheckTO); err != nil {
- conn.close()
- p.evictConn(accountID, conn.id)
- if retry < 1 {
- return p.acquire(ctx, req, retry+1)
- }
- return nil, err
- }
- }
- lease := &openAIWSConnLease{pool: p, accountID: accountID, conn: conn, connPick: connPick, reused: true}
- p.metrics.acquireReuseTotal.Add(1)
- p.recordLastSuccessfulAcquire(accountID, acquireGeneration, req)
- p.ensureTargetIdleAsync(accountID)
- return lease, nil
- }
- }
- }
- }
- if !req.ForceNewConn && len(ap.conns)+ap.creating >= effectiveMaxConns {
- affine := p.pickLeastBusyConnWithRoutingAffinityLocked(ap, compatibility, routingAffinity)
- if idle := p.pickOldestIdleConnWithoutHandshakeCompatibilityLocked(ap, compatibility); idle != nil {
- delete(ap.conns, idle.id)
- evicted = append(evicted, idle)
- p.metrics.scaleDownTotal.Add(1)
- } else if affine == nil {
- compatible := p.pickLeastBusyConnLocked(ap, "", compatibility)
- if compatible != nil {
- // Capacity is full and every compatible connection is busy. The
- // hint remains soft here: queue on a compatible connection below.
- goto acquireAtCapacity
- }
- hasConnection := false
- for _, conn := range ap.conns {
- if conn != nil {
- hasConnection = true
- break
- }
- }
- if !hasConnection && ap.creating == 0 {
- ap.mu.Unlock()
- closeOpenAIWSConns(evicted)
- return nil, errOpenAIWSConnClosed
- }
- changedCh := ap.changeChannelLocked()
- ap.mu.Unlock()
- closeOpenAIWSConns(evicted)
- select {
- case <-ctx.Done():
- return nil, ctx.Err()
- case <-changedCh:
- goto retryAcquire
- }
- }
- }
- if req.ForceNewConn && len(ap.conns)+ap.creating >= effectiveMaxConns {
- if idle := p.pickOldestIdleConnLocked(ap); idle != nil {
- delete(ap.conns, idle.id)
- evicted = append(evicted, idle)
- p.metrics.scaleDownTotal.Add(1)
- }
- }
- if len(ap.conns)+ap.creating < effectiveMaxConns {
- connPick := time.Since(pickStartedAt)
- p.recordConnPickDuration(connPick)
- ap.creating++
- ap.mu.Unlock()
- closeOpenAIWSConns(evicted)
- conn, dialErr := p.dialConn(ctx, req)
- ap = p.getOrCreateAccountPool(accountID)
- ap.mu.Lock()
- ap.creating--
- if ap.generation != acquireGeneration {
- ap.signalChangedLocked()
- ap.mu.Unlock()
- if conn != nil {
- conn.close()
- }
- if retry < 1 {
- return p.acquire(ctx, req, retry+1)
- }
- return nil, errOpenAIWSConnClosed
- }
- if dialErr != nil {
- ap.prewarmFails++
- ap.prewarmFailAt = time.Now()
- ap.signalChangedLocked()
- ap.mu.Unlock()
- return nil, dialErr
- }
- // Claim the freshly dialed connection before publishing it. Otherwise a
- // topology waiter awakened below can take the free semaphore first and
- // make the caller that paid for the dial queue behind it.
- if !conn.tryAcquire() {
- ap.signalChangedLocked()
- ap.mu.Unlock()
- conn.close()
- return nil, errOpenAIWSConnClosed
- }
- ap.conns[conn.id] = conn
- ap.prewarmFails = 0
- ap.prewarmFailAt = time.Time{}
- // Wake acquires that observed creating>0 with no compatible connection.
- // Without this signal they can remain asleep until the new lease is
- // released, even though the pool topology already changed.
- ap.signalChangedLocked()
- ap.mu.Unlock()
- p.metrics.acquireCreateTotal.Add(1)
- lease := &openAIWSConnLease{pool: p, accountID: accountID, conn: conn, connPick: connPick}
- p.recordLastSuccessfulAcquire(accountID, acquireGeneration, req)
- p.ensureTargetIdleAsync(accountID)
- return lease, nil
- }
- if req.ForceNewConn {
- p.recordConnPickDuration(time.Since(pickStartedAt))
- ap.mu.Unlock()
- closeOpenAIWSConns(evicted)
- return nil, errOpenAIWSConnQueueFull
- }
- acquireAtCapacity:
- target := p.pickLeastBusyConnLocked(ap, req.PreferredConnID, compatibility)
- connPick := time.Since(pickStartedAt)
- p.recordConnPickDuration(connPick)
- if target == nil {
- ap.mu.Unlock()
- closeOpenAIWSConns(evicted)
- return nil, errOpenAIWSConnClosed
- }
- if int(target.waiters.Load()) >= p.queueLimitPerConn() {
- ap.mu.Unlock()
- closeOpenAIWSConns(evicted)
- return nil, errOpenAIWSConnQueueFull
- }
- target.waiters.Add(1)
- ap.mu.Unlock()
- closeOpenAIWSConns(evicted)
- defer target.waiters.Add(-1)
- waitStart := time.Now()
- p.metrics.acquireQueueWaitTotal.Add(1)
- if err := target.acquire(ctx); err != nil {
- if errors.Is(err, errOpenAIWSConnClosed) && retry < 1 {
- return p.acquire(ctx, req, retry+1)
- }
- return nil, err
- }
- if p.shouldHealthCheckConn(target) {
- if err := target.pingWithTimeout(openAIWSConnHealthCheckTO); err != nil {
- target.release()
- target.close()
- p.evictConn(accountID, target.id)
- if retry < 1 {
- return p.acquire(ctx, req, retry+1)
- }
- return nil, err
- }
- }
- queueWait := time.Since(waitStart)
- p.metrics.acquireQueueWaitMs.Add(queueWait.Milliseconds())
- lease := &openAIWSConnLease{pool: p, accountID: accountID, conn: target, queueWait: queueWait, connPick: connPick, reused: true}
- p.metrics.acquireReuseTotal.Add(1)
- p.recordLastSuccessfulAcquire(accountID, acquireGeneration, req)
- p.ensureTargetIdleAsync(accountID)
- return lease, nil
- }
- func (p *openAIWSConnPool) recordConnPickDuration(duration time.Duration) {
- if p == nil {
- return
- }
- if duration < 0 {
- duration = 0
- }
- p.metrics.connPickTotal.Add(1)
- p.metrics.connPickMs.Add(duration.Milliseconds())
- }
- func (p *openAIWSConnPool) recordLastSuccessfulAcquire(accountID int64, generation uint64, req openAIWSAcquireRequest) {
- if p == nil || accountID <= 0 {
- return
- }
- ap, ok := p.getAccountPool(accountID)
- if !ok || ap == nil {
- return
- }
- ap.mu.Lock()
- if ap.generation != generation {
- ap.mu.Unlock()
- return
- }
- ap.lastAcquire = cloneOpenAIWSAcquireRequestPtr(&req)
- ap.mu.Unlock()
- }
- func (p *openAIWSConnPool) pickOldestIdleConnLocked(ap *openAIWSAccountPool) *openAIWSConn {
- if ap == nil || len(ap.conns) == 0 {
- return nil
- }
- var oldest *openAIWSConn
- for _, conn := range ap.conns {
- if conn == nil || conn.isLeased() || conn.waiters.Load() > 0 || p.isConnPinnedLocked(ap, conn.id) {
- continue
- }
- if oldest == nil || conn.lastUsedAt().Before(oldest.lastUsedAt()) {
- oldest = conn
- }
- }
- return oldest
- }
- func (p *openAIWSConnPool) pickOldestIdleConnWithoutHandshakeCompatibilityLocked(
- ap *openAIWSAccountPool,
- compatibility openAIWSHandshakeCompatibilityKey,
- ) *openAIWSConn {
- if ap == nil || len(ap.conns) == 0 {
- return nil
- }
- var oldest *openAIWSConn
- for _, conn := range ap.conns {
- if conn == nil ||
- conn.matchesHandshakeCompatibility(compatibility) ||
- conn.isLeased() || conn.waiters.Load() > 0 || p.isConnPinnedLocked(ap, conn.id) {
- continue
- }
- if oldest == nil || conn.lastUsedAt().Before(oldest.lastUsedAt()) {
- oldest = conn
- }
- }
- return oldest
- }
- func (p *openAIWSConnPool) getOrCreateAccountPool(accountID int64) *openAIWSAccountPool {
- if p == nil || accountID <= 0 {
- return nil
- }
- if existing, ok := p.accounts.Load(accountID); ok {
- if ap, typed := existing.(*openAIWSAccountPool); typed && ap != nil {
- return ap
- }
- }
- ap := &openAIWSAccountPool{
- conns: make(map[string]*openAIWSConn),
- pinnedConns: make(map[string]int),
- changedCh: make(chan struct{}),
- }
- actual, _ := p.accounts.LoadOrStore(accountID, ap)
- if typed, ok := actual.(*openAIWSAccountPool); ok && typed != nil {
- return typed
- }
- return ap
- }
- // ensureAccountPoolLocked 兼容旧调用。
- func (p *openAIWSConnPool) ensureAccountPoolLocked(accountID int64) *openAIWSAccountPool {
- return p.getOrCreateAccountPool(accountID)
- }
- func (p *openAIWSConnPool) getAccountPool(accountID int64) (*openAIWSAccountPool, bool) {
- if p == nil || accountID <= 0 {
- return nil, false
- }
- value, ok := p.accounts.Load(accountID)
- if !ok || value == nil {
- return nil, false
- }
- ap, typed := value.(*openAIWSAccountPool)
- return ap, typed && ap != nil
- }
- func (p *openAIWSConnPool) notifyAccountPoolChanged(accountID int64) {
- ap, ok := p.getAccountPool(accountID)
- if !ok || ap == nil {
- return
- }
- ap.mu.Lock()
- ap.signalChangedLocked()
- ap.mu.Unlock()
- }
- func (p *openAIWSConnPool) isConnPinnedLocked(ap *openAIWSAccountPool, connID string) bool {
- if ap == nil || connID == "" || len(ap.pinnedConns) == 0 {
- return false
- }
- return ap.pinnedConns[connID] > 0
- }
- func (p *openAIWSConnPool) cleanupAccountLocked(ap *openAIWSAccountPool, now time.Time, maxConns int) []*openAIWSConn {
- if ap == nil {
- return nil
- }
- maxAge := p.maxConnAge()
- evicted := make([]*openAIWSConn, 0)
- for id, conn := range ap.conns {
- if conn == nil {
- delete(ap.conns, id)
- if len(ap.pinnedConns) > 0 {
- delete(ap.pinnedConns, id)
- }
- continue
- }
- select {
- case <-conn.closedCh:
- delete(ap.conns, id)
- if len(ap.pinnedConns) > 0 {
- delete(ap.pinnedConns, id)
- }
- evicted = append(evicted, conn)
- continue
- default:
- }
- if p.isConnPinnedLocked(ap, id) {
- continue
- }
- if !conn.isLeased() && conn.waiters.Load() == 0 &&
- !conn.supportsIdlePingWithoutReader() &&
- conn.idleDuration(now) >= openAIWSConnIdleRecycleAfter {
- delete(ap.conns, id)
- if len(ap.pinnedConns) > 0 {
- delete(ap.pinnedConns, id)
- }
- evicted = append(evicted, conn)
- p.metrics.scaleDownTotal.Add(1)
- continue
- }
- if maxAge > 0 && !conn.isLeased() && conn.age(now) > maxAge {
- delete(ap.conns, id)
- if len(ap.pinnedConns) > 0 {
- delete(ap.pinnedConns, id)
- }
- evicted = append(evicted, conn)
- }
- }
- if maxConns <= 0 {
- maxConns = p.maxConnsHardCap()
- }
- maxIdle := p.maxIdlePerAccount()
- if maxIdle < 0 || maxIdle > maxConns {
- maxIdle = maxConns
- }
- if maxIdle >= 0 && len(ap.conns) > maxIdle {
- idleConns := make([]*openAIWSConn, 0, len(ap.conns))
- for id, conn := range ap.conns {
- if conn == nil {
- delete(ap.conns, id)
- if len(ap.pinnedConns) > 0 {
- delete(ap.pinnedConns, id)
- }
- continue
- }
- // 有等待者的连接不能在清理阶段被淘汰,否则等待中的 acquire 会收到 closed 错误。
- if conn.isLeased() || conn.waiters.Load() > 0 || p.isConnPinnedLocked(ap, conn.id) {
- continue
- }
- idleConns = append(idleConns, conn)
- }
- sort.SliceStable(idleConns, func(i, j int) bool {
- return idleConns[i].lastUsedAt().Before(idleConns[j].lastUsedAt())
- })
- redundant := len(ap.conns) - maxIdle
- if redundant > len(idleConns) {
- redundant = len(idleConns)
- }
- for i := 0; i < redundant; i++ {
- conn := idleConns[i]
- delete(ap.conns, conn.id)
- if len(ap.pinnedConns) > 0 {
- delete(ap.pinnedConns, conn.id)
- }
- evicted = append(evicted, conn)
- }
- if redundant > 0 {
- p.metrics.scaleDownTotal.Add(int64(redundant))
- }
- }
- if len(evicted) > 0 {
- ap.signalChangedLocked()
- }
- return evicted
- }
- func (p *openAIWSConnPool) pickLeastBusyConnLocked(
- ap *openAIWSAccountPool,
- preferredConnID string,
- compatibility openAIWSHandshakeCompatibilityKey,
- ) *openAIWSConn {
- if ap == nil || len(ap.conns) == 0 {
- return nil
- }
- preferredConnID = stringsTrim(preferredConnID)
- if preferredConnID != "" {
- if conn, ok := ap.conns[preferredConnID]; ok && conn.matchesHandshakeCompatibility(compatibility) {
- return conn
- }
- }
- var best *openAIWSConn
- var bestWaiters int32
- var bestLastUsed time.Time
- for _, conn := range ap.conns {
- if conn == nil || !conn.matchesHandshakeCompatibility(compatibility) {
- continue
- }
- waiters := conn.waiters.Load()
- lastUsed := conn.lastUsedAt()
- if best == nil ||
- waiters < bestWaiters ||
- (waiters == bestWaiters && lastUsed.Before(bestLastUsed)) {
- best = conn
- bestWaiters = waiters
- bestLastUsed = lastUsed
- }
- }
- return best
- }
- func (p *openAIWSConnPool) pickLeastBusyConnWithRoutingAffinityLocked(
- ap *openAIWSAccountPool,
- compatibility openAIWSHandshakeCompatibilityKey,
- routingAffinity string,
- ) *openAIWSConn {
- if ap == nil || len(ap.conns) == 0 {
- return nil
- }
- var best *openAIWSConn
- var bestWaiters int32
- var bestLastUsed time.Time
- for _, conn := range ap.conns {
- if conn == nil ||
- !conn.matchesHandshakeCompatibility(compatibility) ||
- !conn.matchesRoutingAffinity(routingAffinity) {
- continue
- }
- waiters := conn.waiters.Load()
- lastUsed := conn.lastUsedAt()
- if best == nil ||
- waiters < bestWaiters ||
- (waiters == bestWaiters && lastUsed.Before(bestLastUsed)) {
- best = conn
- bestWaiters = waiters
- bestLastUsed = lastUsed
- }
- }
- return best
- }
- func accountPoolLoadLocked(ap *openAIWSAccountPool) (inflight int, waiters int) {
- if ap == nil {
- return 0, 0
- }
- for _, conn := range ap.conns {
- if conn == nil {
- continue
- }
- if conn.isLeased() {
- inflight++
- }
- waiters += int(conn.waiters.Load())
- }
- return inflight, waiters
- }
- // AccountPoolLoad 返回指定账号连接池的并发与排队快照。
- func (p *openAIWSConnPool) AccountPoolLoad(accountID int64) (inflight int, waiters int, conns int) {
- if p == nil || accountID <= 0 {
- return 0, 0, 0
- }
- ap, ok := p.getAccountPool(accountID)
- if !ok || ap == nil {
- return 0, 0, 0
- }
- ap.mu.Lock()
- defer ap.mu.Unlock()
- inflight, waiters = accountPoolLoadLocked(ap)
- return inflight, waiters, len(ap.conns)
- }
- func (p *openAIWSConnPool) ensureTargetIdleAsync(accountID int64) {
- if p == nil || accountID <= 0 {
- return
- }
- var req openAIWSAcquireRequest
- generation := uint64(0)
- need := 0
- ap, ok := p.getAccountPool(accountID)
- if !ok || ap == nil {
- return
- }
- ap.mu.Lock()
- defer ap.mu.Unlock()
- if ap.lastAcquire == nil {
- return
- }
- if ap.prewarmActive {
- return
- }
- now := time.Now()
- if !ap.prewarmUntil.IsZero() && now.Before(ap.prewarmUntil) {
- return
- }
- if p.shouldSuppressPrewarmLocked(ap, now) {
- return
- }
- effectiveMaxConns := p.maxConnsHardCap()
- if ap.lastAcquire != nil && ap.lastAcquire.Account != nil {
- effectiveMaxConns = p.effectiveMaxConnsByAccount(ap.lastAcquire.Account)
- }
- target := p.targetConnCountLocked(ap, effectiveMaxConns)
- current := len(ap.conns) + ap.creating
- if current >= target {
- return
- }
- need = target - current
- if need <= 0 {
- return
- }
- req = cloneOpenAIWSAcquireRequest(*ap.lastAcquire)
- generation = ap.generation
- ap.prewarmActive = true
- if cooldown := p.prewarmCooldown(); cooldown > 0 {
- ap.prewarmUntil = now.Add(cooldown)
- }
- ap.creating += need
- p.metrics.scaleUpTotal.Add(int64(need))
- go p.prewarmConns(accountID, req, need, generation)
- }
- func (p *openAIWSConnPool) targetConnCountLocked(ap *openAIWSAccountPool, maxConns int) int {
- if ap == nil {
- return 0
- }
- if maxConns <= 0 {
- return 0
- }
- minIdle := p.minIdlePerAccount()
- if minIdle < 0 {
- minIdle = 0
- }
- if minIdle > maxConns {
- minIdle = maxConns
- }
- inflight, waiters := accountPoolLoadLocked(ap)
- utilization := p.targetUtilization()
- demand := inflight + waiters
- if demand <= 0 {
- return minIdle
- }
- target := 1
- if demand > 1 {
- target = int(math.Ceil(float64(demand) / utilization))
- }
- if waiters > 0 && target < len(ap.conns)+1 {
- target = len(ap.conns) + 1
- }
- if target < minIdle {
- target = minIdle
- }
- if target > maxConns {
- target = maxConns
- }
- return target
- }
- func (p *openAIWSConnPool) prewarmConns(accountID int64, req openAIWSAcquireRequest, total int, generations ...uint64) {
- generation := uint64(0)
- if len(generations) > 0 {
- generation = generations[0]
- }
- staleTarget := false
- defer func() {
- if ap, ok := p.getAccountPool(accountID); ok && ap != nil {
- ap.mu.Lock()
- ap.prewarmActive = false
- ap.signalChangedLocked()
- ap.mu.Unlock()
- }
- if staleTarget {
- // A newer acquire arrived while the old dial was in flight. Re-run
- // target selection only after clearing prewarmActive so the latest
- // beta/hint target can fill the idle budget.
- p.ensureTargetIdleAsync(accountID)
- }
- }()
- for i := 0; i < total; i++ {
- ctx, cancel := context.WithTimeout(context.Background(), p.dialTimeout()+openAIWSConnPrewarmExtraDelay)
- conn, err := p.dialConn(ctx, req)
- cancel()
- ap, ok := p.getAccountPool(accountID)
- if !ok || ap == nil {
- if conn != nil {
- conn.close()
- }
- return
- }
- ap.mu.Lock()
- if ap.creating > 0 {
- ap.creating--
- }
- if err != nil {
- ap.prewarmFails++
- ap.prewarmFailAt = time.Now()
- ap.signalChangedLocked()
- ap.mu.Unlock()
- continue
- }
- if ap.generation != generation || ap.lastAcquire == nil {
- ap.mu.Unlock()
- conn.close()
- continue
- }
- if !sameOpenAIWSPrewarmTarget(req, *ap.lastAcquire) {
- staleTarget = true
- ap.signalChangedLocked()
- ap.mu.Unlock()
- conn.close()
- continue
- }
- if len(ap.conns) >= p.effectiveMaxConnsByAccount(req.Account) {
- ap.signalChangedLocked()
- ap.mu.Unlock()
- conn.close()
- continue
- }
- ap.conns[conn.id] = conn
- ap.prewarmFails = 0
- ap.prewarmFailAt = time.Time{}
- ap.signalChangedLocked()
- ap.mu.Unlock()
- }
- }
- // ClearAccount closes all pooled connections and discards delayed prewarm
- // state for one account. The generation guard prevents an in-flight prewarm
- // started before credential recovery from re-entering the pool afterwards.
- func (p *openAIWSConnPool) ClearAccount(accountID int64) {
- if p == nil || accountID <= 0 {
- return
- }
- ap, ok := p.getAccountPool(accountID)
- if !ok || ap == nil {
- return
- }
- ap.mu.Lock()
- ap.generation++
- conns := make([]*openAIWSConn, 0, len(ap.conns))
- for id, conn := range ap.conns {
- delete(ap.conns, id)
- delete(ap.pinnedConns, id)
- if conn != nil {
- conns = append(conns, conn)
- }
- }
- ap.lastAcquire = nil
- ap.prewarmUntil = time.Time{}
- ap.prewarmFails = 0
- ap.prewarmFailAt = time.Time{}
- ap.signalChangedLocked()
- ap.mu.Unlock()
- closeOpenAIWSConns(conns)
- }
- func (p *openAIWSConnPool) evictConn(accountID int64, connID string) {
- if p == nil || accountID <= 0 || stringsTrim(connID) == "" {
- return
- }
- var conn *openAIWSConn
- ap, ok := p.getAccountPool(accountID)
- if ok && ap != nil {
- ap.mu.Lock()
- if c, exists := ap.conns[connID]; exists {
- conn = c
- delete(ap.conns, connID)
- if len(ap.pinnedConns) > 0 {
- delete(ap.pinnedConns, connID)
- }
- ap.signalChangedLocked()
- }
- ap.mu.Unlock()
- }
- if conn != nil {
- conn.close()
- }
- }
- func (p *openAIWSConnPool) PinConn(accountID int64, connID string) bool {
- if p == nil || accountID <= 0 {
- return false
- }
- connID = stringsTrim(connID)
- if connID == "" {
- return false
- }
- ap, ok := p.getAccountPool(accountID)
- if !ok || ap == nil {
- return false
- }
- ap.mu.Lock()
- defer ap.mu.Unlock()
- if _, exists := ap.conns[connID]; !exists {
- return false
- }
- if ap.pinnedConns == nil {
- ap.pinnedConns = make(map[string]int)
- }
- ap.pinnedConns[connID]++
- return true
- }
- func (p *openAIWSConnPool) UnpinConn(accountID int64, connID string) {
- if p == nil || accountID <= 0 {
- return
- }
- connID = stringsTrim(connID)
- if connID == "" {
- return
- }
- ap, ok := p.getAccountPool(accountID)
- if !ok || ap == nil {
- return
- }
- ap.mu.Lock()
- defer ap.mu.Unlock()
- if len(ap.pinnedConns) == 0 {
- return
- }
- count := ap.pinnedConns[connID]
- if count <= 1 {
- delete(ap.pinnedConns, connID)
- ap.signalChangedLocked()
- return
- }
- ap.pinnedConns[connID] = count - 1
- ap.signalChangedLocked()
- }
- func (p *openAIWSConnPool) dialConn(ctx context.Context, req openAIWSAcquireRequest) (*openAIWSConn, error) {
- if p == nil || p.clientDialer == nil {
- return nil, errors.New("openai ws client dialer is nil")
- }
- headers := cloneHeader(req.Headers)
- var err error
- if req.HeadersFactory != nil {
- headers, err = req.HeadersFactory(ctx, headers)
- if err != nil {
- return nil, err
- }
- }
- dialCtx := withOpenAIWSTLSProfile(ctx, req.Account.ID, req.tlsProfile)
- conn, status, handshakeHeaders, err := p.clientDialer.Dial(dialCtx, req.WSURL, headers, req.ProxyURL)
- if err != nil {
- var handshakeErr *openAIWSHandshakeError
- var responseBody []byte
- if errors.As(err, &handshakeErr) && handshakeErr != nil {
- responseBody = append([]byte(nil), handshakeErr.Body...)
- }
- return nil, &openAIWSDialError{
- StatusCode: status,
- ResponseHeaders: cloneHeader(handshakeHeaders),
- ResponseBody: responseBody,
- Err: err,
- }
- }
- if conn == nil {
- return nil, &openAIWSDialError{
- StatusCode: status,
- ResponseHeaders: cloneHeader(handshakeHeaders),
- Err: errors.New("openai ws dialer returned nil connection"),
- }
- }
- id := p.nextConnID(req.Account.ID)
- pooledConn := newOpenAIWSConn(id, req.Account.ID, conn, handshakeHeaders)
- pooledConn.handshakeCompatibility = openAIWSAcquireCompatibility(req)
- pooledConn.routingAffinity = normalizeOpenAIWSRoutingAffinity(req.Headers)
- return pooledConn, nil
- }
- func (p *openAIWSConnPool) nextConnID(accountID int64) string {
- seq := p.seq.Add(1)
- buf := make([]byte, 0, 32)
- buf = append(buf, "oa_ws_"...)
- buf = strconv.AppendInt(buf, accountID, 10)
- buf = append(buf, '_')
- buf = strconv.AppendUint(buf, seq, 10)
- return string(buf)
- }
- func (p *openAIWSConnPool) shouldHealthCheckConn(conn *openAIWSConn) bool {
- if conn == nil || !conn.supportsIdlePingWithoutReader() {
- return false
- }
- return conn.idleDuration(time.Now()) >= openAIWSConnHealthCheckIdle
- }
- func (p *openAIWSConnPool) maxConnsHardCap() int {
- if p != nil && p.cfg != nil && p.cfg.Gateway.OpenAIWS.MaxConnsPerAccount > 0 {
- return p.cfg.Gateway.OpenAIWS.MaxConnsPerAccount
- }
- return 8
- }
- func (p *openAIWSConnPool) dynamicMaxConnsEnabled() bool {
- if p != nil && p.cfg != nil {
- return p.cfg.Gateway.OpenAIWS.DynamicMaxConnsByAccountConcurrencyEnabled
- }
- return false
- }
- func (p *openAIWSConnPool) modeRouterV2Enabled() bool {
- if p != nil && p.cfg != nil {
- return p.cfg.Gateway.OpenAIWS.ModeRouterV2Enabled
- }
- return false
- }
- func (p *openAIWSConnPool) maxConnsFactorByAccount(account *Account) float64 {
- if p == nil || p.cfg == nil || account == nil {
- return 1.0
- }
- switch account.Type {
- case AccountTypeOAuth:
- if p.cfg.Gateway.OpenAIWS.OAuthMaxConnsFactor > 0 {
- return p.cfg.Gateway.OpenAIWS.OAuthMaxConnsFactor
- }
- case AccountTypeAPIKey:
- if p.cfg.Gateway.OpenAIWS.APIKeyMaxConnsFactor > 0 {
- return p.cfg.Gateway.OpenAIWS.APIKeyMaxConnsFactor
- }
- }
- return 1.0
- }
- func (p *openAIWSConnPool) effectiveMaxConnsByAccount(account *Account) int {
- hardCap := p.maxConnsHardCap()
- if isMode1ProtectionEnabled(account) {
- hardCap = min(hardCap, account.Mode1EffectiveConcurrency())
- }
- if hardCap <= 0 {
- return 0
- }
- if p.modeRouterV2Enabled() {
- if account == nil {
- return hardCap
- }
- if account.Mode1EffectiveConcurrency() <= 0 {
- return 0
- }
- return min(account.Mode1EffectiveConcurrency(), hardCap)
- }
- if account == nil || !p.dynamicMaxConnsEnabled() {
- return hardCap
- }
- if account.Mode1EffectiveConcurrency() <= 0 {
- // 0/-1 等“无限制”并发场景下,仍由全局硬上限兜底。
- return hardCap
- }
- factor := p.maxConnsFactorByAccount(account)
- if factor <= 0 {
- factor = 1.0
- }
- effective := int(math.Ceil(float64(account.Mode1EffectiveConcurrency()) * factor))
- if effective < 1 {
- effective = 1
- }
- if effective > hardCap {
- effective = hardCap
- }
- return effective
- }
- func (p *openAIWSConnPool) minIdlePerAccount() int {
- if p != nil && p.cfg != nil && p.cfg.Gateway.OpenAIWS.MinIdlePerAccount >= 0 {
- return p.cfg.Gateway.OpenAIWS.MinIdlePerAccount
- }
- return 0
- }
- func (p *openAIWSConnPool) maxIdlePerAccount() int {
- if p != nil && p.cfg != nil && p.cfg.Gateway.OpenAIWS.MaxIdlePerAccount >= 0 {
- return p.cfg.Gateway.OpenAIWS.MaxIdlePerAccount
- }
- return 4
- }
- func (p *openAIWSConnPool) maxConnAge() time.Duration {
- return openAIWSConnMaxAge
- }
- func (p *openAIWSConnPool) queueLimitPerConn() int {
- if p != nil && p.cfg != nil && p.cfg.Gateway.OpenAIWS.QueueLimitPerConn > 0 {
- return p.cfg.Gateway.OpenAIWS.QueueLimitPerConn
- }
- return 256
- }
- func (p *openAIWSConnPool) targetUtilization() float64 {
- if p != nil && p.cfg != nil {
- ratio := p.cfg.Gateway.OpenAIWS.PoolTargetUtilization
- if ratio > 0 && ratio <= 1 {
- return ratio
- }
- }
- return 0.7
- }
- func (p *openAIWSConnPool) prewarmCooldown() time.Duration {
- if p != nil && p.cfg != nil && p.cfg.Gateway.OpenAIWS.PrewarmCooldownMS > 0 {
- return time.Duration(p.cfg.Gateway.OpenAIWS.PrewarmCooldownMS) * time.Millisecond
- }
- return 0
- }
- func (p *openAIWSConnPool) shouldSuppressPrewarmLocked(ap *openAIWSAccountPool, now time.Time) bool {
- if ap == nil {
- return true
- }
- if ap.prewarmFails <= 0 {
- return false
- }
- if ap.prewarmFailAt.IsZero() {
- ap.prewarmFails = 0
- return false
- }
- if now.Sub(ap.prewarmFailAt) > openAIWSPrewarmFailureWindow {
- ap.prewarmFails = 0
- ap.prewarmFailAt = time.Time{}
- return false
- }
- return ap.prewarmFails >= openAIWSPrewarmFailureSuppress
- }
- func (p *openAIWSConnPool) dialTimeout() time.Duration {
- if p != nil && p.cfg != nil && p.cfg.Gateway.OpenAIWS.DialTimeoutSeconds > 0 {
- return time.Duration(p.cfg.Gateway.OpenAIWS.DialTimeoutSeconds) * time.Second
- }
- return 10 * time.Second
- }
- func cloneOpenAIWSAcquireRequest(req openAIWSAcquireRequest) openAIWSAcquireRequest {
- copied := req
- copied.Headers = cloneHeader(req.Headers)
- copied.WSURL = stringsTrim(req.WSURL)
- copied.ProxyURL = stringsTrim(req.ProxyURL)
- copied.PreferredConnID = stringsTrim(req.PreferredConnID)
- if req.tlsProfile != nil {
- copied.tlsProfile = req.tlsProfile.Clone()
- }
- return copied
- }
- func cloneOpenAIWSAcquireRequestPtr(req *openAIWSAcquireRequest) *openAIWSAcquireRequest {
- if req == nil {
- return nil
- }
- copied := cloneOpenAIWSAcquireRequest(*req)
- return &copied
- }
- func sameOpenAIWSPrewarmTarget(a, b openAIWSAcquireRequest) bool {
- return stringsTrim(a.WSURL) == stringsTrim(b.WSURL) &&
- stringsTrim(a.ProxyURL) == stringsTrim(b.ProxyURL) &&
- openAIWSAcquireCompatibility(a) == openAIWSAcquireCompatibility(b)
- }
- func openAIWSAcquireCompatibility(req openAIWSAcquireRequest) openAIWSHandshakeCompatibilityKey {
- key := normalizeOpenAIWSHandshakeCompatibility(req.Account, req.Headers)
- if req.Account != nil {
- key.transportKey = openAIWSTransportKey(req.Account.ID, req.WSURL, req.ProxyURL, req.tlsProfile)
- }
- return key
- }
- func normalizeOpenAIWSBetaFeatures(headers http.Header) string {
- features := make(map[string]struct{})
- for name, values := range headers {
- if !strings.EqualFold(strings.TrimSpace(name), "x-codex-beta-features") {
- continue
- }
- for _, value := range values {
- for _, feature := range strings.Split(value, ",") {
- if feature = strings.TrimSpace(feature); feature != "" {
- features[feature] = struct{}{}
- }
- }
- }
- }
- if len(features) == 0 {
- return ""
- }
- normalized := make([]string, 0, len(features))
- for feature := range features {
- normalized = append(normalized, feature)
- }
- sort.Strings(normalized)
- return strings.Join(normalized, ",")
- }
- func normalizeOpenAIWSHandshakeCompatibility(account *Account, headers http.Header) openAIWSHandshakeCompatibilityKey {
- key := openAIWSHandshakeCompatibilityKey{
- betaFeatures: normalizeOpenAIWSBetaFeatures(headers),
- }
- mode := activeCodexFingerprintMode(account)
- if mode == codexFingerprintOff {
- return key
- }
- key.codexInstallationID = normalizeOpenAIWSStableIdentityHeader(headers, "x-codex-installation-id")
- if mode == codexFingerprintDevice && !account.AntiDegradationEnabled() {
- return key
- }
- key.sessionIDHyphen = normalizeOpenAIWSStableIdentityHeader(headers, "session-id")
- key.sessionIDUnderscore = normalizeOpenAIWSStableIdentityHeader(headers, "session_id")
- key.threadID = normalizeOpenAIWSStableIdentityHeader(headers, "thread-id")
- key.clientRequestID = normalizeOpenAIWSStableIdentityHeader(headers, "x-client-request-id")
- key.codexWindowID = normalizeOpenAIWSStableIdentityHeader(headers, "x-codex-window-id")
- if account.AntiDegradationEnabled() {
- key.conversationID = normalizeOpenAIWSStableIdentityHeader(headers, "conversation_id")
- }
- return key
- }
- func activeCodexFingerprintMode(account *Account) codexFingerprintMode {
- if account == nil || account.GetCodexFingerprintMode() == codexFingerprintOff {
- return codexFingerprintOff
- }
- if _, ok := codexFingerprintSeed(account.Extra); !ok {
- return codexFingerprintOff
- }
- return account.GetCodexFingerprintMode()
- }
- func normalizeOpenAIWSStableIdentityHeader(headers http.Header, name string) string {
- if headers == nil {
- return ""
- }
- return strings.TrimSpace(headers.Get(name))
- }
- func normalizeOpenAIWSRoutingAffinity(headers http.Header) string {
- canonicalName := http.CanonicalHeaderKey(openAICodexRoutingHintHeader)
- if values, ok := headers[canonicalName]; ok {
- for _, value := range values {
- if value = strings.TrimSpace(value); value != "" {
- return value
- }
- }
- }
- variantNames := make([]string, 0)
- for name := range headers {
- if name != canonicalName && strings.EqualFold(strings.TrimSpace(name), openAICodexRoutingHintHeader) {
- variantNames = append(variantNames, name)
- }
- }
- sort.Strings(variantNames)
- for _, name := range variantNames {
- for _, value := range headers[name] {
- if value = strings.TrimSpace(value); value != "" {
- return value
- }
- }
- }
- return ""
- }
- func cloneHeader(src http.Header) http.Header {
- if src == nil {
- return nil
- }
- dst := make(http.Header, len(src))
- for k, vals := range src {
- if len(vals) == 0 {
- dst[k] = nil
- continue
- }
- copied := make([]string, len(vals))
- copy(copied, vals)
- dst[k] = copied
- }
- return dst
- }
- func closeOpenAIWSConns(conns []*openAIWSConn) {
- if len(conns) == 0 {
- return
- }
- for _, conn := range conns {
- if conn == nil {
- continue
- }
- conn.close()
- }
- }
- func stringsTrim(value string) string {
- return strings.TrimSpace(value)
- }
|