From cf95b2d94b00c43a5e80682b4a6cbe5d87ab0b79 Mon Sep 17 00:00:00 2001 From: Jimmy Zelinskie Date: Thu, 8 May 2014 06:48:32 -0400 Subject: [PATCH] improve comments and logging --- main.go | 25 ++++++++++++++++--------- server/server.go | 3 ++- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/main.go b/main.go index eb0bc09..8a753e3 100644 --- a/main.go +++ b/main.go @@ -30,28 +30,34 @@ func main() { flag.Parse() runtime.GOMAXPROCS(runtime.NumCPU()) + // Enable the profile if flagged. if profile { - log.Println("Running with profiling enabled") + log.Println("running with profiling enabled") f, err := os.Create("chihaya.cpu") 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() pprof.StartCPUProfile(f) } + // Load the config file. if configPath == "" { - log.Fatalf("Must specify a configuration file") + log.Fatalf("must specify a configuration file") } conf, err := config.Open(configPath) 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) 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() { c := make(chan os.Signal, 1) signal.Notify(c, os.Interrupt) @@ -61,18 +67,19 @@ func main() { pprof.StopCPUProfile() } - log.Println("Caught interrupt, shutting down..") + log.Println("caught interrupt, shutting down.") err := s.Stop() if err != nil { - panic("Failed to shutdown cleanly") + panic("failed to shutdown cleanly") } - log.Println("Shutdown successfully") + log.Println("shutdown successfully") <-c os.Exit(0) }() + // Start the server listening and handling requests. err = s.ListenAndServe() if err != nil { - log.Fatalf("Failed to start server: %s\n", err) + log.Fatalf("failed to start server: %s\n", err) } } diff --git a/server/server.go b/server/server.go index 447ae06..cc2d963 100644 --- a/server/server.go +++ b/server/server.go @@ -28,7 +28,7 @@ import ( type Server struct { conf *config.Config - // These are open connections. + // These are open connections/pools. listener *stoppableListener.StoppableListener trackerPool tracker.Pool 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) { errmsg := err.Error() + log.Println("handled failure: " + errmsg) msg := "d14:failure reason" + strconv.Itoa(len(errmsg)) + ":" + errmsg + "e" length, _ := io.WriteString(w, msg) w.Header().Add("Content-Length", string(length))