Skip to content

Commit

Permalink
Removed ClosedChannel errors on exit
Browse files Browse the repository at this point in the history
  • Loading branch information
Helge Olav Helgesen authored and tsaikd committed Oct 7, 2021
1 parent d8ca9c9 commit f4f89b9
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions input/socket/inputsocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package inputsocket
import (
"bufio"
"context"
"errors"
"io"
"net"
"os"
Expand Down Expand Up @@ -30,7 +31,7 @@ type InputConfig struct {
// For Unix networks, the address must be a file system path.
Address string `json:"address"`
ReusePort bool `json:"reuseport"`
BufferSize int `json:"buffer_size"`
BufferSize int `json:"buffer_size" yaml:"buffer_size"`
// packetmode is only valid for UDP sessions and handles each packet as a message on its own
PacketMode bool `json:"packetmode"`
}
Expand Down Expand Up @@ -143,6 +144,9 @@ func (i *InputConfig) Start(ctx context.Context, msgChan chan<- logevent.LogEven
for {
conn, err := l.Accept()
if err != nil {
if errors.Is(err, net.ErrClosed) {
return nil
}
return ErrorSocketAccept.New(err)
}
doneCh := make(chan struct{})
Expand Down Expand Up @@ -214,6 +218,9 @@ func (i *InputConfig) handleUDPpacketMode(ctx context.Context, conn net.PacketCo
case io.EOF:
return nil
default:
if errors.Is(err, net.ErrClosed) {
return nil // do not return error on closed socket
}
return err
}
}
Expand Down Expand Up @@ -243,12 +250,14 @@ func (i *InputConfig) handleUDP(ctx context.Context, conn net.PacketConn, msgCha
default:
}
n, _, err := conn.ReadFrom(b)
if err == io.EOF {
if n > 0 {
pw.Write(b[:n])
}
if errors.Is(err, net.ErrClosed) || err == io.EOF {
break
} else if err != nil {
return err
}
pw.Write(b[:n])
}
return nil
})
Expand Down

0 comments on commit f4f89b9

Please sign in to comment.