Skip to content

Commit

Permalink
dial.go and listener.go: Slightly cleaned up logging.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sandertv committed Jun 21, 2024
1 parent 5942afc commit ccb02cc
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 4 deletions.
6 changes: 5 additions & 1 deletion conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,10 @@ func (conn *Conn) handlePacket(b []byte) error {
if len(b) == 0 {
return errZeroPacket
}
if conn.closing.Load() != 0 {
// Don't continue handling packets if the connection is being closed.
return nil
}
handled, err := conn.handler.handle(conn, b)
if err != nil {
return fmt.Errorf("handle packet: %w", err)
Expand Down Expand Up @@ -623,7 +627,7 @@ func (conn *Conn) writeTo(p []byte, raddr net.Addr) error {
if _, err := conn.conn.WriteTo(p, raddr); errors.Is(err, net.ErrClosed) {
return fmt.Errorf("write to: %w", err)
} else if err != nil {
conn.handler.log().Error("write to: " + err.Error())
conn.handler.log().Error("write to: "+err.Error(), "raddr", raddr.String())
}
return nil
}
Expand Down
3 changes: 2 additions & 1 deletion dial.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ func (dialer Dialer) DialContext(ctx context.Context, address string) (*Conn, er
if err != nil {
return nil, dialer.error("dial", err)
}
dialer.ErrorLog = dialer.ErrorLog.With("src", "dialer", "raddr", conn.RemoteAddr().String())

cs := &connState{conn: conn, raddr: conn.RemoteAddr(), id: atomic.AddInt64(&dialerID, 1), ticker: time.NewTicker(time.Second / 2)}
defer cs.ticker.Stop()
Expand Down Expand Up @@ -268,7 +269,7 @@ func (dialer Dialer) clientListen(rakConn *Conn, conn net.Conn) {
}
// Errors reading a packet other than the connection being
// closed may be worth logging.
dialer.ErrorLog.Error("client: " + err.Error())
dialer.ErrorLog.Error("handle packet: " + err.Error())
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,10 @@ func (h listenerConnectionHandler) handleUnconnected(b []byte, addr net.Addr) er
// In some cases, the client will keep trying to send datagrams
// while it has already timed out. In this case, we should not return
// an error.
h.log().Debug("unexpected datagram", "raddr", addr.String())
return nil
}
return fmt.Errorf("unknown packet received (id=%x, len=%v)", b[0], len(b))
return fmt.Errorf("unknown unconnected packet (id=%x, len=%v)", b[0], len(b))
}

// handleUnconnectedPing handles an unconnected ping packet stored in buffer b,
Expand Down
4 changes: 3 additions & 1 deletion listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ func (conf ListenConfig) Listen(address string) (*Listener, error) {
if conf.ErrorLog == nil {
conf.ErrorLog = slog.New(internal.DiscardHandler{})
}
conf.ErrorLog = conf.ErrorLog.With("src", "listener")

if conf.BlockDuration == 0 {
conf.BlockDuration = time.Second * 10
}
Expand Down Expand Up @@ -184,7 +186,7 @@ func (listener *Listener) listen() {
continue
}
if err = listener.handle(b[:n], addr); err != nil && !errors.Is(err, net.ErrClosed) {
listener.conf.ErrorLog.Error("listener: handle packet: "+err.Error(), "address", addr.String(), "block-duration", max(0, listener.conf.BlockDuration))
listener.conf.ErrorLog.Error("handle packet: "+err.Error(), "raddr", addr.String(), "block-duration", max(0, listener.conf.BlockDuration))
listener.sec.block(addr)
}
}
Expand Down

0 comments on commit ccb02cc

Please sign in to comment.