improve comments and logging
This commit is contained in:
parent
90491dc386
commit
cf95b2d94b
2 changed files with 18 additions and 10 deletions
25
main.go
25
main.go
|
@ -30,28 +30,34 @@ func main() {
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
runtime.GOMAXPROCS(runtime.NumCPU())
|
runtime.GOMAXPROCS(runtime.NumCPU())
|
||||||
|
|
||||||
|
// Enable the profile if flagged.
|
||||||
if profile {
|
if profile {
|
||||||
log.Println("Running with profiling enabled")
|
log.Println("running with profiling enabled")
|
||||||
f, err := os.Create("chihaya.cpu")
|
f, err := os.Create("chihaya.cpu")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Failed to create profile file: %s\n", err)
|
log.Fatalf("failed to create profile file: %s\n", err)
|
||||||
}
|
}
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
pprof.StartCPUProfile(f)
|
pprof.StartCPUProfile(f)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Load the config file.
|
||||||
if configPath == "" {
|
if configPath == "" {
|
||||||
log.Fatalf("Must specify a configuration file")
|
log.Fatalf("must specify a configuration file")
|
||||||
}
|
}
|
||||||
conf, err := config.Open(configPath)
|
conf, err := config.Open(configPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Failed to parse configuration file: %s\n", err)
|
log.Fatalf("failed to parse configuration file: %s\n", err)
|
||||||
}
|
}
|
||||||
|
log.Println("succesfully loaded config")
|
||||||
|
|
||||||
|
// Create a new server.
|
||||||
s, err := server.New(conf)
|
s, err := server.New(conf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Failed to create server: %s\n", err)
|
log.Fatalf("failed to create server: %s\n", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Spawn a goroutine to handle interrupts and safely shut down.
|
||||||
go func() {
|
go func() {
|
||||||
c := make(chan os.Signal, 1)
|
c := make(chan os.Signal, 1)
|
||||||
signal.Notify(c, os.Interrupt)
|
signal.Notify(c, os.Interrupt)
|
||||||
|
@ -61,18 +67,19 @@ func main() {
|
||||||
pprof.StopCPUProfile()
|
pprof.StopCPUProfile()
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Println("Caught interrupt, shutting down..")
|
log.Println("caught interrupt, shutting down.")
|
||||||
err := s.Stop()
|
err := s.Stop()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic("Failed to shutdown cleanly")
|
panic("failed to shutdown cleanly")
|
||||||
}
|
}
|
||||||
log.Println("Shutdown successfully")
|
log.Println("shutdown successfully")
|
||||||
<-c
|
<-c
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
// Start the server listening and handling requests.
|
||||||
err = s.ListenAndServe()
|
err = s.ListenAndServe()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Failed to start server: %s\n", err)
|
log.Fatalf("failed to start server: %s\n", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,7 +28,7 @@ import (
|
||||||
type Server struct {
|
type Server struct {
|
||||||
conf *config.Config
|
conf *config.Config
|
||||||
|
|
||||||
// These are open connections.
|
// These are open connections/pools.
|
||||||
listener *stoppableListener.StoppableListener
|
listener *stoppableListener.StoppableListener
|
||||||
trackerPool tracker.Pool
|
trackerPool tracker.Pool
|
||||||
backendConn backend.Conn
|
backendConn backend.Conn
|
||||||
|
@ -130,6 +130,7 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
func fail(err error, w http.ResponseWriter, r *http.Request) {
|
func fail(err error, w http.ResponseWriter, r *http.Request) {
|
||||||
errmsg := err.Error()
|
errmsg := err.Error()
|
||||||
|
log.Println("handled failure: " + errmsg)
|
||||||
msg := "d14:failure reason" + strconv.Itoa(len(errmsg)) + ":" + errmsg + "e"
|
msg := "d14:failure reason" + strconv.Itoa(len(errmsg)) + ":" + errmsg + "e"
|
||||||
length, _ := io.WriteString(w, msg)
|
length, _ := io.WriteString(w, msg)
|
||||||
w.Header().Add("Content-Length", string(length))
|
w.Header().Add("Content-Length", string(length))
|
||||||
|
|
Loading…
Add table
Reference in a new issue