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

103 lines
6.5 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>RateLimitChecker.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">RateLimitChecker.java</span></div><h1>RateLimitChecker.java</h1><pre class="source lang-java linenums">package org.kohsuke.github;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* A GitHub API Rate Limit Checker called before each request
*
* &lt;p&gt;
* GitHub allots a certain number of requests to each user or application per period of time. The number of requests
* remaining and the time when the number will be reset is returned in the response header and can also be requested
* using {@link GitHub#getRateLimit()}. The &quot;requests per interval&quot; is referred to as the &quot;rate limit&quot;.
* &lt;/p&gt;
* &lt;p&gt;
* GitHub prefers that clients stop before exceeding their rate limit rather than stopping after they exceed it. The
* {@link RateLimitChecker} is called before each request to check the rate limit and wait if the checker criteria are
* met.
* &lt;/p&gt;
*/
<span class="fc" id="L20">public abstract class RateLimitChecker {</span>
<span class="fc" id="L22"> private static final Logger LOGGER = Logger.getLogger(RateLimitChecker.class.getName());</span>
<span class="fc" id="L24"> public static final RateLimitChecker NONE = new RateLimitChecker() {</span>
};
/**
* Decides whether the current request exceeds the allowed &quot;rate limit&quot; budget. If this determines the rate limit
* will be exceeded, this method should sleep for some amount of time and must return {@code true}. Implementers are
* free to choose whatever strategy they prefer for what is considered to exceed the budget and how long to sleep.
*
* &lt;p&gt;
* The caller of this method figures out which {@link GHRateLimit.Record} applies for the current request and
* provides it to this method.
* &lt;/p&gt;
* &lt;p&gt;
* As long as this method returns {@code true} it is guaranteed that {@link GitHubRateLimitChecker} will retrieve
* updated rate limit information and call this method again with {@code count} incremented by one. When this
* checker returns {@code false}, the calling {@link GitHubRateLimitChecker} will let the request continue.
* &lt;/p&gt;
* &lt;p&gt;
* Rate limit reset times are only accurate to the second. Trying to sleep to exactly the reset time could result in
* requests being sent before the new rate limit was available. For this reason, if this method returned
* {@code true} at least once for a particular request, {@link GitHubRateLimitChecker} may choose to sleep for some
* small additional between calls and before letting the request continue.
* &lt;/p&gt;
*
* @param rateLimitRecord
* the current {@link GHRateLimit.Record} to check against.
* @param count
* the number of times in a row this method has been called for the current request
* @return {@code false} if the current request does not exceed the allowed budget, {@code true} if the current
* request exceeded the budget.
* @throws InterruptedException
* if the thread is interrupted while sleeping
*/
protected boolean checkRateLimit(GHRateLimit.Record rateLimitRecord, long count) throws InterruptedException {
<span class="nc" id="L58"> return false;</span>
}
protected final boolean sleepUntilReset(GHRateLimit.Record record) throws InterruptedException {
// Sleep until reset
<span class="fc" id="L63"> long sleepMilliseconds = record.getResetDate().getTime() - System.currentTimeMillis();</span>
<span class="pc bpc" id="L64" title="1 of 2 branches missed."> if (sleepMilliseconds &gt; 0) {</span>
<span class="fc" id="L65"> String message = String.format(</span>
&quot;GitHub API - Current quota has %d remaining of %d. Waiting for quota to reset at %tT.&quot;,
<span class="fc" id="L67"> record.getRemaining(),</span>
<span class="fc" id="L68"> record.getLimit(),</span>
<span class="fc" id="L69"> record.getResetDate());</span>
<span class="fc" id="L71"> LOGGER.log(Level.INFO, message);</span>
<span class="fc" id="L73"> Thread.sleep(sleepMilliseconds);</span>
<span class="fc" id="L74"> return true;</span>
}
<span class="nc" id="L76"> return false;</span>
}
/**
* A {@link RateLimitChecker} with a simple number as the limit.
*/
public static class LiteralValue extends RateLimitChecker {
private final int sleepAtOrBelow;
<span class="fc" id="L85"> public LiteralValue(int sleepAtOrBelow) {</span>
<span class="pc bpc" id="L86" title="1 of 2 branches missed."> if (sleepAtOrBelow &lt; 0) {</span>
<span class="nc" id="L87"> throw new IllegalArgumentException(&quot;sleepAtOrBelow must &gt;= 0&quot;);</span>
}
<span class="fc" id="L89"> this.sleepAtOrBelow = sleepAtOrBelow;</span>
<span class="fc" id="L90"> }</span>
@Override
protected boolean checkRateLimit(GHRateLimit.Record record, long count) throws InterruptedException {
<span class="fc bfc" id="L94" title="All 2 branches covered."> if (record.getRemaining() &lt;= sleepAtOrBelow) {</span>
<span class="fc" id="L95"> return sleepUntilReset(record);</span>
}
<span class="fc" id="L97"> return false;</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>