netlink: add ExecuteFunc method to process large responses with low mem overhead #214
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
One of the great things you can do with netlink is query for detailed stats on all open TCP connections, including e.g. retransmits, how much data is in socket buffers, the computed BBR bandwidth estimates, etc. Unfortunately, if you ask the kernel for this info on a host with a lot of connections, the
[]netlink.Message
slice returned bynetlink/Conn.Execute
can take 50+ MB of space, when the slice will be (likely) be thrown away after its iterated over once.This PR adds a new method
ExecuteFunc
where a user can pass in a callback function that is called once per message. Becuase of howConn.receive
/the netlinkrecvmsg
API works in batches/streaming, this dramatically reduces the peak RSS/memory overhead required to process a large stream of netlink message responses.To try to minimize duplication, I refactored
receive
intoreceiveEach
which operates on a callback, changedlockedReceive
to callreceiveEach
, and then added a newlockedReceiveEach
which is callled byExecuteFunc
(I started with theEach
suffix, but then saw that a bunch of the callback variants in the Go stdlib have aFunc
suffix. LMK if you prefer one or the other or something else and I can standardize the naming).