Skip to content

Commit

Permalink
Turn back on the on-disk cache.
Browse files Browse the repository at this point in the history
- For reasons that are lost in the mists of time, the on-disk cache
  proved problematic before. I suspect the issue was that the cache
  needs to be sharded by credentials as well as api url. We cannot use
  the same cache as the GitHub plugin because they use a different
  credentials type.
- Defaulting to 20MB and on. Can be disabled by setting the system
  property to `0` or using the Groovy console:
  `org.jenkinsci.plugins.github_branch_source.GitHubSCMSource.cacheSize`
  • Loading branch information
stephenc committed Sep 21, 2018
1 parent 816192d commit 1b3a370
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
Expand Up @@ -35,6 +35,7 @@
import com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials;
import com.cloudbees.plugins.credentials.domains.DomainRequirement;
import com.cloudbees.plugins.credentials.domains.URIRequirementBuilder;
import com.squareup.okhttp.Cache;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.OkUrlFactory;
import edu.umd.cs.findbugs.annotations.NonNull;
Expand All @@ -49,11 +50,15 @@
import hudson.security.ACL;
import hudson.util.FormValidation;
import hudson.util.ListBoxModel;
import java.io.File;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
Expand All @@ -70,8 +75,10 @@
import javax.annotation.Nonnull;
import jenkins.model.Jenkins;
import jenkins.scm.api.SCMSourceOwner;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.plugins.gitclient.GitClient;
import org.jenkinsci.plugins.github.GitHubPlugin;
import org.jenkinsci.plugins.github.config.GitHubServerConfig;
import org.kohsuke.github.GHRateLimit;
import org.kohsuke.github.GitHub;
Expand Down Expand Up @@ -360,6 +367,31 @@ public static void checkApiUrlValidity(@Nonnull GitHub gitHub, @CheckForNull Sta

OkHttpClient client = new OkHttpClient().setProxy(getProxy(host));

int cacheSize = GitHubSCMSource.getCacheSize();
Jenkins jenkins = Jenkins.getInstance();
if (cacheSize > 0 || jenkins == null) {
File cacheBase = new File(jenkins.getRootDir(),
GitHubSCMProbe.class.getName() + ".cache");
File cacheDir = null;
try {
MessageDigest sha256 = MessageDigest.getInstance("SHA-256");
sha256.update(apiUrl.getBytes(StandardCharsets.UTF_8));
sha256.update("::".getBytes(StandardCharsets.UTF_8));
if (username != null) {
sha256.update(username.getBytes(StandardCharsets.UTF_8));
}
sha256.update("::".getBytes(StandardCharsets.UTF_8));
sha256.update(hash.getBytes(StandardCharsets.UTF_8));
cacheDir = new File(cacheBase, Base64.encodeBase64URLSafeString(sha256.digest()));
} catch (NoSuchAlgorithmException e) {
// no cache for you mr non-spec compliant JVM
}
if (cacheDir != null) {
Cache cache = new Cache(cacheDir, cacheSize * 1024L * 1024L);
client.setCache(cache);
}
}

gb.withConnector(new OkHttpConnector(new OkUrlFactory(client)));

if (username != null) {
Expand Down
Expand Up @@ -151,6 +151,11 @@ public class GitHubSCMSource extends AbstractGitSCMSource {
*/
private static /*mostly final*/ int eventDelaySeconds =
Math.min(300, Math.max(0, Integer.getInteger(GitHubSCMSource.class.getName() + ".eventDelaySeconds", 5)));
/**
* How big (in megabytes) an on-disk cache to keep of GitHub API responses. Cache is per repo, per credentials.
*/
private static /*mostly final*/ int cacheSize =
Math.min(1024, Math.max(0, Integer.getInteger(GitHubSCMSource.class.getName() + ".cacheSize", 20)));
/**
* Lock to guard access to the {@link #pullRequestSourceMap} field and prevent concurrent GitHub queries during
* a 1.x to 2.2.0+ upgrade.
Expand Down Expand Up @@ -500,6 +505,26 @@ public static void setEventDelaySeconds(int eventDelaySeconds) {
GitHubSCMSource.eventDelaySeconds = Math.min(300, Math.max(0, eventDelaySeconds));
}

/**
* Returns how many megabytes of on-disk cache to maintain per GitHub API URL per credentials.
*
* @return how many megabytes of on-disk cache to maintain per GitHub API URL per credentials.
*/
public static int getCacheSize() {
return cacheSize;
}

/**
* Sets how long to delay events received from GitHub in order to allow the API caches to sync.
*
* @param cacheSize how many megabytes of on-disk cache to maintain per GitHub API URL per credentials,
* will be restricted into a value within the range {@code [0,1024]} inclusive.
*/
@Restricted(NoExternalUse.class) // to allow configuration from system groovy console
public static void setCacheSize(int cacheSize) {
GitHubSCMSource.cacheSize = Math.min(1024, Math.max(0, cacheSize));
}

/**
* {@inheritDoc}
*/
Expand Down

0 comments on commit 1b3a370

Please sign in to comment.