client close nicely

This commit is contained in:
ken 2025-04-04 20:33:44 +08:00
parent 2005fbcf44
commit 3a5af241e7
2 changed files with 8 additions and 4 deletions

View File

@ -49,6 +49,7 @@ type client struct {
wantResponseId uint64 wantResponseId uint64
wantResponse chan []byte wantResponse chan []byte
lock sync.Mutex lock sync.Mutex
once sync.Once
} }
func newClient(conn *websocket.Conn, a *Agent) *client { func newClient(conn *websocket.Conn, a *Agent) *client {
@ -142,9 +143,12 @@ func (c *client) writePump() {
} }
func (c *client) Close() { func (c *client) Close() {
c.fnCancel() once.Do(func(){
c.agent.removeClient(c) c.fnCancel()
c.conn.Close() c.agent.removeClient(c)
c.conn.Close()
log.Info("client close", "remote", c.conn.RemoteAddr().String(), "connected", c.agent.clients.Size())
})
} }
func (c *client) WriteJson(data interface{}, block bool) error { func (c *client) WriteJson(data interface{}, block bool) error {

View File

@ -47,8 +47,8 @@ func (task *Task) SendExecFail(errMsg string) {
func (task *Task) do() { func (task *Task) do() {
defer func() { defer func() {
task.pool.remove(task.client)
<-task.pool.sem <-task.pool.sem
task.pool.remove(task.client)
if err := recover(); err != nil { if err := recover(); err != nil {
log.Error("[Task] do panic", "err", err) log.Error("[Task] do panic", "err", err)
} }