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_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_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_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_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 +) 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.