Skip to content

Commit

Permalink
quic: implement error codes
Browse files Browse the repository at this point in the history
  • Loading branch information
sukunrt committed Aug 19, 2024
1 parent f38ad48 commit e8115a5
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 10 deletions.
9 changes: 9 additions & 0 deletions core/network/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package network

import (
"context"
"fmt"
"io"

ic "github.com/libp2p/go-libp2p/core/crypto"
Expand All @@ -13,6 +14,14 @@ import (

type ConnErrorCode uint32

type ConnError struct {
ErrorCode uint32
}

func (c *ConnError) Error() string {
return fmt.Sprintf("connection closed: code: %s", c.ErrorCode)

Check failure on line 22 in core/network/conn.go

View workflow job for this annotation

GitHub Actions / go-check / All

fmt.Sprintf format %s has arg c.ErrorCode of wrong type uint32

Check failure on line 22 in core/network/conn.go

View workflow job for this annotation

GitHub Actions / go-check / All

Printf format %s has arg #1 of wrong type uint32 (SA5009)

Check failure on line 22 in core/network/conn.go

View workflow job for this annotation

GitHub Actions / go-test / ubuntu (go 1.21.x)

fmt.Sprintf format %s has arg c.ErrorCode of wrong type uint32

Check failure on line 22 in core/network/conn.go

View workflow job for this annotation

GitHub Actions / go-test / ubuntu (go 1.22.x)

fmt.Sprintf format %s has arg c.ErrorCode of wrong type uint32

Check failure on line 22 in core/network/conn.go

View workflow job for this annotation

GitHub Actions / go-test / windows (go 1.21.x)

fmt.Sprintf format %s has arg c.ErrorCode of wrong type uint32

Check failure on line 22 in core/network/conn.go

View workflow job for this annotation

GitHub Actions / go-test / windows (go 1.22.x)

fmt.Sprintf format %s has arg c.ErrorCode of wrong type uint32

Check failure on line 22 in core/network/conn.go

View workflow job for this annotation

GitHub Actions / go-test / macos (go 1.21.x)

fmt.Sprintf format %s has arg c.ErrorCode of wrong type uint32

Check failure on line 22 in core/network/conn.go

View workflow job for this annotation

GitHub Actions / go-test / macos (go 1.22.x)

fmt.Sprintf format %s has arg c.ErrorCode of wrong type uint32
}

// Conn is a connection to a remote peer. It multiplexes streams.
// Usually there is no need to use a Conn directly, but it may
// be useful to get information about the peer on the other side:
Expand Down
11 changes: 10 additions & 1 deletion core/network/mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package network
import (
"context"
"errors"
"fmt"
"io"
"net"
"time"
Expand All @@ -14,7 +15,15 @@ var ErrReset = errors.New("stream reset")
type StreamErrorCode uint32

type StreamError struct {
ErrorCode int
ErrorCode StreamErrorCode
}

func (s *StreamError) Error() string {
return fmt.Sprintf("stream reset: code: %s", s.ErrorCode)

Check failure on line 22 in core/network/mux.go

View workflow job for this annotation

GitHub Actions / go-check / All

fmt.Sprintf format %s has arg s.ErrorCode of wrong type github.com/libp2p/go-libp2p/core/network.StreamErrorCode

Check failure on line 22 in core/network/mux.go

View workflow job for this annotation

GitHub Actions / go-check / All

Printf format %s has arg #1 of wrong type github.com/libp2p/go-libp2p/core/network.StreamErrorCode (SA5009)

Check failure on line 22 in core/network/mux.go

View workflow job for this annotation

GitHub Actions / go-test / ubuntu (go 1.21.x)

fmt.Sprintf format %s has arg s.ErrorCode of wrong type github.com/libp2p/go-libp2p/core/network.StreamErrorCode

Check failure on line 22 in core/network/mux.go

View workflow job for this annotation

GitHub Actions / go-test / ubuntu (go 1.22.x)

fmt.Sprintf format %s has arg s.ErrorCode of wrong type github.com/libp2p/go-libp2p/core/network.StreamErrorCode

Check failure on line 22 in core/network/mux.go

View workflow job for this annotation

GitHub Actions / go-test / windows (go 1.21.x)

fmt.Sprintf format %s has arg s.ErrorCode of wrong type github.com/libp2p/go-libp2p/core/network.StreamErrorCode

Check failure on line 22 in core/network/mux.go

View workflow job for this annotation

GitHub Actions / go-test / windows (go 1.22.x)

fmt.Sprintf format %s has arg s.ErrorCode of wrong type github.com/libp2p/go-libp2p/core/network.StreamErrorCode

Check failure on line 22 in core/network/mux.go

View workflow job for this annotation

GitHub Actions / go-test / macos (go 1.21.x)

fmt.Sprintf format %s has arg s.ErrorCode of wrong type github.com/libp2p/go-libp2p/core/network.StreamErrorCode

Check failure on line 22 in core/network/mux.go

View workflow job for this annotation

GitHub Actions / go-test / macos (go 1.22.x)

fmt.Sprintf format %s has arg s.ErrorCode of wrong type github.com/libp2p/go-libp2p/core/network.StreamErrorCode
}

func (s *StreamError) Is(target error) bool {
return target == ErrReset
}

// MuxedStream is a bidirectional io pipe within a connection.
Expand Down
29 changes: 20 additions & 9 deletions p2p/transport/quic/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package libp2pquic

import (
"errors"
"math"

"github.com/libp2p/go-libp2p/core/network"

Expand All @@ -18,20 +19,28 @@ type stream struct {

var _ network.MuxedStream = &stream{}

func parseStreamError(err error) error {
se := &quic.StreamError{}
if err != nil && errors.As(err, &se) {
code := se.ErrorCode
if code > math.MaxUint32 {
code = 0
}
err = &network.StreamError{
ErrorCode: network.StreamErrorCode(code),
}
}
return err
}

func (s *stream) Read(b []byte) (n int, err error) {
n, err = s.Stream.Read(b)
if err != nil && errors.Is(err, &quic.StreamError{}) {
err = network.ErrReset
}
return n, err
return n, parseStreamError(err)
}

func (s *stream) Write(b []byte) (n int, err error) {
n, err = s.Stream.Write(b)
if err != nil && errors.Is(err, &quic.StreamError{}) {
err = network.ErrReset
}
return n, err
return n, parseStreamError(err)
}

func (s *stream) Reset() error {
Expand All @@ -41,7 +50,9 @@ func (s *stream) Reset() error {
}

func (s *stream) ResetWithError(errCode network.StreamErrorCode) error {
panic("not implemented")
s.Stream.CancelRead(quic.StreamErrorCode(errCode))
s.Stream.CancelWrite(quic.StreamErrorCode(errCode))
return nil
}

func (s *stream) Close() error {
Expand Down

0 comments on commit e8115a5

Please sign in to comment.