vert-x3 / vertx-redis-client

Redis client for Vert.x
http://vertx.io
Apache License 2.0
128 stars 116 forks source link

Add support for ping command in Pub/Sub mode #454

Open sagaxu opened 2 months ago

sagaxu commented 2 months ago

It's nice to use ping command to check if the connection is still alive.

https://github.com/vert-x3/vertx-redis-client/blob/e3731a3e8a4d61beb4d2e03a223621a7c7252af2/src/main/java/io/vertx/redis/client/impl/RedisStandaloneConnection.java#L378

  1. run in pubsub mode
  2. call conn.toReceiveChannel, it will pause the stream first, then call ReadStream.fetch(1)
  3. run ping command and redis server reply with pong
  4. the handle in RedisStandaloneConnection didn't dispatch the reply of ping to onMessage
  5. so nobody will call stream.fetch(1), the stream can't receive any more
private class ChannelReadStream<T>(val stream: ReadStream<T>,
                                   val channel: Channel<T>,
                                   context: Context) : Channel<T> by channel, CoroutineScope {

  override val coroutineContext: CoroutineContext = context.dispatcher()

  fun subscribe() {
    stream.endHandler {
      close()
    }
    stream.exceptionHandler { err ->
      close(err)
    }
    stream.handler { event ->
      launch {
        send(event)
        stream.fetch(1)
      }
    }
  }
}