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
This commit is contained in:
KOMATA 2025-12-04 18:07:47 +08:00 committed by GitHub
parent 3caf0ac529
commit 781155b029
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 7 additions and 10 deletions

View file

@ -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()))
},
}
}

View file

@ -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)