refactor: 优化日志记录和变量作用域
- 修复了日志变量 l 的作用域问题,将其移至更合适的位置 - 删除了不必要的日志记录,包括代理请求和响应的日志 - 移除了健康检查中的状态变更日志 - 注释掉了服务器管理器中的调试日志 - 优化了代码结构,提高了代码可读性和性能
This commit is contained in:
parent
1cb0152147
commit
8c054e8f49
|
@ -22,10 +22,11 @@ type ProxyHandler struct {
|
|||
}
|
||||
|
||||
func (p *ProxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
l := gologger.GetLogger("Proxy")
|
||||
originalUrl := r.Host + r.URL.String()
|
||||
s, err := r.Cookie("s")
|
||||
var proxyIndex int
|
||||
l := gologger.GetLogger("Proxy") // 修复日志变量作用域
|
||||
|
||||
if err != nil {
|
||||
proxyIndex = p.count
|
||||
p.count++
|
||||
|
@ -47,7 +48,7 @@ func (p *ProxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||
proxyIndex = (proxyIndex + 1) % len(p.proxy) // 选择下一个上游服务器
|
||||
}
|
||||
|
||||
l.Error(fmt.Sprintf("All upstream servers are unhealthy"))
|
||||
l.Error("All upstream servers are unhealthy")
|
||||
http.Error(w, "Service Unavailable", http.StatusServiceUnavailable)
|
||||
}
|
||||
|
||||
|
@ -59,7 +60,6 @@ func (p *ProxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||
// 返回值:
|
||||
// httputil.ReverseProxy实例
|
||||
func makeProxy(upstream string, path *model.HttpPath, index int) *httputil.ReverseProxy {
|
||||
l := gologger.GetLogger("Proxy")
|
||||
p := &httputil.ReverseProxy{}
|
||||
p.Transport = &http.Transport{
|
||||
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
||||
|
@ -82,12 +82,12 @@ func makeProxy(upstream string, path *model.HttpPath, index int) *httputil.Rever
|
|||
p.Director = func(req *http.Request) {
|
||||
for _, handler := range directiveHandlers {
|
||||
handler(req)
|
||||
l.Info(fmt.Sprintf("proxy %s to %s", req.URL.String(), upstream))
|
||||
// 删除代理请求的详细日志记录
|
||||
}
|
||||
}
|
||||
|
||||
p.ModifyResponse = func(resp *http.Response) error {
|
||||
l.Info(fmt.Sprintf("proxy %s to %s, with status %d", resp.Request.URL.String(), upstream, resp.StatusCode))
|
||||
// 删除代理响应日志记录
|
||||
hasSticky := false
|
||||
for _, cookie := range resp.Cookies() {
|
||||
if cookie.Name == "s" {
|
||||
|
|
|
@ -44,7 +44,6 @@ func (hc *HealthChecker) CheckHealth(url string) bool {
|
|||
|
||||
// StartHealthCheck 启动健康检查协程
|
||||
func (hc *HealthChecker) StartHealthCheck(upstreams []string, onStatusChange func(string, bool)) {
|
||||
logger := gologger.GetLogger("healthcheck")
|
||||
go func() {
|
||||
statusMap := make(map[string]bool)
|
||||
for {
|
||||
|
@ -52,7 +51,6 @@ func (hc *HealthChecker) StartHealthCheck(upstreams []string, onStatusChange fun
|
|||
healthy := hc.CheckHealth(upstream)
|
||||
if status, exists := statusMap[upstream]; exists {
|
||||
if status != healthy {
|
||||
logger.Info(fmt.Sprintf("Upstream %s status changed to %v", upstream, healthy))
|
||||
if onStatusChange != nil {
|
||||
onStatusChange(upstream, healthy)
|
||||
}
|
||||
|
|
|
@ -17,7 +17,7 @@ import (
|
|||
var ServerManager map[string]*ServerListener = make(map[string]*ServerListener)
|
||||
|
||||
func makeMatcher(name model.Strings, s *ServerListener) cmux.Matcher {
|
||||
l := logger.GetLogger("ServerListener")
|
||||
// l := logger.GetLogger("ServerListener")
|
||||
return func(r io.Reader) bool {
|
||||
if s.ServerCount() == 1 {
|
||||
return true
|
||||
|
@ -26,13 +26,13 @@ func makeMatcher(name model.Strings, s *ServerListener) cmux.Matcher {
|
|||
if err != nil {
|
||||
return false
|
||||
}
|
||||
l.Debug("Request Host:", req.Host, "URL Host:", req.URL.Host, "Name:", name)
|
||||
// l.Debug("Request Host:", req.Host, "URL Host:", req.URL.Host, "Name:", name)
|
||||
return name.HasOrContainPrefix(req.Host) || name.HasOrContainPrefix(req.URL.Host)
|
||||
}
|
||||
}
|
||||
|
||||
func makeSSLMatcher(name model.Strings, s *ServerListener) cmux.Matcher {
|
||||
l := logger.GetLogger("ServerListener")
|
||||
// l := logger.GetLogger("ServerListener")
|
||||
tlsMathcer := cmux.TLS()
|
||||
return func(r io.Reader) bool {
|
||||
isTls := tlsMathcer(r)
|
||||
|
@ -42,7 +42,7 @@ func makeSSLMatcher(name model.Strings, s *ServerListener) cmux.Matcher {
|
|||
if err != nil {
|
||||
return false
|
||||
}
|
||||
l.Debug("Request Host:", string(buf[:n]), "Name:", name)
|
||||
// l.Debug("Request Host:", string(buf[:n]), "Name:", name)
|
||||
return name.HasOrContain(string(buf[:n]))
|
||||
}
|
||||
return false
|
||||
|
|
Loading…
Reference in New Issue