In some situations, you may need to adjust the default buffer sizes of a Golang HTTP server.
Adjusting the default buffer sizes allows you to benefit from higher throughput and improve the performance of your HTTP servers overall.
You may also tune the performance of your Linux kernel overall [if you are hosting your application on a Linux system]
I recommend https://github.com/klaver/sysctl as a good starting point on this.
To avoid the occurrence of any inconveniences, please make sure not to blindly copy and then paste things. Feel free to check the documented comments above each kernel parameter before actually using it to learn what each parameter does :)
At the present, the Golang HTTP server does not have an option that allows you to configure the write buffer size for unknown reasons.
The net/http
package will use a hard coded 4 KB buffer size for the response writer.
To modify this, check out https://github.com/oddmario/go-http-server-custom-write-buffer-patch
- Recommended tuned value: a value of 8 KB to 1 MB should be optimal for a higher throughput.
This is achievable by hooking into the ConnContext
of a http.Server
instance.
oneLovelyServer := &http.Server{
...
ConnContext: func(ctx context.Context, c net.Conn) context.Context {
// https://github.com/golang/go/issues/50795
(c.(*net.TCPConn)).SetReadBuffer(4096)
(c.(*net.TCPConn)).SetWriteBuffer(4096)
return ctx
},
...
}
- Recommended tuned value: a value of 4 KB to 4 MB.