Odin newbie here so looking for thoughts on how to handle the ‘bufs’ parameter when using nbio.
As a “learn Odin” thing I’m writing a client to talk to a nats.io server using the nbio-package.
The flow is to connect to the nats-server, then wait for the server to send some data and after that I send some data to the server.
So I do nbio.connect, the callback for connect will now set up to receive data from the server. The ‘bufs’ parameter to nbio.recv wants a [][]byte so I allocate that using
buffers := make([][]byte, 1, allocator)
buffers[0] = make([]byte, 1024*1024, allocator)
When the receive callback is invoked I can parse the data found in op.recv.bufs[0]
When I’m done with the parsing I want to either delete the buffers or reuse them somehow. This is where I struggle with finding a pattern.
Currently I’m keeping a reference to the slice of slices buffer in the user_data of the operation when I call recv so the callback can delete them after the callback is done parsing the contents of the buffers.
Is there a better/other way to handle this? How would you do it?
The next step in the flow is to send some data to the server, again passing a [][]byte to the nbio.send - is there some way I can reuse the memory I already allocated or is it better to delete and allocate new memory for the bufs in subsequent calls to nbio?