Reputation: 414
I need to send message to clients from a netty server base on user names of clients. As a result I need to map channels with user name and find the channel each time I want to send message.
I have two approaches in my mind, the question is : Which approach is better, in term of performance in server side. do you have any better idea?
Map channels with user name in a hashmap.
//Send userName from client side in first request
//Get userName in server side and put it in a map
Map<String, Channel> userMap = new ConcurrentHashMap<String,Channel>();
//loop over userMap to find specific client
Set Attachment with user name.
//Set the attachment in client side
ctx.getChannel().setAttachment(username);
//Put all channels to a default channel group
//Get all channels, search in their attachments to find specific client
Upvotes: 0
Views: 180
Reputation: 30335
How about creating a "UserInfo" object, which holds the user's name and his associated channel?
Upvotes: 0
Reputation: 4872
From your code I suspect that the second option uses linear search to find a specific channel. The first option would simple perform a get. (But the key must be string in this case)
Average linear search time: O(n/2)
Average hashmap access time: O(1)! (see this posting for more information)
That means that the linear search gets worse if you have more channels. The hashmap option is more stable and you can expect almost constant time access.
What you could do is "fuse" both option, so you have the map to access the channels easily and the ChannelGroup for handling the difficult stuff. What you need to do is to remove the channel from the map when its closed.
Upvotes: 1