ccm,ocm: fix data race, remove dead code, clean up inefficiencies

This commit is contained in:
世界
2026-03-15 21:09:08 +08:00
parent 14ade76956
commit 2dd093a32e
7 changed files with 23 additions and 13 deletions

View File

@@ -642,8 +642,9 @@ func (c *defaultCredential) fetchProfile(ctx context.Context, httpClient *http.C
if rateLimitTier != "" {
c.state.rateLimitTier = rateLimitTier
}
resolvedAccountType := c.state.accountType
c.stateAccess.Unlock()
c.logger.Info("fetched profile for ", c.tag, ": type=", c.state.accountType, ", tier=", rateLimitTier, ", weight=", ccmPlanWeight(c.state.accountType, rateLimitTier))
c.logger.Info("fetched profile for ", c.tag, ": type=", resolvedAccountType, ", tier=", rateLimitTier, ", weight=", ccmPlanWeight(resolvedAccountType, rateLimitTier))
}
func (c *defaultCredential) close() {

View File

@@ -348,6 +348,14 @@ func (p *balancerProvider) pollIfStale(ctx context.Context) {
}
p.sessionAccess.Unlock()
p.interruptAccess.Lock()
for key, entry := range p.credentialInterrupts {
if entry.context.Err() != nil {
delete(p.credentialInterrupts, key)
}
}
p.interruptAccess.Unlock()
for _, credential := range p.credentials {
if time.Since(credential.lastUpdatedTime()) > credential.pollBackoff(p.pollInterval) {
credential.pollUsage(ctx)

View File

@@ -5,7 +5,7 @@ import (
"encoding/json"
"net/http"
"strings"
"sync"
"github.com/sagernet/sing-box/adapter"
boxService "github.com/sagernet/sing-box/adapter/service"
@@ -157,9 +157,7 @@ type Service struct {
listener *listener.Listener
tlsConfig tls.ServerConfig
httpServer *http.Server
userManager *UserManager
trackingGroup sync.WaitGroup
shuttingDown bool
userManager *UserManager
providers map[string]credentialProvider
allCredentials []Credential

View File

@@ -652,7 +652,7 @@ func (u *AggregatedUsage) AddUsageWithCycleHint(
addUsageToCombinations(&u.Combinations, model, contextWindow, weekStartUnix, messagesCount, inputTokens, outputTokens, cacheReadTokens, cacheCreationTokens, cacheCreation5MinuteTokens, cacheCreation1HourTokens, user)
go u.scheduleSave()
u.scheduleSave()
return nil
}

View File

@@ -587,12 +587,7 @@ func (c *defaultCredential) pollUsage(ctx context.Context) {
return
}
var usageURL string
if c.isAPIKeyMode() {
usageURL = openaiAPIBaseURL + "/api/codex/usage"
} else {
usageURL = strings.TrimSuffix(chatGPTBackendURL, "/codex") + "/wham/usage"
}
usageURL := strings.TrimSuffix(chatGPTBackendURL, "/codex") + "/wham/usage"
accountID := c.getAccountID()
pollClient := &http.Client{

View File

@@ -375,6 +375,14 @@ func (p *balancerProvider) pollIfStale(ctx context.Context) {
}
p.sessionAccess.Unlock()
p.interruptAccess.Lock()
for key, entry := range p.credentialInterrupts {
if entry.context.Err() != nil {
delete(p.credentialInterrupts, key)
}
}
p.interruptAccess.Unlock()
for _, credential := range p.credentials {
if time.Since(credential.lastUpdatedTime()) > credential.pollBackoff(p.pollInterval) {
credential.pollUsage(ctx)

View File

@@ -1148,7 +1148,7 @@ func (u *AggregatedUsage) AddUsageWithCycleHint(model string, contextWindow int,
addUsageToCombinations(&u.Combinations, model, normalizedServiceTier, contextWindow, weekStartUnix, user, inputTokens, outputTokens, cachedTokens)
go u.scheduleSave()
u.scheduleSave()
return nil
}