Skip to content

Commit

Permalink
Fix dns server shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
owenthereal committed Sep 3, 2023
1 parent 5784b3f commit 23bfd16
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 13 deletions.
11 changes: 0 additions & 11 deletions dns/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"net"
"sync"
"time"

"github.com/miekg/dns"
Expand Down Expand Up @@ -41,36 +40,26 @@ func (d *dnsServer) Run(ctx context.Context) error {

var g run.Group
{
var wg sync.WaitGroup
wg.Add(1)
udp := &dns.Server{
Handler: mux,
Addr: d.cfg.Addr,
Net: "udp",
}
g.Add(func() error {
wg.Done()
return udp.ListenAndServe()
}, func(err error) {
// Wait for udp server before shutting it down
wg.Wait()
_ = udp.ShutdownContext(ctx)
})
}
{
var wg sync.WaitGroup
wg.Add(1)
tcp := &dns.Server{
Handler: mux,
Addr: d.cfg.Addr,
Net: "tcp",
}
g.Add(func() error {
wg.Done()
return tcp.ListenAndServe()
}, func(err error) {
// Wait for tcp server before shutting it down
wg.Wait()
_ = tcp.ShutdownContext(ctx)
})
}
Expand Down
12 changes: 10 additions & 2 deletions server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,12 @@ func Test_Server(t *testing.T) {
DnsAddr: dnsAddr,
})
errch := make(chan error)

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

go func() {
err := svr.Run(context.Background())
err := svr.Run(ctx)
if err != nil {
candy.Log().Error("error running server", zap.Error(err))
}
Expand Down Expand Up @@ -259,8 +263,12 @@ func Test_Server_Shutdown(t *testing.T) {
t.Run(c.Name, func(t *testing.T) {
errch := make(chan error)
srv := New(c.Config)

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

go func() {
err := srv.Run(context.Background())
err := srv.Run(ctx)
if err != nil {
candy.Log().Error("error running server", zap.Error(err))
}
Expand Down

0 comments on commit 23bfd16

Please sign in to comment.