Search code examples
c#redisbooksleeve

Booksleeve, error thrown when trying to open connection after connection was closed


I cannot re-connect to my Redis DB doing the following:

  • Create a new RedisConnection called "connection"
  • Open the connection connection.Open().Wait();
  • Close the connection connection.Close(true);
  • Now when I attempt to connection.Open().Wait(); an error is thrown "Connection is closed".

I am aware the connection was closed but why I cannot re-open it?

The same happens if I instead of close the connection, shut down the Redis server, let the client raise the Closed event, re-start the server, and then attempt to open the connection with connection.Open().Wait(); again. "Connection is closed" error is thrown.

What is wrong with my approach? I do not seem to be able to manage connection states properly with Booksleeve.

Thanks


Solution

  • It is not the expected usage that you open and close a BookSleeve connection.

    • since it is a multiplexer, it is expected that it is opened once and then used by multiple callers concurrently (it is thread-safe etc)
    • opening involves various handshakes (at both network and protocol levels) to ensure correct operation - this is best avoided (it isn't insanely expensive, note)
    • no ongoing state would be viable once closed; you night as well just use a brand new connection if you really want to close it

    Personally I'd only close it if I was reconfiguring the system at runtime, or the connection broke.