func init() { if client, err := db.Client(); err != nil { glog.Errorln(err) } else { defer db.Release(client) { if len(config.Cfg.Metrics.AddScript) > 0 { if addSha, err = client.Cmd("SCRIPT", "LOAD", config.Cfg.Metrics.AddScript).Str(); err != nil { glog.Errorln(err) } else { glog.Infoln("ADD SHA", addSha) } } if len(config.Cfg.Metrics.GetScript) > 0 { if getSha, err = client.Cmd("SCRIPT", "LOAD", config.Cfg.Metrics.GetScript).Str(); err != nil { glog.Errorln(err) } else { glog.Infoln("GET SHA", getSha) } } if len(config.Cfg.Metrics.TtlScript) > 0 { if ttlSha, err = client.Cmd("SCRIPT", "LOAD", config.Cfg.Metrics.TtlScript).Str(); err != nil { glog.Errorln(err) } else { glog.Infoln("TTL SHA", ttlSha) } } } } }
// Release pushes back client to pool (if number of available clients in the pool is < MaxClients), // otherwise closes client func Release(client *redis.Client) { if client != nil { if len(clients) < config.Cfg.DB.MaxClients { glog.Infoln("Releasing Db Client") clients <- client glog.Infoln("Number of idle Db Clients: ", len(clients)) } else { glog.Infoln("Closing Db Client") if err := client.Close(); err != nil { glog.Warningln("Close error: ", err) } } } }
func main() { if config.Pprof.Cpu != "" { // Start Profiler f, err := os.Create(config.Pprof.Cpu) if err != nil { glog.Fatal(err) } defer f.Close() pprof.StartCPUProfile(f) defer pprof.StopCPUProfile() } http.HandleFunc("/render", render.Handler) glog.Infoln("ListenAndServe", config.Cfg.Webserver.Addr) go func() { glog.Fatal(http.ListenAndServe(config.Cfg.Webserver.Addr, nil)) }() // Handle SIGINT and SIGTERM. sig := make(chan os.Signal) signal.Notify(sig, os.Interrupt, os.Kill) glog.Errorf("Syscall: %v - Exit\n", <-sig) if config.Pprof.Mem != "" { // Start Mem Profiler f, err := os.Create(config.Pprof.Mem) if err != nil { glog.Fatal(err) } pprof.WriteHeapProfile(f) f.Close() } }
func Handler(w http.ResponseWriter, r *http.Request) { t := time.Now() glog.Infoln(r.RequestURI) // ResponseWriter wrapper w.Header().Set("Server", "YAG") w.Header().Set("Content-Type", "application/json") rw := &RenderResponseWriter{w: w} // Handler composition http.TimeoutHandler(&RenderHandler{}, time.Duration(config.Cfg.Webserver.Timeout)*time.Second, http.StatusText(http.StatusRequestTimeout)).ServeHTTP(rw, r) glog.Infof("[%v] in %v\n", rw.Code, time.Now().Sub(t)) }
func main() { if config.Pprof.Cpu != "" { // Start Cpu Profiler f, err := os.Create(config.Pprof.Cpu) if err != nil { glog.Fatal(err) } defer f.Close() pprof.StartCPUProfile(f) defer pprof.StopCPUProfile() } ln, err := net.Listen("tcp", config.Cfg.Listener.Addr) if err != nil { glog.Fatalln(err) } glog.Infoln("ListenAndServe", config.Cfg.Listener.Addr) go func() { for { conn, err := ln.Accept() if err != nil { glog.Errorln(err) continue } go handle(conn) } }() // Handle SIGINT and SIGTERM. sig := make(chan os.Signal) signal.Notify(sig, os.Interrupt, os.Kill) glog.Errorf("Syscall: %v - Exit\n", <-sig) if config.Pprof.Mem != "" { // Start Mem Profiler f, err := os.Create(config.Pprof.Mem) if err != nil { glog.Fatal(err) } pprof.WriteHeapProfile(f) f.Close() } }