send the same data to multiple channels

117 views
Skip to first unread message

matya...@gmail.com

unread,
Apr 12, 2019, 12:31:57 AM4/12/19
to Netty discussions
Are there any optimizations available if I'd like to send the very same data to dozens (or hundreds) of Websocket channels?
At a minimum I'd like to avoid creating (and copying) the data to new buffers. Ideally I would provide a single buffer and a list of channels.

Frederic

unread,
Apr 12, 2019, 2:54:45 AM4/12/19
to Netty discussions
You can create one buffer and then use to send to all your sockets the very same buffer content but using different indexes (read here) using the ByteBuf.duplicate() method.


Javadoc says:

public abstract ByteBuf duplicate()
Returns a buffer which shares the whole region of this buffer. Modifying the content of the returned buffer or this buffer affects each other's content while they maintain separate indexes and marks. This method does not modify readerIndex or writerIndex of this buffer.

The reader and writer marks will not be duplicated. Also be aware that this method will NOT call retain() and so the reference count will NOT be increased.

Returns:
A buffer whose readable content is equivalent to the buffer returned by slice(). However this buffer will share the capacity of the underlying buffer, and therefore allows access to all of the underlying content if necessary.

Norman Maurer

unread,
Apr 12, 2019, 5:16:35 AM4/12/19
to ne...@googlegroups.com
Actually you want to call ByteBuf.retainedDuplicate() as you also need to increment the reference count.


--
You received this message because you are subscribed to the Google Groups "Netty discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to netty+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/netty/4c93f31c-a1f0-48be-94ef-b7cda094e2c5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

matya...@gmail.com

unread,
Apr 15, 2019, 1:51:44 AM4/15/19
to Netty discussions
... so something like this? Is this safe? Won't this create a memory leak?
String data = "some data";
TextWebSocketFrame frame = new TextWebSocketFrame(data)
channels.forEach(c -> {
        c.write(frame.duplicate().retain());
    }
}
frame.release()

On Friday, April 12, 2019 at 11:16:35 AM UTC+2, Norman Maurer wrote:
Actually you want to call ByteBuf.retainedDuplicate() as you also need to increment the reference count.
On 12. Apr 2019, at 08:19, Frederic <fredb...@free.fr> wrote:

You can create one buffer and then use to send to all your sockets the very same buffer content but using different indexes (read here) using the ByteBuf.duplicate() method.


Javadoc says:

public abstract ByteBuf duplicate()
Returns a buffer which shares the whole region of this buffer. Modifying the content of the returned buffer or this buffer affects each other's content while they maintain separate indexes and marks. This method does not modify readerIndex or writerIndex of this buffer.

The reader and writer marks will not be duplicated. Also be aware that this method will NOT call retain() and so the reference count will NOT be increased.

Returns:
A buffer whose readable content is equivalent to the buffer returned by slice(). However this buffer will share the capacity of the underlying buffer, and therefore allows access to all of the underlying content if necessary.

Le vendredi 12 avril 2019 06:31:57 UTC+2, matya...@gmail.com a écrit :
Are there any optimizations available if I'd like to send the very same data to dozens (or hundreds) of Websocket channels?
At a minimum I'd like to avoid creating (and copying) the data to new buffers. Ideally I would provide a single buffer and a list of channels.

--
You received this message because you are subscribed to the Google Groups "Netty discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ne...@googlegroups.com.

Norman Maurer

unread,
Apr 15, 2019, 1:52:07 AM4/15/19
to ne...@googlegroups.com
Yes exactly like this
--
You received this message because you are subscribed to the Google Groups "Netty discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to netty+un...@googlegroups.com.

matya...@gmail.com

unread,
Apr 16, 2019, 3:29:19 AM4/16/19
to Netty discussions
Thank you Norman.
Can you please be so kind and also address the the OutOfMemory situation described here https://github.com/netty/netty/issues/9036?
I am looking for a reliable way to back-pressure the producing loop (and not reach an OOM), but be as efficient with the sending as possible.


On Monday, April 15, 2019 at 7:52:07 AM UTC+2, Norman Maurer wrote:
Yes exactly like this

Am 12.04.2019 um 08:19 schrieb Frederic <fredb...@free.fr>:

You can create one buffer and then use to send to all your sockets the very same buffer content but using different indexes (read here) using the ByteBuf.duplicate() method.


Javadoc says:

public abstract ByteBuf duplicate()
Returns a buffer which shares the whole region of this buffer. Modifying the content of the returned buffer or this buffer affects each other's content while they maintain separate indexes and marks. This method does not modify readerIndex or writerIndex of this buffer.

The reader and writer marks will not be duplicated. Also be aware that this method will NOT call retain() and so the reference count will NOT be increased.

Returns:
A buffer whose readable content is equivalent to the buffer returned by slice(). However this buffer will share the capacity of the underlying buffer, and therefore allows access to all of the underlying content if necessary.

Le vendredi 12 avril 2019 06:31:57 UTC+2, matya...@gmail.com a écrit :
Are there any optimizations available if I'd like to send the very same data to dozens (or hundreds) of Websocket channels?
At a minimum I'd like to avoid creating (and copying) the data to new buffers. Ideally I would provide a single buffer and a list of channels.

--
You received this message because you are subscribed to the Google Groups "Netty discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ne...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages