Files
github-api/jacoco/org.kohsuke.github/RateLimitHandler.java.html
2021-06-02 11:09:28 -07:00

66 lines
4.2 KiB
HTML

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../jacoco-resources/report.gif" type="image/gif"/><title>RateLimitHandler.java</title><link rel="stylesheet" href="../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../index.html" class="el_report">GitHub API for Java</a> &gt; <a href="index.source.html" class="el_package">org.kohsuke.github</a> &gt; <span class="el_source">RateLimitHandler.java</span></div><h1>RateLimitHandler.java</h1><pre class="source lang-java linenums">package org.kohsuke.github;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.net.HttpURLConnection;
/**
* Pluggable strategy to determine what to do when the API rate limit is reached.
*
* @author Kohsuke Kawaguchi
* @see GitHubBuilder#withRateLimitHandler(RateLimitHandler) GitHubBuilder#withRateLimitHandler(RateLimitHandler)
* @see AbuseLimitHandler
*/
<span class="fc" id="L14">public abstract class RateLimitHandler {</span>
/**
* Called when the library encounters HTTP error indicating that the API rate limit is reached.
*
* &lt;p&gt;
* Any exception thrown from this method will cause the request to fail, and the caller of github-api will receive
* an exception. If this method returns normally, another request will be attempted. For that to make sense, the
* implementation needs to wait for some time.
*
* @param e
* Exception from Java I/O layer. If you decide to fail the processing, you can throw this exception (or
* wrap this exception into another exception and throw it.)
* @param uc
* Connection that resulted in an error. Useful for accessing other response headers.
* @throws IOException
* the io exception
* @see &lt;a href=&quot;https://developer.github.com/v3/#rate-limiting&quot;&gt;API documentation from GitHub&lt;/a&gt;
*/
public abstract void onError(IOException e, HttpURLConnection uc) throws IOException;
/**
* Block until the API rate limit is reset. Useful for long-running batch processing.
*/
<span class="fc" id="L37"> public static final RateLimitHandler WAIT = new RateLimitHandler() {</span>
@Override
public void onError(IOException e, HttpURLConnection uc) throws IOException {
try {
<span class="fc" id="L41"> Thread.sleep(parseWaitTime(uc));</span>
<span class="nc" id="L42"> } catch (InterruptedException x) {</span>
<span class="nc" id="L43"> throw (InterruptedIOException) new InterruptedIOException().initCause(e);</span>
<span class="fc" id="L44"> }</span>
<span class="fc" id="L45"> }</span>
private long parseWaitTime(HttpURLConnection uc) {
<span class="fc" id="L48"> String v = uc.getHeaderField(&quot;X-RateLimit-Reset&quot;);</span>
<span class="pc bpc" id="L49" title="1 of 2 branches missed."> if (v == null)</span>
<span class="nc" id="L50"> return 10000; // can't tell</span>
<span class="fc" id="L52"> return Math.max(10000, Long.parseLong(v) * 1000 - System.currentTimeMillis());</span>
}
};
/**
* Fail immediately.
*/
<span class="fc" id="L59"> public static final RateLimitHandler FAIL = new RateLimitHandler() {</span>
@Override
public void onError(IOException e, HttpURLConnection uc) throws IOException {
<span class="fc" id="L62"> throw (IOException) new IOException(&quot;API rate limit reached&quot;).initCause(e);</span>
}
};
}
</pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.8.7.202105040129</span></div></body></html>