From a28e4c6892ec28c8f5f1a4341090ae1e5ea12d54 Mon Sep 17 00:00:00 2001 From: Joseph Copenhaver Date: Thu, 17 Oct 2024 22:57:53 -0500 Subject: [PATCH] minor best practice style improvement - stops extra allocations on once-calls --- loadtester/example/main.go | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/loadtester/example/main.go b/loadtester/example/main.go index fdea6be..86a0e7a 100644 --- a/loadtester/example/main.go +++ b/loadtester/example/main.go @@ -126,17 +126,14 @@ func main() { // // define input handling channel and closer - inputChan := make(chan string) - closeInputChan := func() func() { - var once sync.Once + inputChan, closeInputChan := func() (chan string, func()) { + c := make(chan string) - c := inputChan + closer := sync.OnceFunc(func() { + close(c) + }) - return func() { - once.Do(func() { - close(c) - }) - } + return c, closer }() // @@ -169,7 +166,7 @@ func main() { // note it's possible for this channel // write to panic due to the user - // doing thing really fast and pressing control-c afterward + // doing things really fast and pressing control-c afterward // // but in that case we're still going through the stop procedure so meh