Skip to content

Commit

Permalink
refactor(sse): 优化客户端关闭逻辑
Browse files Browse the repository at this point in the history
- 在 Client 结构中添加 Close 方法,用于关闭客户端连接
- 在 ClientManager 中使用 Close 方法替代直接关闭 client.Send通道
  • Loading branch information
aide-cloud committed Jan 9, 2025
1 parent 4bc9579 commit 8598596
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion pkg/helper/sse/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ type Client struct {
Send chan []byte
}

// Close closes the client
func (c *Client) Close() {
close(c.Send)
}

// NewClientManager creates a new client manager
func NewClientManager() *ClientManager {
return &ClientManager{
Expand All @@ -46,7 +51,7 @@ func (cm *ClientManager) AddClient(client *Client) {
// RemoveClient removes a client from the manager
func (cm *ClientManager) RemoveClient(id uint32) {
if client, ok := cm.clients.Get(id); ok {
close(client.Send)
client.Close()
cm.clients.Delete(id)
}
}
Expand Down

0 comments on commit 8598596

Please sign in to comment.