我有以下简单的反向代理:
func main() {
go func() {
for range time.NewTicker(30 * time.Second).C {
log.Println("NUMBER OF OPEN CONNECTIONS")
}
}()
http.HandleFunc("/", handleRequestAndRedirect)
if err := http.ListenAndServe(getListenAddress(), nil); err != nil {
panic(err)
}
}
func handleRequestAndRedirect(res http.ResponseWriter, req *http.Request) {
serveReverseProxy("https://example.com", res, req)
}
func serveReverseProxy(target string, res http.ResponseWriter, req *http.Request) {
url, _ := url.Parse(target)
req.URL.Host = url.Host
req.URL.Scheme = url.Scheme
req.Host = url.Host
proxy := httputil.NewSingleHostReverseProxy(url)
proxy.ServeHTTP(res, req)
}
我要弄清楚的方法是每30秒打印一次当前打开的TCP连接数。我可以在http或httputil中访问某些属性来执行此操作吗?我有什么方法可以对我自己进行跟踪,而只需对代码进行最少的修改?
谢谢。