perlin-network / noise

A decentralized P2P networking stack written in Go.
https://godoc.org/github.com/perlin-network/noise
MIT License
1.78k stars 213 forks source link

Remove unnecessary allocations and goroutines, and introduce worker pool for handling requests concurrently. #273

Closed iwasaki-kenta closed 4 years ago

iwasaki-kenta commented 4 years ago

This PR reduces allocations in a number of different areas and reduces the number of goroutines a single peer connection takes up by 1.

A worker pool is introduced to allow for the handling of multiple messages at once. The default number of workers is set to runtime.NumCPU().

No breaking changes were made in this PR.

aead: reuse buffers when possible and error if aead input is invalid node, node/opts, client, conn: remove one unnecessary goroutine, add worker pool for pipelining incoming peer messages, allow configuring size of worker pool, and update docs client: use single read buffer and write buffer, remove unnecessary bytes sent back/forth peers from handshake protocol logic, and simplify concurrency msg: prevent allocs on marshal by allowing a byte buffer to be passed in node: remove the need to resolve address every time a new connection is established to reduce allocs node/opts, readme: set default max recv message size to 4mb, and update readme id: copy host ip on unmarshal