From b6e05d956adeaa23cbc74ddb6c75d9d4551c3b65 Mon Sep 17 00:00:00 2001 From: Denis Tingaikin Date: Wed, 20 Jan 2021 19:32:18 +0700 Subject: [PATCH] properly close workerchannel Signed-off-by: Denis Tingaikin --- fanout.go | 9 +++++++-- setup_test.go | 2 +- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/fanout.go b/fanout.go index 56608fa..1d7d5e7 100644 --- a/fanout.go +++ b/fanout.go @@ -78,12 +78,17 @@ func (f *Fanout) ServeDNS(ctx context.Context, w dns.ResponseWriter, m *dns.Msg) defer cancel() clientCount := len(f.clients) workerChannel := make(chan Client, f.workerCount) - defer close(workerChannel) responseCh := make(chan *response, clientCount) go func() { + defer close(workerChannel) for i := 0; i < clientCount; i++ { client := f.clients[i] - workerChannel <- client + select { + case <-timeoutContext.Done(): + return + case workerChannel <- client: + continue + } } }() for i := 0; i < f.workerCount; i++ { diff --git a/setup_test.go b/setup_test.go index 2042bcf..0497e47 100644 --- a/setup_test.go +++ b/setup_test.go @@ -104,7 +104,7 @@ func TestSetupResolvconf(t *testing.T) { const resolv = "resolv.conf" if err := ioutil.WriteFile(resolv, []byte(`nameserver 10.10.255.252 -nameserver 10.10.255.253`), 0666); err != nil { +nameserver 10.10.255.253`), 0600); err != nil { t.Fatalf("Failed to write resolv.conf file: %s", err) } defer func() {