Differentiate UDP stream and TCP connection in logs

This commit is contained in:
Romain 2023-01-31 16:00:10 +01:00 committed by GitHub
parent 479878503d
commit 38f5024ed0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 11 deletions

View file

@ -48,14 +48,14 @@ func NewProxy(address string, terminationDelay time.Duration, proxyProtocol *dyn
// ServeTCP forwards the connection to a service.
func (p *Proxy) ServeTCP(conn WriteCloser) {
log.WithoutContext().Debugf("Handling connection from %s to %s", conn.RemoteAddr(), p.address)
log.WithoutContext().Debugf("Handling TCP connection from %s to %s", conn.RemoteAddr(), p.address)
// needed because of e.g. server.trackedConnection
defer conn.Close()
connBackend, err := p.dialBackend()
if err != nil {
log.WithoutContext().Errorf("Error while connecting to backend: %v", err)
log.WithoutContext().Errorf("Error while dialing backend: %v", err)
return
}
@ -66,7 +66,7 @@ func (p *Proxy) ServeTCP(conn WriteCloser) {
if p.proxyProtocol != nil && p.proxyProtocol.Version > 0 && p.proxyProtocol.Version < 3 {
header := proxyproto.HeaderProxyFromAddrs(byte(p.proxyProtocol.Version), conn.RemoteAddr(), conn.LocalAddr())
if _, err := header.WriteTo(connBackend); err != nil {
log.WithoutContext().Errorf("Error while writing proxy protocol headers to backend connection: %v", err)
log.WithoutContext().Errorf("Error while writing TCP proxy protocol headers to backend connection: %v", err)
return
}
}
@ -80,9 +80,9 @@ func (p *Proxy) ServeTCP(conn WriteCloser) {
// This allows to not report an RST packet sent by the peer as an error,
// as it is an abrupt but possible end for the TCP session
if isReadConnResetError(err) {
log.WithoutContext().Debugf("Error during connection: %v", err)
log.WithoutContext().Debugf("Error while handling TCP connection: %v", err)
} else {
log.WithoutContext().Errorf("Error during connection: %v", err)
log.WithoutContext().Errorf("Error while handling TCP connection: %v", err)
}
}
@ -119,7 +119,7 @@ func (p Proxy) connCopy(dst, src WriteCloser, errCh chan error) {
// In that case, logging the error is superfluous,
// as in the first place we should not have needed to call CloseWrite.
if !isSocketNotConnectedError(errClose) {
log.WithoutContext().Debugf("Error while terminating connection: %v", errClose)
log.WithoutContext().Debugf("Error while terminating TCP connection: %v", errClose)
}
return
@ -128,7 +128,7 @@ func (p Proxy) connCopy(dst, src WriteCloser, errCh chan error) {
if p.terminationDelay >= 0 {
err := dst.SetReadDeadline(time.Now().Add(p.terminationDelay))
if err != nil {
log.WithoutContext().Debugf("Error while setting deadline: %v", err)
log.WithoutContext().Debugf("Error while setting TCP connection deadline: %v", err)
}
}
}

View file

@ -20,14 +20,14 @@ func NewProxy(address string) (*Proxy, error) {
// ServeUDP implements the Handler interface.
func (p *Proxy) ServeUDP(conn *Conn) {
log.WithoutContext().Debugf("Handling connection from %s to %s", conn.rAddr, p.target)
log.WithoutContext().Debugf("Handling UDP stream from %s to %s", conn.rAddr, p.target)
// needed because of e.g. server.trackedConnection
defer conn.Close()
connBackend, err := net.Dial("udp", p.target)
if err != nil {
log.WithoutContext().Errorf("Error while connecting to backend: %v", err)
log.WithoutContext().Errorf("Error while dialing backend: %v", err)
return
}
@ -40,7 +40,7 @@ func (p *Proxy) ServeUDP(conn *Conn) {
err = <-errChan
if err != nil {
log.WithoutContext().Errorf("Error while serving UDP: %v", err)
log.WithoutContext().Errorf("Error while handling UDP stream: %v", err)
}
<-errChan
@ -55,6 +55,6 @@ func connCopy(dst io.WriteCloser, src io.Reader, errCh chan error) {
errCh <- err
if err := dst.Close(); err != nil {
log.WithoutContext().Debugf("Error while terminating connection: %v", err)
log.WithoutContext().Debugf("Error while terminating UDP stream: %v", err)
}
}