Skip to content

GitHubCommand Autocomplete Cache is blocking #1315

@Zabuzard

Description

@Zabuzard

Problem

The buildup of the autocomplete-cache in GitHubCommand is currently coded blocking during construction. The method takes long (around 30s) because it queries APIs (GHRepository#getIssues() takes 30s) and this causes the bot-startup to now take over 30s.

That is not okay and also totally unecessary as this autocomplete-cache is an optional feature that is not needed for the bot to work. It should be triggered non-blocking (async) and be used whenever it is ready.

Details

The issue starts here:

public GitHubCommand(GitHubReference reference) {
  ...
  updateCache();
}

That method looks as follows:

private void updateCache() {
  autocompleteGHIssueCache = reference.getRepositories().stream().map(repo -> {
    ...
    return repo.getIssues(GHIssueState.ALL)
    ...
  })
  ...
}

The culprit is the call reference.getRepositories() plus repo.getIssues(...), which takes 30s because it needs to query the GitHub API.

The thing is, autocompleteGHIssueCache is only needed for the autocompletion of the /github-search slashcommand in onAutoComplete:

autocomplete

This is an optional feature and it would not be a problem for anyone if the autocompletion simply is not available during the first 30s of the bot being startup.

Solution

The updateCache() method should be triggered async, not blocking the constructor. While it is then being build in the background, the List<String> autocompleteGHIssueCache needs to simply be an empty list. That way, autocompletion will naturally not suggest anything until the list is then swapped out with the real list from updateCache once it is ready.

So:

  1. In the constructor (GitHubCommand#69) swap out updateCache(); by something like:
CompletableFuture.runAsync(this::updateCache);

To not miss any potential errors from this async path we can also instead use:

CompletableFuture.runAsync(() -> {
    try {
        updateCache();
    } catch (Exception e) {
        logger.error("Unknown error updating the GitHub cache", e);
    }
});
  1. Ensure the List<String> autocompleteGHIssueCache has a proper value for while the cache is building up, i.e. change its declaration in GitHubCommand#48 by
private List<String> autocompleteGHIssueCache = List.of();

That should be all. Test it. Startup of the bot should now not take 30s anymore and when using /github-search, the autocompletion should be empty if used within 30s after bot startup and filled with data after 30s.

Logging (optional)

To make it easier to debug in the future, the updateCache method should log on DEBUG level when it starts updating the cache and when its finished. So adding something like:

logger.debug("Updating GitHub cache...");
...
logger.debug("Done updating GitHub cache");

in updateCache() would be helpful.

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions