Nbio and how to handle the bufs

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?

I might try an arena. Not sure which is ultimately the best to go with, but I’d start with virtual arena.

Something like this. Maybe adjust to your flow and data structures.

buf_virt_arena: virtual.Arena
buf_alloc_arena := virtual.arena_allocator(&buf_virt_arena)

buffers := make([][]byte, 1, buf_alloc_arena)
buffers[0] = make([]byte, 1024*1024, buf_alloc_arena)

// when you want to start fresh and reuse buffers
// may be able to also just use virtual.arena_destroy and then reuse
// I include both for your edification
virtual.arena_free_all(&buf_virt_arena)
// resuse, etc....

//Then when you are totally done with it
virtual.arena_destroy(&buf_virt_arena)