perf(watcher): Avoid unnecessary auth dir scan on config reload

This commit is contained in:
hkfires
2025-09-27 08:43:06 +08:00
parent b56edd4db0
commit afff9216ea

View File

@@ -145,7 +145,7 @@ func (w *Watcher) Start(ctx context.Context) error {
go w.processEvents(ctx) go w.processEvents(ctx)
// Perform an initial full reload based on current config and auth dir // Perform an initial full reload based on current config and auth dir
w.reloadClients() w.reloadClients(true)
return nil return nil
} }
@@ -532,14 +532,16 @@ func (w *Watcher) reloadConfig() bool {
} }
} }
authDirChanged := oldConfig == nil || oldConfig.AuthDir != newConfig.AuthDir
log.Infof("config successfully reloaded, triggering client reload") log.Infof("config successfully reloaded, triggering client reload")
// Reload clients with new config // Reload clients with new config
w.reloadClients() w.reloadClients(authDirChanged)
return true return true
} }
// reloadClients performs a full scan and reload of all clients. // reloadClients performs a full scan and reload of all clients.
func (w *Watcher) reloadClients() { func (w *Watcher) reloadClients(rescanAuth bool) {
log.Debugf("starting full client reload process") log.Debugf("starting full client reload process")
w.clientsMutex.RLock() w.clientsMutex.RLock()
@@ -556,15 +558,26 @@ func (w *Watcher) reloadClients() {
// Create new API key clients based on the new config // Create new API key clients based on the new config
glAPIKeyCount, claudeAPIKeyCount, codexAPIKeyCount, openAICompatCount := BuildAPIKeyClients(cfg) glAPIKeyCount, claudeAPIKeyCount, codexAPIKeyCount, openAICompatCount := BuildAPIKeyClients(cfg)
log.Debugf("created %d new API key clients", 0) totalAPIKeyClients := glAPIKeyCount + claudeAPIKeyCount + codexAPIKeyCount + openAICompatCount
log.Debugf("created %d new API key clients", totalAPIKeyClients)
// Load file-based clients var authFileCount int
authFileCount := w.loadFileClients(cfg) if rescanAuth {
log.Debugf("loaded %d new file-based clients", 0) // Load file-based clients when explicitly requested (startup or authDir change)
authFileCount = w.loadFileClients(cfg)
log.Debugf("loaded %d new file-based clients", authFileCount)
} else {
// Preserve existing auth hashes and only report current known count to avoid redundant scans.
w.clientsMutex.RLock()
authFileCount = len(w.lastAuthHashes)
w.clientsMutex.RUnlock()
log.Debugf("skipping auth directory rescan; retaining %d existing auth files", authFileCount)
}
// no legacy file-based clients to unregister // no legacy file-based clients to unregister
// Update client maps // Update client maps
if rescanAuth {
w.clientsMutex.Lock() w.clientsMutex.Lock()
// Rebuild auth file hash cache for current clients // Rebuild auth file hash cache for current clients
@@ -583,6 +596,7 @@ func (w *Watcher) reloadClients() {
return nil return nil
}) })
w.clientsMutex.Unlock() w.clientsMutex.Unlock()
}
totalNewClients := authFileCount + glAPIKeyCount + claudeAPIKeyCount + codexAPIKeyCount + openAICompatCount totalNewClients := authFileCount + glAPIKeyCount + claudeAPIKeyCount + codexAPIKeyCount + openAICompatCount