Skip to content

Commit 781155b

Browse files
authored
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
1 parent 3caf0ac commit 781155b

File tree

2 files changed

+7
-10
lines changed

2 files changed

+7
-10
lines changed

core/init/proxy/proxy.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,18 @@ import (
88
"time"
99
)
1010

11-
var (
12-
sockPath = "/etc/1panel/agent.sock"
11+
const SockPath = "/etc/1panel/agent.sock"
1312

13+
var (
1414
LocalAgentProxy *httputil.ReverseProxy
1515
)
1616

1717
func Init() {
18+
dialer := &net.Dialer{
19+
Timeout: 5 * time.Second,
20+
}
1821
dialUnix := func(ctx context.Context, network, addr string) (net.Conn, error) {
19-
return net.Dial("unix", sockPath)
22+
return dialer.DialContext(ctx, "unix", SockPath)
2023
}
2124
transport := &http.Transport{
2225
DialContext: dialUnix,
@@ -33,7 +36,7 @@ func Init() {
3336
Transport: transport,
3437
ErrorHandler: func(rw http.ResponseWriter, req *http.Request, err error) {
3538
rw.WriteHeader(http.StatusBadGateway)
36-
rw.Write([]byte("Bad Gateway"))
39+
_, _ = rw.Write([]byte("Bad Gateway: " + err.Error()))
3740
},
3841
}
3942
}

core/init/router/proxy.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package router
33
import (
44
"net/http"
55
"net/url"
6-
"os"
76
"strconv"
87
"strings"
98

@@ -52,11 +51,6 @@ func Proxy() gin.HandlerFunc {
5251
}
5352

5453
if !strings.HasPrefix(c.Request.URL.Path, "/api/v2/core") && (currentNode == "local" || len(currentNode) == 0) {
55-
sockPath := "/etc/1panel/agent.sock"
56-
if _, err := os.Stat(sockPath); err != nil {
57-
helper.ErrorWithDetail(c, http.StatusBadRequest, "ErrProxy", err)
58-
return
59-
}
6054
defer func() {
6155
if err := recover(); err != nil && err != http.ErrAbortHandler {
6256
global.LOG.Debug(err)

0 commit comments

Comments
 (0)