Class RedisCommandTimeout

java.lang.Object
me.ahoo.cosid.spring.redis.RedisCommandTimeout

public final class RedisCommandTimeout extends Object
Bounds how long a caller waits for a Redis command.

StringRedisTemplate has no per-call timeout: a blocked command waits for the connection factory's command timeout (60s by default for Lettuce). Machine id guarding runs on a single thread, so one hung command stalls it. When enabled, the command runs on a daemon worker and the caller gives up after getTimeout(), interrupting the worker and throwing RedisCommandTimeoutException.

It bounds the steady-state commands (machine id guard and revert, segment nextMaxId). The first command at startup (machine id distribution, segment offset initialization) is left unbounded: on a cold JVM it pays for connecting and class loading, which can take about a second even against a local Redis.

A timed-out command may still have been applied on the server. That is safe for CosId's commands: a lost segment increment only leaves a gap, and machine id distribution is idempotent per instance.

  • Field Details

    • DEFAULT_TIMEOUT

      public static final Duration DEFAULT_TIMEOUT
    • NONE

      public static final RedisCommandTimeout NONE
      No timeout: commands run on the caller thread, as before.
    • MAX_CONCURRENT_COMMANDS

      public static final int MAX_CONCURRENT_COMMANDS
      Upper bound of concurrently running commands, shared with the other CallTimeout users. Beyond it, callers fail fast instead of piling up threads behind a hung Redis.
      See Also:
  • Method Details

    • of

      public static RedisCommandTimeout of(Duration timeout)
      Create a command timeout.
      Parameters:
      timeout - timeout of each command, null or zero means no timeout
      Returns:
      command timeout
    • getTimeout

      public Duration getTimeout()
    • isEnabled

      public boolean isEnabled()
    • call

      public <T> T call(String operation, Supplier<T> command)
      Run a Redis command within the timeout.
      Type Parameters:
      T - result type
      Parameters:
      operation - name of the operation, used in the timeout message
      command - the Redis command
      Returns:
      the command result
    • run

      public void run(String operation, Runnable command)
    • toString

      public String toString()
      Overrides:
      toString in class Object