From 921e755171d8d33a5623eb88ba81140fd655ddd5 Mon Sep 17 00:00:00 2001 From: Tim Gross Date: Fri, 15 Sep 2023 15:19:13 -0400 Subject: [PATCH] helper: reduce size of buffer used by template connections In #12458 we added an in-memory connection buffer so that template runners that want access to the Nomad API for Service Registration and Variables can communicate with Nomad without having to create a real HTTP client. The size of this buffer (1 MiB) was taken directly from its usage in Vault, and each connection makes 2 such buffers (send and receive). Because each template runner has its own connection, when there are large numbers of allocations this adds up to significant memory usage. The largest Nomad Variable payload is 64KiB, and a small amount of metadata. Service Registration responses are much smaller, and we don't include check results in them (as Consul does), so the size is relatively bounded. We should be able to safely reduce the size of the buffer by a factor of 10 or more without forcing the template runner to make multiple read calls over the buffer. Fixes: #18508 --- .changelog/18524.txt | 3 +++ helper/bufconndialer/bufconndialer.go | 5 ++++- 2 files changed, 7 insertions(+), 1 deletion(-) create mode 100644 .changelog/18524.txt diff --git a/.changelog/18524.txt b/.changelog/18524.txt new file mode 100644 index 00000000000..f2f3a7d6b1b --- /dev/null +++ b/.changelog/18524.txt @@ -0,0 +1,3 @@ +```release-note:improvement +template: reduce memory usage associated with communicating with the Nomad API +``` diff --git a/helper/bufconndialer/bufconndialer.go b/helper/bufconndialer/bufconndialer.go index 2a7650d57d4..b334bc45455 100644 --- a/helper/bufconndialer/bufconndialer.go +++ b/helper/bufconndialer/bufconndialer.go @@ -24,7 +24,10 @@ type BufConnWrapper struct { // New returns a new BufConnWrapper with a new bufconn.Listener. The wrapper // provides a dialer for creating connections to the listener. func New() (net.Listener, *BufConnWrapper) { - ln := bufconn.Listen(1024 * 1024) + // this buffer is sized to accept a maximum-sized Nomad Variable payload + // (64k) with plenty of room to spare for the metadata and envelope, in a + // single read + ln := bufconn.Listen(1024 * 100) return ln, &BufConnWrapper{listener: ln} }