Fix bug where the connection handler tries to access a channel before it has been created

This commit is contained in:
Elara 2022-05-10 02:07:35 -07:00
parent 349123fe25
commit af77b121f8

View File

@ -128,7 +128,9 @@ func (c *Client) Call(ctx context.Context, rcvr, method string, arg interface{},
// Create new channel using channel ID // Create new channel using channel ID
c.chMtx.Lock() c.chMtx.Lock()
if _, ok := c.chs[chID]; !ok {
c.chs[chID] = make(chan *types.Response, 5) c.chs[chID] = make(chan *types.Response, 5)
}
c.chMtx.Unlock() c.chMtx.Unlock()
go func() { go func() {
@ -136,7 +138,6 @@ func (c *Client) Call(ctx context.Context, rcvr, method string, arg interface{},
chElemType := retVal.Type().Elem() chElemType := retVal.Type().Elem()
// For every value received from channel // For every value received from channel
for val := range c.chs[chID] { for val := range c.chs[chID] {
//s := time.Now()
if val.Type == types.ResponseTypeChannelDone { if val.Type == types.ResponseTypeChannelDone {
// Close and delete channel // Close and delete channel
c.chMtx.Lock() c.chMtx.Lock()
@ -215,11 +216,12 @@ func (c *Client) handleConn() {
} }
c.chMtx.Lock() c.chMtx.Lock()
// Get channel from map, skip if it doesn't exist // Attempt to get channel from map
ch, ok := c.chs[resp.ID] ch, ok := c.chs[resp.ID]
// If channel does not exist, make it
if !ok { if !ok {
c.chMtx.Unlock() ch = make(chan *types.Response, 5)
continue c.chs[resp.ID] = ch
} }
c.chMtx.Unlock() c.chMtx.Unlock()