redis.clients.jedis.exceptions.JedisNoScriptException Java Examples

The following examples show how to use redis.clients.jedis.exceptions.JedisNoScriptException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DistributedFixedTimeWindowRateLimiterTest.java    From ratelimiter4j with Apache License 2.0 5 votes vote down vote up
public void testTryAquire_withJedisNoScriptException() throws InternalErrorException {
  JedisTaskExecutor executor = Mockito.mock(JedisTaskExecutor.class);
  RateLimiter ratelimiter = new DistributedFixedTimeWindowRateLimiter("test-key", 5, executor);
  
  when(executor.evalsha(any(), any(), any())).thenThrow(new JedisNoScriptException(""));
  when(executor.eval(any(), any(), any())).thenReturn(1l);
  boolean passed = ratelimiter.tryAcquire();
  assertTrue(passed);
  
  when(executor.eval(any(), any(), any())).thenReturn(0l);
  passed = ratelimiter.tryAcquire();
  assertFalse(passed);
}
 
Example #2
Source File: DistributedFixedTimeWindowRateLimiterTest.java    From ratelimiter4j with Apache License 2.0 5 votes vote down vote up
@Test(expectedExceptions = { InternalErrorException.class })
public void testTryAquire_eval_JedisConnectionException() throws InternalErrorException {
  JedisTaskExecutor executor = Mockito.mock(JedisTaskExecutor.class);
  RateLimiter ratelimiter = new DistributedFixedTimeWindowRateLimiter("test-key", 5, executor);
  
  when(executor.evalsha(any(), any(), any())).thenThrow(new JedisNoScriptException(""));
  when(executor.eval(any(), any(), any())).thenThrow(new JedisConnectionException(""));
  ratelimiter.tryAcquire();
}
 
Example #3
Source File: CloudDB.java    From appinventor-extensions with Apache License 2.0 5 votes vote down vote up
public Object jEval(String script, String scriptsha1, int argcount, String... args) throws JedisException {
  Jedis jedis = getJedis();
  try {
    return jedis.evalsha(scriptsha1, argcount, args);
  } catch (JedisNoScriptException e) {
    if (DEBUG) {
      Log.d(LOG_TAG, "Got a JedisNoScriptException for " + scriptsha1);
    }
    // This happens if the server doesn't have the script loaded
    // So we use regular eval, which should then cache the script
    return jedis.eval(script, argcount, args);
  }
}
 
Example #4
Source File: IncreasingRateLimiter.java    From MantaroBot with GNU General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private RateLimit limit0(String key) {
    try (Jedis j = pool.getResource()) {
        if (scriptSha == null) {
            scriptSha = j.scriptLoad(SCRIPT);
        }

        long start = Instant.now().toEpochMilli();
        List<Long> result;
        boolean premiumAwareness = premiumAware && MantaroData.db().getUser(key).isPremium();
        try {
            int cd = cooldown + (randomIncrement && !premiumAwareness ? ThreadLocalRandom.current().nextInt(cooldown / incrementDivider) : 0);
            result = (List<Long>) j.evalsha(scriptSha,
                    Collections.singletonList(key),
                    Arrays.asList(
                            String.valueOf(limit),
                            String.valueOf(start),
                            String.valueOf(premiumAwareness ? cd - ThreadLocalRandom.current().nextInt(cooldown / 4) : cd),
                            String.valueOf(spamBeforeCooldownIncrease),
                            String.valueOf(cooldownIncrease),
                            String.valueOf(maxCooldown)
                    )
            );
        } catch (JedisNoScriptException e) {
            //script not in cache. force load it and try again.
            scriptSha = j.scriptLoad(SCRIPT);
            return limit0(key);
        }

        return new RateLimit(
                start,
                (int) (limit - result.get(0)),
                result.get(1) - start,
                result.get(2).intValue()
        );
    }
}