Skip to content
This repository has been archived by the owner on Oct 8, 2024. It is now read-only.

Using RateLimiter from Guava to avoid triggering GitHub abuse detection mechanism #90

Merged
merged 1 commit into from
May 24, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ protected GitHubClient createClient(String host, String userName,
}
}
}

if (configureUsernamePassword(client, userName, password)
|| configureOAuth2Token(client, oauth2Token)
|| configureServerCredentials(client, serverId, settings,
Expand All @@ -215,10 +215,10 @@ protected GitHubClient createClient(String host, String userName,
protected GitHubClient createClient(String hostname)
throws MojoExecutionException {
if (!hostname.contains("://"))
return new GitHubClientEgit(hostname);
return new RateLimitedGitHubClient(hostname);
try {
URL hostUrl = new URL(hostname);
return new GitHubClientEgit(hostUrl.getHost(), hostUrl.getPort(),
return new RateLimitedGitHubClient(hostUrl.getHost(), hostUrl.getPort(),
hostUrl.getProtocol());
} catch (MalformedURLException e) {
throw new MojoExecutionException("Could not parse host URL "
Expand All @@ -234,7 +234,7 @@ protected GitHubClient createClient(String hostname)
* @return non-null client
*/
protected GitHubClient createClient() {
return new GitHubClientEgit();
return new RateLimitedGitHubClient();
}

/**
Expand Down Expand Up @@ -333,7 +333,7 @@ protected boolean configureServerCredentials(final GitHubClient client,
throw new MojoExecutionException( "Unable to lookup SettingsDecrypter: " + cle.getMessage(), cle );
}
}

serverUsername = server.getUsername();
serverPassword = server.getPassword();
// }
Expand Down Expand Up @@ -407,7 +407,7 @@ protected Server getServer(final Settings settings, final String serverId) {

/**
* Check hostname that matched nonProxy setting
*
*
* @param proxy Maven Proxy. Must not null
* @param hostname
* @return matching result. true: match nonProxy
Expand Down Expand Up @@ -515,10 +515,10 @@ protected Proxy getProxy(final Settings settings, final String serverId, final S

return null;
}

@Requirement
private PlexusContainer container;

/**
* {@inheritDoc}
*/
Expand All @@ -527,5 +527,5 @@ public void contextualize( Context context )
{
container = (PlexusContainer) context.get( PlexusConstants.PLEXUS_KEY );
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.github.maven.plugins.core;

import com.github.maven.plugins.core.egit.GitHubClientEgit;
import com.google.common.util.concurrent.RateLimiter;

import java.io.IOException;
import java.net.HttpURLConnection;

public class RateLimitedGitHubClient extends GitHubClientEgit {

/**
* AS per https://github.com/octokit/octokit.net/issues/638#issuecomment-67795998,
* it seems that GitHub only allow 20 API calls per 1-minute period
*/
private RateLimiter rateLimiter = RateLimiter.create(20.0/60.0);

public RateLimitedGitHubClient() {
super();
}

public RateLimitedGitHubClient(String hostname) {
super(hostname);
}

public RateLimitedGitHubClient(String hostname, int port, String scheme) {
super(hostname, port, scheme);
}

@Override
protected HttpURLConnection createDelete(String uri) throws IOException {
//rateLimiter.acquire();
return super.createDelete(uri);
}

@Override
protected HttpURLConnection createGet(String uri) throws IOException {
//rateLimiter.acquire();
return super.createGet(uri);
}

@Override
protected HttpURLConnection createPost(String uri) throws IOException {
rateLimiter.acquire();
return super.createPost(uri);
}

@Override
protected HttpURLConnection createPut(String uri) throws IOException {
rateLimiter.acquire();
return super.createPut(uri);
}
}
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,13 @@
<version>2.2.2</version>
</dependency>

<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<!-- More recent version incompatible with Guice from Maven / Sisu -->
<version>14.0</version>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down