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

[FEATURE] Ability to set custom client name on subscriber session

Open Cezero opened this issue 2 years ago • 3 comments

When the subscriber creates a new connection, it has a blank name. This ultimately results in a bunch of client sessions on the Redis server that all have blank names and are hard to distinguish between.

Given that there is no interface to send commands on the "subscriber" connection, there is no way to call CLIENT SETNAME to modify the subscriber session's name. Ideally I would like to be able to set the client name in connection_options and have that be applied to the Redis connection (either the main one, or a connection created by a call to subscriber()). Alternatively, exposing a function subscriber.setName() or something similar that would call CLIENT SETNAME on the underlying subscriber connection would do the trick as well.

Thank you.

Cezero avatar Jan 14 '22 23:01 Cezero

@Cezero Thanks for your suggestion!

When connection is in subscribing mode, it's restricted, and can only call a few commands. So Subscriber::set_name() might be hard to be used correctly, since user must call it before Subscriber enters the subscribing mode.

I like the idea that making the connection name a part of ConnectionOptions. The only problem is that when connection pool is used, all connections in the pool will have the same name.

I'll try to implement this feature ASAP.

Regards

sewenew avatar Jan 16 '22 13:01 sewenew

Given that connections are established lazily, if the connection name is part of the ConnectionOptions, then I would think you could then set that on the subscriber connection prior to entering subscriber mode, safe in the knowledge that the "Redis" connection will never be established?

As for connection pool, you could append a number to connection name or something, such that you end up with ConnName-0, ConnName-1, ConnName-2 ... that would allow connections that are part of a pool to be easily grouped and yet still have unique identifiers.

Cezero avatar Jan 16 '22 18:01 Cezero

@Cezero Sorry for the late reply!

The first version of this feature has been implemented, and you can check out the setname branch and take a try:

ConnectionOptions opts;
opts.name = "client-name";        // <--- set client name
// set other options

auto redis = Redis(opts);

ConnectionOptions subscriber_opts;
subscriber_opts.name = "subscriber";
// set other options

auto tmp_redis = Redis(subscriber_opts);     // <----- connections are lazily created, so there's no performance penalty
auto sub = tmp_redis.subscriber();        // <---- reuse the name of `Redis`

Given that connections are established lazily, if the connection name is part of the ConnectionOptions, then I would think you could then set that on the subscriber connection prior to entering subscriber mode, safe in the knowledge that the "Redis" connection will never be established?

Yes the best timing is that setting name in Subscriber's constructor. The first version I implemented, sets name in the constructor, i.e. when creating the connection, but reuse the connection name of Redis.

As for connection pool, you could append a number to connection name or something, such that you end up with ConnName-0, ConnName-1, ConnName-2 ... that would allow connections that are part of a pool to be easily grouped and yet still have unique identifiers.

Yes, that's possible solution. However, in that case, we have to maintain a counter, and what's if the connection is broken, and recreated, should it have the same new as the broken connection? If a Pipeline, Transaction, or Subscriber is created by the Redis object, do we need a suffix to distinguish them? Also does it really matter to distinguish connections in the same pool? I'm still not quite sure about these problems. So the first version is quite simple, i.e. all connections created by the same Redis object has the same name. Maybe in the future, we can let users set a pattern instead of a string. So that each connection can have a different name based on the given pattern. For example, opts.name = "name-%d"; means each connection has a name with a numeric suffix.

Regards

sewenew avatar Jan 18 '22 15:01 sewenew

Any news about this feature?

bronekrab avatar Aug 11 '23 14:08 bronekrab

The problem is that we still haven't decided how to distinguish different connections in the connection pool. If you have any suggestion or even better any clue on how do other client libraries implement this feature, feel free to let me know.

Also, I'll try to make a decision in the near future.

Regards

sewenew avatar Aug 12 '23 01:08 sewenew

@sewenew Thanks for the reply!

Maybe just use one client name for all connections from the pool? Like the official redis-py library.

https://github.com/redis/redis-py/commit/dca7bd40a3a5d0c0853fe2befe706e214407697b

Regards

bronekrab avatar Aug 14 '23 22:08 bronekrab

I've implemented this feature, and as you suggested, all connections in the pool have the same name. You can test the latest code on master branch with the following code:

ConnectionOptions opts;
opts.host = "127.0.0.1";
opts.port = 6379;
opts.name = "name";
auto r = Redis(opts);

Sorry for the delay.

Regards

sewenew avatar Dec 30 '23 14:12 sewenew