-
Notifications
You must be signed in to change notification settings - Fork 3.2k
fix: Fix the problem that some request connections are not released #8329
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -226,6 +226,7 @@ func (g *googleDriveClient) handleDownload(urlItem string, target string) error | |
| Proxy: http.ProxyURL(proxyURL), | ||
| }, | ||
| } | ||
| defer client.CloseIdleConnections() | ||
| response, err := client.Do(req) | ||
| if err != nil { | ||
| return err | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The There are no other issues or optimizations in this code snippet. |
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,14 +5,15 @@ import ( | |
| "encoding/json" | ||
| "errors" | ||
| "fmt" | ||
| "github.com/1Panel-dev/1Panel/core/app/dto" | ||
| "github.com/1Panel-dev/1Panel/core/i18n" | ||
| "io" | ||
| "net" | ||
| "net/http" | ||
| "net/url" | ||
| "os" | ||
| "strings" | ||
|
|
||
| "github.com/1Panel-dev/1Panel/core/app/dto" | ||
| "github.com/1Panel-dev/1Panel/core/i18n" | ||
| ) | ||
|
|
||
| func NewLocalClient(reqUrl, reqMethod string, body io.Reader) (interface{}, error) { | ||
|
|
@@ -31,6 +32,7 @@ func NewLocalClient(reqUrl, reqMethod string, body io.Reader) (interface{}, erro | |
| client := &http.Client{ | ||
| Transport: transport, | ||
| } | ||
| defer client.CloseIdleConnections() | ||
| parsedURL, err := url.Parse("http://unix") | ||
| if err != nil { | ||
| return nil, fmt.Errorf("handle url Parse failed, err: %v \n", err) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The provided code seems to be part of a function that creates an HTTP client for local operations. There are a few areas where improvements can be made:
Here’s an optimized version of the code: import (
"encoding/json"
"errors"
"fmt"
"io"
"net"
"net/http"
"os"
"strings"
dto "github.com/1Panel-dev/1Panel/core/app/dto"
i18n "github.com/1Panel-dev/1Panel/core/i18n"
)
func NewLocalClient(reqUrl, reqMethod string, body io.Reader) (interface{}, error) {
client := &http.Client{}
// Adjust transport based on requirements
transport := &http.Transport{
DialContext: (&net.Dialer{}).DialContext,
TLSHandshakeTimeout: 15 * time.Second,
}
switch reqUrl {
case dto.LocalRequestType:
host := getUnixSocketHost()
if host == "" {
return nil, i18n.GetMessage(i18n.MissingConfigKey)
}
transport.Dial = funcctx context.Context, _, _ string) (conn net.Conn, err error) {
conn, err = net.DialContext(ctx, "unix", host)
return
}
default:
// Use default TCP connection for other methods
transport.Dial = (&net.Dialer{}).Dial
}
client.Transport = transport
defer client.CloseIdleConnections()
request, err := http.NewRequestWithContext(context.Background(), reqMethod, reqUrl, body)
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}
responseBody, err := client.Do(request)
if err != nil {
return nil, fmt.Errorf("request execution failed: %w", err)
}
defer responseBody.Body.Close()
var result interface{}
switch responseType {
case respText:
bodyContent, err := ioutil.ReadAll(responseBody)
if err != nil {
return nil, fmt.Errorf("reading response content failed: %w", err)
}
result = string(bodyContent)
case respJSON:
err := json.NewDecoder(responseBody).Decode(&result)
if err != nil && !errors.As(err, new(UnexpectedEOF)) {
return nil, fmt.Errorf("decoding JSON response failed: %w", err)
}
default:
log.Println(fmt.Sprintf("Unsupported response type: %+v\n\n", responseType))
return map[string]interface{}{
"statusCode": int(responseBody.StatusCode),
}, nil // Return a generic status code map until proper handling implemented.
panic(errors.New("unreachable")) // Just to satisfy compile-time checks
}
return result, nil
}
// Utility functions omitted for brevityKey Changes:
These changes make the code cleaner and more robust, especially considering it's intended for local server interactions. |
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code appears to include several areas for improvement, primarily related to resource management and best practices:
Resource Management:
clientis created only once at the beginning of the functions and should be retained throughout their execution scope rather than being re-created on each request. This will help manage resources better.Closing Idle Connections:
defer client.CloseIdleConnections(). This can be removed as they are already cleaning up properly.Error Handling:
handleDownloadfunction checks ifresponse.Bodyis not nil before closing it to avoid errors when attempting to read from an unexpected response body closure.Here's a revised version of the code with these improvements:
Key Changes:
client) is reused across multiple requests within each function to improve efficiency.resp.Body.