Skip to content

Commit

Permalink
fix: handle exiting channel read loop more nicely
Browse files Browse the repository at this point in the history
  • Loading branch information
carlmontanari committed Apr 2, 2024
1 parent 03bc0c8 commit 4a4b17a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
2 changes: 2 additions & 0 deletions channel/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ func (c *Channel) Open() (reterr error) {
func (c *Channel) Close() error {
c.l.Info("channel closing...")

close(c.Errs)

ch := make(chan struct{})

go func() {
Expand Down
21 changes: 16 additions & 5 deletions channel/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,22 @@ func (c *Channel) read() {

b, err := c.t.Read()
if err != nil {
select {
case <-c.done:
// this prevents us from ever writing to, what would in this case be, a closed
// errs channel. also if we are "done" we probably only got an error about transport
// dying so we can safely ignore that
return
default:
}

if errors.Is(err, io.EOF) {
// the underlying transport was closed so just return, we *probably* will have
// already bailed out by reading from the (maybe/probably) closed done channel, but
// if we hit EOF we know we are done anyway
return
}

// we got a transport error, put it into the error channel for processing during
// the next read activity, log it, sleep and then try again...
c.l.Criticalf(
Expand All @@ -58,11 +74,6 @@ func (c *Channel) read() {

c.Errs <- err

if errors.Is(err, io.EOF) {
// the underlying transport was closed so just return
return
}

time.Sleep(c.ReadDelay)

continue
Expand Down

0 comments on commit 4a4b17a

Please sign in to comment.