redis / go-redis

Redis Go client
https://redis.uptrace.dev
BSD 2-Clause "Simplified" License
19.89k stars 2.34k forks source link

Closing callback for a node client in ClusterClient #2800

Open lzakharov opened 10 months ago

lzakharov commented 10 months ago

Good day!

Is there any option to set the closing callback for a node client in ClusterClient?

Use Case I want to start metrics collection process on every new node client creation and stop it when the node client is removed for some reason. To stop the process I have to provide a callback somehow. To start the process I can use ClusterClient.OnNewNode.

Ideas One solution that comes to my mind is to add a callback to the return function that the ClusterClient.OnNewNode accepts:

func (c *ClusterClient) OnNewNode(fn func(rdb *Client) func()) {
    ...
}

This callback should be added to the clusterNode and called inside it's Close method.

However, to avoid breaking backward compatibility, it's possible to add a separate method for ClusterClient, such as OnNewNodeWithClose.

Another idea is to expand the redis.Hook interface to provide additional CloseHook method.

Thanks for any feedback in advance.

lzakharov commented 10 months ago

Another option is to make the baseClient.onClose configurable.