mirror of
https://github.com/SagerNet/sing-box.git
synced 2026-04-13 20:28:32 +10:00
ccm,ocm: fix data race, remove dead code, clean up inefficiencies
This commit is contained in:
@@ -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() {
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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{
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user