Skip to content

fix(management): ensure management.html is available synchronously and improve asset sync handling#1492

Merged
luispater merged 1 commit intodevfrom
management
Feb 9, 2026
Merged

fix(management): ensure management.html is available synchronously and improve asset sync handling#1492
luispater merged 1 commit intodevfrom
management

Conversation

@hkfires
Copy link
Collaborator

@hkfires hkfires commented Feb 9, 2026

No description provided.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @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 management.html upon request. It integrates singleflight to optimize asset synchronization, ensuring that multiple concurrent requests for the asset trigger only a single update operation, thereby enhancing reliability and performance.

Highlights

  • Synchronous Asset Availability: The management.html asset is now ensured to be available synchronously when requested, preventing potential 404 errors if the file is not present locally.
  • Improved Asset Synchronization: Concurrent attempts to synchronize the management.html asset are now coalesced using golang.org/x/sync/singleflight, reducing redundant operations and improving efficiency.
  • Dependency Update: The golang.org/x/sync package has been added as a direct dependency to support the singleflight mechanism.

🧠 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
  • go.mod
    • Added golang.org/x/sync as a direct dependency.
    • Removed golang.org/x/sync from indirect dependencies as it is now a direct requirement.
  • internal/api/server.go
    • Modified the serveManagementControlPanel function to call managementasset.EnsureLatestManagementHTML synchronously instead of in a goroutine.
    • Implemented conditional http.StatusNotFound or http.StatusInternalServerError responses based on the synchronous asset check's outcome, ensuring proper error handling if the asset cannot be retrieved.
  • internal/managementasset/updater.go
    • Imported and initialized golang.org/x/sync/singleflight to manage concurrent asset updates.
    • Changed the EnsureLatestManagementHTML function signature to return a boolean indicating successful asset availability.
    • Wrapped the asset synchronization logic within singleflight.Do to coalesce concurrent calls, ensuring only one update process runs at a time for a given asset path.
Activity
  • The pull request was opened by hkfires with the title "fix(management): ensure management.html is available synchronously and improve asset sync handling".
  • The changes focus on improving the reliability and efficiency of management asset delivery by making the asset availability check synchronous and optimizing concurrent synchronization attempts.
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +189 to +272
_, _, _ = 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
})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

@luispater luispater merged commit 0dff329 into dev Feb 9, 2026
2 checks passed
@luispater luispater deleted the management branch February 9, 2026 11:55
CreatorMetaSky pushed a commit to AIxSpace/CLIProxyAPI that referenced this pull request Feb 11, 2026
fix(management): ensure management.html is available synchronously and improve asset sync handling
AoaoMH pushed a commit to AoaoMH/CLIProxyAPI-Aoao that referenced this pull request Mar 3, 2026
fix(management): ensure management.html is available synchronously and improve asset sync handling
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants