scala-redis icon indicating copy to clipboard operation
scala-redis copied to clipboard

RedisClient is not initialized when the server is restarted.

Open Seyun opened this issue 9 years ago • 3 comments

If a redis server restarts, the client connection is not initialized.

When the redis server restarts, the socket connection between the server and the client is lost. So the value of 'connected' in the following code gets to be false, which lets 'connect' method invoked.

https://github.com/debasishg/scala-redis/blob/master/src/main/scala/com/redis/IO.scala#L68

The problem is that 'connect' does not invoke 'initialize' method in it compared to 'reconnect'.

In my case, this makes problems such as

  • 'auth' is not invoked because 'initialize' is not invoked
  • the overridden 'reconnect' is not invoked, which is for re-subscribing channels when reconnected.

FYI, the following is an inherited class from RedisClient for taking a function of 'onReconnect'

class ResilientRedisClient(override val host: String, override val port: Int,
                           override val database: Int = 0, override val secret: Option[Any] = None, override val timeout : Int = 0)
                          (onReconnect:RedisClient => Any) extends RedisClient {
  override def reconnect: Boolean = {
    val result = super.reconnect

    if (result) onReconnect(this)

    result
  }
}

Seyun avatar Nov 14 '16 02:11 Seyun

Thanks for reporting .. I will look into it .. and in case u have fixed it already, a PR will be welcome :-)

debasishg avatar Nov 14 '16 06:11 debasishg

Thanks for your quick response.

I tried but a quick solution did not come up. :(

Seyun avatar Nov 14 '16 06:11 Seyun

I've run into this problem in production, after redis server restarts - all operations are performed against database 0, causing loss of data

voidconductor avatar Oct 22 '19 16:10 voidconductor