Skip to content

Commit 6814ca8

Browse files
authored
Merge pull request #3022 from lifenjoiner/goroutine
Optimize the main slightly
2 parents a16e21c + 745d82c commit 6814ca8

1 file changed

Lines changed: 10 additions & 16 deletions

File tree

dnscrypt-proxy/main.go

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ import (
77
"fmt"
88
"math/rand"
99
"os"
10+
"os/signal"
1011
"runtime"
11-
"sync"
12+
"syscall"
1213

1314
"github.com/jedisct1/dlog"
1415
"github.com/kardianos/service"
@@ -20,8 +21,7 @@ const (
2021
)
2122

2223
type App struct {
23-
wg sync.WaitGroup
24-
quit chan struct{}
24+
quit chan os.Signal
2525
proxy *Proxy
2626
flags *ConfigFlags
2727
}
@@ -120,18 +120,17 @@ func main() {
120120
dlog.Fatal(err)
121121
}
122122
} else {
123-
app.Start(nil)
123+
app.quit = make(chan os.Signal, 1)
124+
signal.Notify(app.quit, os.Interrupt, syscall.SIGTERM)
125+
// Possible to exit while initializing
126+
go app.AppMain()
127+
<-app.quit
128+
dlog.Notice("Quit signal received...")
124129
}
125130
}
126131

127132
func (app *App) Start(service service.Service) error {
128-
if service != nil {
129-
go func() {
130-
app.AppMain()
131-
}()
132-
} else {
133-
app.AppMain()
134-
}
133+
go app.AppMain()
135134
return nil
136135
}
137136

@@ -149,13 +148,8 @@ func (app *App) AppMain() {
149148
if err := app.proxy.InitHotReload(); err != nil {
150149
dlog.Warnf("Failed to initialize hot-reloading: %v", err)
151150
}
152-
app.quit = make(chan struct{})
153-
app.wg.Add(1)
154151
app.proxy.StartProxy()
155152
runtime.GC()
156-
<-app.quit
157-
dlog.Notice("Quit signal received...")
158-
app.wg.Done()
159153
}
160154

161155
func (app *App) Stop(service service.Service) error {

0 commit comments

Comments
 (0)