Skip to content

Commit

Permalink
Add ResponseHandler.CancelAllRequests and remove Client.ctx
Browse files Browse the repository at this point in the history
  • Loading branch information
javiercr committed May 21, 2024
1 parent 9d66dfb commit 79950cb
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
2 changes: 0 additions & 2 deletions messagix/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ type Client struct {
endpoints map[string]string
taskMutex *sync.Mutex
activeTasks []int
ctx context.Context

catRefreshLock sync.Mutex
unnecessaryCATRequests int
Expand Down Expand Up @@ -218,7 +217,6 @@ func (c *Client) Connect() error {
return err
}
ctx, cancel := context.WithCancel(context.TODO())
c.ctx = ctx
oldCancel := c.stopCurrentConnection.Swap(&cancel)
if oldCancel != nil {
(*oldCancel)()
Expand Down
14 changes: 11 additions & 3 deletions messagix/responsehandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,6 @@ func (p *ResponseHandler) waitForDetails(packetId uint16, channelType ChannelTyp
}

select {
case <-p.client.ctx.Done():
p.deleteDetails(packetId, channelType)
return nil, ErrContextCancelled
case response := <-ch:
p.deleteDetails(packetId, channelType)
return response, nil
Expand Down Expand Up @@ -147,3 +144,14 @@ func (p *ResponseHandler) getChannel(packetId uint16, channelType ChannelType) (
p.lock.RUnlock()
return ch, ok
}

func (p *ResponseHandler) CancelAllRequests() {
p.lock.Lock()
defer p.lock.Unlock()
for packetId := range p.requestChannels {
p.deleteDetails(packetId, RequestChannel)
}
for packetId := range p.packetChannels {
p.deleteDetails(packetId, PacketChannel)
}
}
1 change: 1 addition & 0 deletions messagix/socket.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ func (s *Socket) Connect() error {
}

err = s.readLoop(conn)
s.responseHandler.CancelAllRequests()
if err != nil {
return fmt.Errorf("%w: %w", ErrInReadLoop, err)
}
Expand Down

0 comments on commit 79950cb

Please sign in to comment.