1Panel/agent/utils/websocket/client.go
KOMATA 38985671c6
perf: improve getNetConnections function and Websocket handling (#11269)
* feat: Enhance WebSocket client functionality and improve data processing

- Reduced message queue size in WebSocket client from 100 to 32.
- Introduced atomic boolean to track client closure state.
- Added SendPayload method to handle message sending with queue management.
- Updated ProcessData function to utilize SendPayload for sending responses.
- Expanded netTypes to include both IPv4 and IPv6 protocols in network connection retrieval.
- Improved net connection processing by using a map for process names, enhancing efficiency.

* feat: Enhance WebSocket client and process data handling

- Added synchronization with sync.Once for safe closure of WebSocket client.
- Updated message queue size to a constant for better maintainability.
- Implemented context timeouts for process data retrieval to prevent blocking.
- Improved network connection handling by utilizing a more efficient method for retrieving connections.
- Introduced a new function to determine connection types based on protocol family.

* feat: Enhance network connection retrieval and process name mapping

- Updated getNetConnections function to improve efficiency by using maps for process names and connections.
- Introduced a new helper function to retrieve process names from the filesystem or process context.
- Enhanced filtering logic for network connections based on process ID, name, and port.
- Increased initial capacity for connection results to optimize performance.

* refactor: Rename SendPayload method to Send in WebSocket client

- Updated the SendPayload method to be more succinctly named Send for clarity.
- Ensured the method continues to handle message sending while maintaining existing functionality.

* refactor: Update ProcessData and getNetConnections for improved clarity and efficiency

- Replaced SendPayload method calls with Send for consistency in WebSocket message handling.
- Enhanced getNetConnections function by refining process name retrieval and filtering logic.
- Improved error handling in getProcessNameWithContext for better robustness.

* refactor: Simplify WebSocket client closure and reading logic

- Removed unnecessary synchronization for closing the WebSocket client.
- Updated the Read method to handle message reading directly without a separate Close method.
- Ensured the Socket is closed properly after reading messages to prevent resource leaks.
2025-12-09 17:30:12 +08:00

59 lines
864 B
Go

package websocket
import (
"sync/atomic"
"github.com/gorilla/websocket"
)
const MaxMessageQuenue = 32
type Client struct {
ID string
Socket *websocket.Conn
Msg chan []byte
closed atomic.Bool
}
func NewWsClient(ID string, socket *websocket.Conn) *Client {
return &Client{
ID: ID,
Socket: socket,
Msg: make(chan []byte, MaxMessageQuenue),
}
}
func (c *Client) Read() {
defer func() {
c.closed.Store(true)
close(c.Msg)
}()
for {
_, message, err := c.Socket.ReadMessage()
if err != nil {
return
}
ProcessData(c, message)
}
}
func (c *Client) Write() {
defer c.Socket.Close()
for {
message, ok := <-c.Msg
if !ok {
return
}
_ = c.Socket.WriteMessage(websocket.TextMessage, message)
}
}
func (c *Client) Send(res []byte) {
if c.closed.Load() {
return
}
select {
case c.Msg <- res:
default:
}
}