The golang net/http package encountered the problem of http: superfluous response.WriteHeader call from xxx in the use of k8s

      In the project, you need to use golang to write an http server and deploy it in k8s. There are livenessProbe (survival probe) and readinessProbe (readiness probe) in server deployment.yaml, both of which I wrote to send an http to the server If the server receives and replies to the request, it means success, but during the use process, the log is always output, which will interfere with the viewing of the log.

After inquiring and testing, and finally repairing, make this record, the code is as follows.

func healthcheck(w http.ResponseWriter, r *http.Request) {
    //fmt.Fprintf(w, "i m live") 原来就是因为多了这行,然后一直输出,屏蔽了即可
	w.WriteHeader(200)
}

func main() {
    router := http.NewServeMux()
    router.HandleFunc("/healthcheck", healthcheck)
	port := fmt.Sprint(":", *config.Conf.HttpPort)
	srv := &http.Server{
		Addr:    port,
		Handler: router,
	}

	logger.Info("[HTTP] http server listen", port)
	if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
		logger.Error("[HTTP] server listen on", port, ", err:", err)
		os.Exit(1)
	}
}

 

Guess you like

Origin blog.csdn.net/banfushen007/article/details/112968249