2013-07-23 11:10:38 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
2013-07-25 04:32:57 +00:00
|
|
|
"runtime"
|
2015-10-14 07:21:21 +00:00
|
|
|
"runtime/pprof"
|
2013-07-23 11:10:38 +00:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2015-10-13 11:33:51 +00:00
|
|
|
logger *GoDNSLogger
|
2013-07-23 11:10:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
|
2015-10-13 11:33:51 +00:00
|
|
|
initLogger()
|
2013-07-23 11:10:38 +00:00
|
|
|
|
|
|
|
server := &Server{
|
|
|
|
host: settings.Server.Host,
|
|
|
|
port: settings.Server.Port,
|
|
|
|
rTimeout: 5 * time.Second,
|
|
|
|
wTimeout: 5 * time.Second,
|
|
|
|
}
|
|
|
|
|
|
|
|
server.Run()
|
|
|
|
|
2015-10-13 11:33:51 +00:00
|
|
|
logger.Info("godns %s start", settings.Version)
|
2013-07-25 04:32:57 +00:00
|
|
|
|
2015-10-14 07:21:21 +00:00
|
|
|
if settings.Debug {
|
|
|
|
go profileCPU()
|
|
|
|
go profileMEM()
|
|
|
|
}
|
|
|
|
|
2013-07-23 11:10:38 +00:00
|
|
|
sig := make(chan os.Signal)
|
|
|
|
signal.Notify(sig, os.Interrupt)
|
|
|
|
|
|
|
|
forever:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-sig:
|
2015-10-13 11:33:51 +00:00
|
|
|
logger.Info("signal received, stopping")
|
2013-07-23 11:10:38 +00:00
|
|
|
break forever
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-10-14 07:21:21 +00:00
|
|
|
func profileCPU() {
|
|
|
|
f, err := os.Create("godns.cprof")
|
|
|
|
if err != nil {
|
|
|
|
logger.Error("%s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
pprof.StartCPUProfile(f)
|
|
|
|
time.AfterFunc(6*time.Minute, func() {
|
|
|
|
pprof.StopCPUProfile()
|
|
|
|
f.Close()
|
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func profileMEM() {
|
|
|
|
f, err := os.Create("godns.mprof")
|
|
|
|
if err != nil {
|
|
|
|
logger.Error("%s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
time.AfterFunc(5*time.Minute, func() {
|
|
|
|
pprof.WriteHeapProfile(f)
|
|
|
|
f.Close()
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-10-13 11:33:51 +00:00
|
|
|
func initLogger() {
|
|
|
|
logger = NewLogger()
|
|
|
|
|
|
|
|
if settings.Log.Stdout {
|
|
|
|
logger.SetLogger("console", nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
if settings.Log.File != "" {
|
|
|
|
config := map[string]interface{}{"file": settings.Log.File}
|
|
|
|
logger.SetLogger("file", config)
|
2013-07-23 11:10:38 +00:00
|
|
|
}
|
|
|
|
|
2015-10-13 11:33:51 +00:00
|
|
|
logger.SetLevel(settings.Log.LogLevel())
|
2013-07-23 11:10:38 +00:00
|
|
|
}
|
2013-07-25 04:32:57 +00:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
runtime.GOMAXPROCS(runtime.NumCPU())
|
|
|
|
}
|