Java源码示例:redis.clients.jedis.exceptions.JedisNoScriptException

示例1
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);
}
 
示例2
@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();
}
 
示例3
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);
  }
}
 
示例4
@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()
        );
    }
}