Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache remote build map, fetch in registry refresh #3624

Merged
merged 1 commit into from
Aug 12, 2022

Conversation

HebaruSan
Copy link
Member

@HebaruSan HebaruSan commented Aug 11, 2022

Problems

My network has been a bit unreliable lately, and I've found that during periods of downtime, not only can I not download mods, but even commands like ckan instance list simply hang. (First noticed while working on the Electron UI project previewed in the comments of #2848, which just wraps around CKAN.dll without additional C# code being run.)

Cause

When CKAN starts, it loads all the game instances pretty early. To do this, it first has to load the known game versions so it can check each instance's version. Currently we always use the remote build map for this, via an HTTPS request to GitHub. If the network isn't working, it just sits there waiting for timeout.

This isn't a great model. As pointed out in #2108, CKAN should refrain from creating network connections without the user's awareness, especially if it isn't necessary. Furthermore, we already have an embedded build map compiled into CKAN that we essentially never use, because the remote build map is always retrieved first.

An additional quirk is that the command line parameters are handled after all this, so even if you pass --debug, the log.Debug output of these steps is never printed.

Changes

  • Now instead of retrieving the remote build map at every startup, we check for a locally cached copy (in the same directory as config.json). If found, we use it, otherwise we fall back to the embedded build map. This eliminates the network call at startup and would allow commands like ckan instance list to complete while offline.
  • The updating code used by ckan update and GUI's Refresh button is updated to fetch the remote build map and save a cached copy for future startups, since this represents a time when the user has authorized network activity.
  • Now before full parsing of the command line parameters, we check for --debug or --verbose and set the log level if found. This way these arguments can be used to see activity before the full parsing happens, including the build map's initialization.

@HebaruSan HebaruSan added Enhancement New features or functionality Core (ckan.dll) Issues affecting the core part of CKAN Network Issues affecting internet connections of CKAN labels Aug 11, 2022
Copy link
Member

@techman83 techman83 left a comment

Choose a reason for hiding this comment

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

That seems like a super sensible improvement! Do we have any tests for this?

From my cursory read, nothing stood out apart from the unused variable. So lgtm!

Log.Debug("Getting cached build map");
return TrySetBuildMap(File.ReadAllText(cachedBuildMapPath));
}
catch (Exception e)
Copy link
Member

Choose a reason for hiding this comment

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

Are we doing anything with this exception we're catching? We're getting a few notes about it.

Copy link
Member Author

Choose a reason for hiding this comment

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

No; unlike the other TrySet*BuildMap functions, we don't want to print a warning for this one because we expect it to be missing the first time. I've removed this variable.

@HebaruSan
Copy link
Member Author

As for tests, I don't know how this could be tested, since it depends on writing to and reading from ~/.local/share/CKAN/builds.json. And as far as I can tell, there were no existing tests for any of the old build map stuff.

@HebaruSan HebaruSan merged commit 21271d6 into KSP-CKAN:master Aug 12, 2022
@HebaruSan HebaruSan deleted the fix/build-map-caching branch August 12, 2022 03:08
@HebaruSan
Copy link
Member Author

For fun I took the latest build for a spin in my Debian and Fedora VMs with their network devices disabled, and the commands that don't need network access all worked fine. 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Core (ckan.dll) Issues affecting the core part of CKAN Enhancement New features or functionality Network Issues affecting internet connections of CKAN
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants