Don't close channel if it's already closed

This commit is contained in:
Elara 2022-05-01 13:54:36 -07:00
parent 72068efb4d
commit 6df8cf53c6
1 changed files with 11 additions and 6 deletions

View File

@ -106,6 +106,7 @@ func (c *Client) Call(rcvr, method string, arg interface{}, ret interface{}) err
c.chs[chID] = make(chan *types.Response, 5) c.chs[chID] = make(chan *types.Response, 5)
c.chMtx.Unlock() c.chMtx.Unlock()
channelClosed := false
go func() { go func() {
// Get type of channel elements // Get type of channel elements
chElemType := retVal.Type().Elem() chElemType := retVal.Type().Elem()
@ -121,6 +122,8 @@ func (c *Client) Call(rcvr, method string, arg interface{}, ret interface{}) err
// Close return channel // Close return channel
retVal.Close() retVal.Close()
channelClosed = true
break break
} }
// Get reflect value from channel response // Get reflect value from channel response
@ -148,12 +151,14 @@ func (c *Client) Call(rcvr, method string, arg interface{}, ret interface{}) err
break break
} }
} }
c.Call("lrpc", "ChannelDone", id, nil) if !channelClosed {
// Close and delete channel c.Call("lrpc", "ChannelDone", id, nil)
c.chMtx.Lock() // Close and delete channel
close(c.chs[chID]) c.chMtx.Lock()
delete(c.chs, chID) close(c.chs[chID])
c.chMtx.Unlock() delete(c.chs, chID)
c.chMtx.Unlock()
}
}() }()
} else { } else {
// IF return value is not a pointer, return error // IF return value is not a pointer, return error