From 14162b99a46ae155a0474dbc6a1768722f7f6285 Mon Sep 17 00:00:00 2001 From: Danny Kopping Date: Fri, 12 Dec 2025 14:56:16 +0200 Subject: [PATCH 1/2] fix: use small buffers to make tests more efficient Signed-off-by: Danny Kopping --- conn/batchsize_default.go | 12 ++++++++++++ conn/batchsize_testsmall.go | 16 ++++++++++++++++ conn/conn.go | 4 ---- device/queueconstants_default.go | 2 +- device/queueconstants_testsmall.go | 21 +++++++++++++++++++++ 5 files changed, 50 insertions(+), 5 deletions(-) create mode 100644 conn/batchsize_default.go create mode 100644 conn/batchsize_testsmall.go create mode 100644 device/queueconstants_testsmall.go diff --git a/conn/batchsize_default.go b/conn/batchsize_default.go new file mode 100644 index 000000000..d9e8ad7ed --- /dev/null +++ b/conn/batchsize_default.go @@ -0,0 +1,12 @@ +//go:build !testsmallbuffers + +/* SPDX-License-Identifier: MIT + * + * Copyright (C) 2017-2023 WireGuard LLC. All Rights Reserved. + */ + +package conn + +const ( + IdealBatchSize = 128 // maximum number of packets handled per read and write +) diff --git a/conn/batchsize_testsmall.go b/conn/batchsize_testsmall.go new file mode 100644 index 000000000..524ae3a8c --- /dev/null +++ b/conn/batchsize_testsmall.go @@ -0,0 +1,16 @@ +//go:build testsmallbuffers + +/* SPDX-License-Identifier: MIT + * + * Copyright (C) 2017-2023 WireGuard LLC. All Rights Reserved. + * + * Modified by Coder for test environments to reduce memory usage. + */ + +package conn + +// IdealBatchSize is reduced for tests to minimize memory usage. +// In production, this is 128 (see batchsize_default.go), but tests don't need +// the full batch capacity and this significantly reduces memory overhead +// from wireguard buffer pools (~8MB per connection down to ~256KB). +const IdealBatchSize = 4 diff --git a/conn/conn.go b/conn/conn.go index a1f57d2b1..f0221562a 100644 --- a/conn/conn.go +++ b/conn/conn.go @@ -15,10 +15,6 @@ import ( "strings" ) -const ( - IdealBatchSize = 128 // maximum number of packets handled per read and write -) - // A ReceiveFunc receives at least one packet from the network and writes them // into packets. On a successful read it returns the number of elements of // sizes, packets, and endpoints that should be evaluated. Some elements of diff --git a/device/queueconstants_default.go b/device/queueconstants_default.go index 9749cb789..3bd62ac51 100644 --- a/device/queueconstants_default.go +++ b/device/queueconstants_default.go @@ -1,4 +1,4 @@ -//go:build !android && !ios && !windows +//go:build !android && !ios && !windows && !testsmallbuffers /* SPDX-License-Identifier: MIT * diff --git a/device/queueconstants_testsmall.go b/device/queueconstants_testsmall.go new file mode 100644 index 000000000..0ab06c0fb --- /dev/null +++ b/device/queueconstants_testsmall.go @@ -0,0 +1,21 @@ +//go:build testsmallbuffers + +/* SPDX-License-Identifier: MIT + * + * Copyright (C) 2017-2023 WireGuard LLC. All Rights Reserved. + * + * Modified by Coder for test environments to reduce memory usage. + */ + +package device + +import "github.com/tailscale/wireguard-go/conn" + +const ( + QueueStagedSize = conn.IdealBatchSize + QueueOutboundSize = 64 // Reduced from 1024 for tests + QueueInboundSize = 64 // Reduced from 1024 for tests + QueueHandshakeSize = 64 // Reduced from 1024 for tests + MaxSegmentSize = 2048 // Reduced from 65535 for tests (enough for typical packets) + PreallocatedBuffersPerPool = 0 // Disable and allow for infinite memory growth +) From bb12dfe22d7ff8127ee642a6829dd2d384c05a98 Mon Sep 17 00:00:00 2001 From: Danny Kopping Date: Fri, 12 Dec 2025 15:42:29 +0200 Subject: [PATCH 2/2] fix: add !testsmallbuffers to platform-specific build tags Windows, Android, and iOS queueconstants files need to exclude testsmallbuffers to avoid duplicate constant declarations when building with -tags=testsmallbuffers. Signed-off-by: Danny Kopping --- device/queueconstants_android.go | 2 ++ device/queueconstants_ios.go | 2 +- device/queueconstants_windows.go | 2 ++ 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/device/queueconstants_android.go b/device/queueconstants_android.go index bab9625c4..44aaa3ddc 100644 --- a/device/queueconstants_android.go +++ b/device/queueconstants_android.go @@ -1,3 +1,5 @@ +//go:build android && !testsmallbuffers + /* SPDX-License-Identifier: MIT * * Copyright (C) 2017-2023 WireGuard LLC. All Rights Reserved. diff --git a/device/queueconstants_ios.go b/device/queueconstants_ios.go index acd3cec13..92bc96f3c 100644 --- a/device/queueconstants_ios.go +++ b/device/queueconstants_ios.go @@ -1,4 +1,4 @@ -//go:build ios +//go:build ios && !testsmallbuffers /* SPDX-License-Identifier: MIT * diff --git a/device/queueconstants_windows.go b/device/queueconstants_windows.go index 1eee32ba1..3e3f660af 100644 --- a/device/queueconstants_windows.go +++ b/device/queueconstants_windows.go @@ -1,3 +1,5 @@ +//go:build !testsmallbuffers + /* SPDX-License-Identifier: MIT * * Copyright (C) 2017-2023 WireGuard LLC. All Rights Reserved.