Skip to content

Commit

Permalink
Fix repeat close
Browse files Browse the repository at this point in the history
  • Loading branch information
zoujiaqing committed May 22, 2024
1 parent e4cd903 commit 3a7a005
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions RpcConnection.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type RpcConnection struct {
onMessageHanle MessageHandler
metadata map[string]string
remoteAddr net.Addr
connected bool
}

func NewRpcConnection(id uint64, ctx context.Context, conn quic.Connection, timeout uint) *RpcConnection {
Expand All @@ -32,6 +33,7 @@ func NewRpcConnection(id uint64, ctx context.Context, conn quic.Connection, time
broker: broker,
metadata: make(map[string]string),
remoteAddr: conn.RemoteAddr(),
connected: true,
}
connection.startReceive()
return connection
Expand Down Expand Up @@ -64,12 +66,16 @@ func (c *RpcConnection) OnClose(handle ClosedHandler) {

// Close 关闭连接
func (c *RpcConnection) Close() {
if !c.connected {
return
}
if c.onCloseHandle != nil {
c.onCloseHandle(c) // 调用 onClose 回调
}
if err := c.conn.CloseWithError(0, "connection closed"); err != nil {
log.Printf("Failed to close connection: %v", err)
}
c.connected = false
}

func (c *RpcConnection) Ping() (int, error) {
Expand Down Expand Up @@ -208,6 +214,7 @@ func (c *RpcConnection) startReceive() {
stream, err := c.conn.AcceptStream(c.ctx)
if err != nil {
log.Printf("Accept stream error: %v", err)
c.Close()
return
}

Expand Down

0 comments on commit 3a7a005

Please sign in to comment.