fix(management): ensure management.html is available synchronously and improve asset sync handling#1492
fix(management): ensure management.html is available synchronously and improve asset sync handling#1492
Conversation
…d improve asset sync handling
Summary of ChangesHello @hkfires, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request refactors the management control panel asset loading mechanism to guarantee the synchronous availability of Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request refactors the management asset handling to ensure management.html is available synchronously on the first request. The changes introduce golang.org/x/sync/singleflight to coalesce concurrent asset update attempts, preventing redundant downloads. The EnsureLatestManagementHTML function is now called synchronously from the server handler and returns a boolean indicating success, which improves the user experience by making the management panel available immediately after download. The implementation is solid and correctly addresses the thundering herd problem. I've added one suggestion to improve code readability by extracting a large anonymous function.
| _, _, _ = sfGroup.Do(localPath, func() (interface{}, error) { | ||
| lastUpdateCheckMu.Lock() | ||
| now := time.Now() | ||
| timeSinceLastAttempt := now.Sub(lastUpdateCheckTime) | ||
| if !lastUpdateCheckTime.IsZero() && timeSinceLastAttempt < managementSyncMinInterval { | ||
| lastUpdateCheckMu.Unlock() | ||
| log.Debugf( | ||
| "management asset sync skipped by throttle: last attempt %v ago (interval %v)", | ||
| timeSinceLastAttempt.Round(time.Second), | ||
| managementSyncMinInterval, | ||
| ) | ||
| return nil, nil | ||
| } | ||
| lastUpdateCheckTime = now | ||
| lastUpdateCheckMu.Unlock() | ||
| log.Debugf( | ||
| "management asset sync skipped by throttle: last attempt %v ago (interval %v)", | ||
| timeSinceLastAttempt.Round(time.Second), | ||
| managementSyncMinInterval, | ||
| ) | ||
| return | ||
| } | ||
| lastUpdateCheckTime = now | ||
| lastUpdateCheckMu.Unlock() | ||
|
|
||
| localPath := filepath.Join(staticDir, managementAssetName) | ||
| localFileMissing := false | ||
| if _, errStat := os.Stat(localPath); errStat != nil { | ||
| if errors.Is(errStat, os.ErrNotExist) { | ||
| localFileMissing = true | ||
| } else { | ||
| log.WithError(errStat).Debug("failed to stat local management asset") | ||
| localFileMissing := false | ||
| if _, errStat := os.Stat(localPath); errStat != nil { | ||
| if errors.Is(errStat, os.ErrNotExist) { | ||
| localFileMissing = true | ||
| } else { | ||
| log.WithError(errStat).Debug("failed to stat local management asset") | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if errMkdirAll := os.MkdirAll(staticDir, 0o755); errMkdirAll != nil { | ||
| log.WithError(errMkdirAll).Warn("failed to prepare static directory for management asset") | ||
| return | ||
| } | ||
| if errMkdirAll := os.MkdirAll(staticDir, 0o755); errMkdirAll != nil { | ||
| log.WithError(errMkdirAll).Warn("failed to prepare static directory for management asset") | ||
| return nil, nil | ||
| } | ||
|
|
||
| releaseURL := resolveReleaseURL(panelRepository) | ||
| client := newHTTPClient(proxyURL) | ||
| releaseURL := resolveReleaseURL(panelRepository) | ||
| client := newHTTPClient(proxyURL) | ||
|
|
||
| localHash, err := fileSHA256(localPath) | ||
| if err != nil { | ||
| if !errors.Is(err, os.ErrNotExist) { | ||
| log.WithError(err).Debug("failed to read local management asset hash") | ||
| localHash, err := fileSHA256(localPath) | ||
| if err != nil { | ||
| if !errors.Is(err, os.ErrNotExist) { | ||
| log.WithError(err).Debug("failed to read local management asset hash") | ||
| } | ||
| localHash = "" | ||
| } | ||
| localHash = "" | ||
| } | ||
|
|
||
| asset, remoteHash, err := fetchLatestAsset(ctx, client, releaseURL) | ||
| if err != nil { | ||
| if localFileMissing { | ||
| log.WithError(err).Warn("failed to fetch latest management release information, trying fallback page") | ||
| if ensureFallbackManagementHTML(ctx, client, localPath) { | ||
| return | ||
| asset, remoteHash, err := fetchLatestAsset(ctx, client, releaseURL) | ||
| if err != nil { | ||
| if localFileMissing { | ||
| log.WithError(err).Warn("failed to fetch latest management release information, trying fallback page") | ||
| if ensureFallbackManagementHTML(ctx, client, localPath) { | ||
| return nil, nil | ||
| } | ||
| return nil, nil | ||
| } | ||
| return | ||
| log.WithError(err).Warn("failed to fetch latest management release information") | ||
| return nil, nil | ||
| } | ||
| log.WithError(err).Warn("failed to fetch latest management release information") | ||
| return | ||
| } | ||
|
|
||
| if remoteHash != "" && localHash != "" && strings.EqualFold(remoteHash, localHash) { | ||
| log.Debug("management asset is already up to date") | ||
| return | ||
| } | ||
| if remoteHash != "" && localHash != "" && strings.EqualFold(remoteHash, localHash) { | ||
| log.Debug("management asset is already up to date") | ||
| return nil, nil | ||
| } | ||
|
|
||
| data, downloadedHash, err := downloadAsset(ctx, client, asset.BrowserDownloadURL) | ||
| if err != nil { | ||
| if localFileMissing { | ||
| log.WithError(err).Warn("failed to download management asset, trying fallback page") | ||
| if ensureFallbackManagementHTML(ctx, client, localPath) { | ||
| return | ||
| data, downloadedHash, err := downloadAsset(ctx, client, asset.BrowserDownloadURL) | ||
| if err != nil { | ||
| if localFileMissing { | ||
| log.WithError(err).Warn("failed to download management asset, trying fallback page") | ||
| if ensureFallbackManagementHTML(ctx, client, localPath) { | ||
| return nil, nil | ||
| } | ||
| return nil, nil | ||
| } | ||
| return | ||
| log.WithError(err).Warn("failed to download management asset") | ||
| return nil, nil | ||
| } | ||
| log.WithError(err).Warn("failed to download management asset") | ||
| return | ||
| } | ||
|
|
||
| if remoteHash != "" && !strings.EqualFold(remoteHash, downloadedHash) { | ||
| log.Warnf("remote digest mismatch for management asset: expected %s got %s", remoteHash, downloadedHash) | ||
| } | ||
| if remoteHash != "" && !strings.EqualFold(remoteHash, downloadedHash) { | ||
| log.Warnf("remote digest mismatch for management asset: expected %s got %s", remoteHash, downloadedHash) | ||
| } | ||
|
|
||
| if err = atomicWriteFile(localPath, data); err != nil { | ||
| log.WithError(err).Warn("failed to update management asset on disk") | ||
| return | ||
| } | ||
| if err = atomicWriteFile(localPath, data); err != nil { | ||
| log.WithError(err).Warn("failed to update management asset on disk") | ||
| return nil, nil | ||
| } | ||
|
|
||
| log.Infof("management asset updated successfully (hash=%s)", downloadedHash) | ||
| return nil, nil | ||
| }) |
There was a problem hiding this comment.
The anonymous function passed to sfGroup.Do is quite large (over 80 lines). To improve readability and testability, consider extracting this logic into a separate, private package-level function.
For example:
// In EnsureLatestManagementHTML
_, _, _ = sfGroup.Do(localPath, func() (interface{}, error) {
return updateManagementHTMLOnce(ctx, staticDir, localPath, proxyURL, panelRepository)
})
// ...
// new private function
func updateManagementHTMLOnce(ctx context.Context, staticDir, localPath, proxyURL, panelRepository string) (interface{}, error) {
// ... logic from the current anonymous function ...
return nil, nil
}This would make the EnsureLatestManagementHTML function's primary responsibility—coalescing calls and checking the final file state—much clearer.
fix(management): ensure management.html is available synchronously and improve asset sync handling
fix(management): ensure management.html is available synchronously and improve asset sync handling
No description provided.