From 781155b029f10a5196c072393ca9fde23d8f8b67 Mon Sep 17 00:00:00 2001 From: KOMATA <20227709+HynoR@users.noreply.github.com> Date: Thu, 4 Dec 2025 18:07:47 +0800 Subject: [PATCH] perf: remove useless stat checking logic (#11178) * feat: Enhance proxy initialization and error handling * Add a timeout to the dialer for Unix socket connections * Improve error response by including the error message in the "Bad Gateway" response * refactor: Change sockPath variable to constant in proxy initialization * Update sockPath to a constant SockPath for improved clarity and consistency * Ensure the new constant is used in the dialer function for Unix socket connections --- core/init/proxy/proxy.go | 11 +++++++---- core/init/router/proxy.go | 6 ------ 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/core/init/proxy/proxy.go b/core/init/proxy/proxy.go index efda4a052..7cbb9526c 100644 --- a/core/init/proxy/proxy.go +++ b/core/init/proxy/proxy.go @@ -8,15 +8,18 @@ import ( "time" ) -var ( - sockPath = "/etc/1panel/agent.sock" +const SockPath = "/etc/1panel/agent.sock" +var ( LocalAgentProxy *httputil.ReverseProxy ) func Init() { + dialer := &net.Dialer{ + Timeout: 5 * time.Second, + } dialUnix := func(ctx context.Context, network, addr string) (net.Conn, error) { - return net.Dial("unix", sockPath) + return dialer.DialContext(ctx, "unix", SockPath) } transport := &http.Transport{ DialContext: dialUnix, @@ -33,7 +36,7 @@ func Init() { Transport: transport, ErrorHandler: func(rw http.ResponseWriter, req *http.Request, err error) { rw.WriteHeader(http.StatusBadGateway) - rw.Write([]byte("Bad Gateway")) + _, _ = rw.Write([]byte("Bad Gateway: " + err.Error())) }, } } diff --git a/core/init/router/proxy.go b/core/init/router/proxy.go index c122f62ee..783c1917d 100644 --- a/core/init/router/proxy.go +++ b/core/init/router/proxy.go @@ -3,7 +3,6 @@ package router import ( "net/http" "net/url" - "os" "strconv" "strings" @@ -52,11 +51,6 @@ func Proxy() gin.HandlerFunc { } if !strings.HasPrefix(c.Request.URL.Path, "/api/v2/core") && (currentNode == "local" || len(currentNode) == 0) { - sockPath := "/etc/1panel/agent.sock" - if _, err := os.Stat(sockPath); err != nil { - helper.ErrorWithDetail(c, http.StatusBadRequest, "ErrProxy", err) - return - } defer func() { if err := recover(); err != nil && err != http.ErrAbortHandler { global.LOG.Debug(err)