Skip to content

Commit

Permalink
opt: reorg errors in gnet
Browse files Browse the repository at this point in the history
  • Loading branch information
panjf2000 committed Jul 4, 2020
1 parent 13b45a4 commit 1c2f4b0
Show file tree
Hide file tree
Showing 18 changed files with 91 additions and 76 deletions.
30 changes: 16 additions & 14 deletions codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (
"encoding/binary"
"errors"
"fmt"

errorset "github.com/panjf2000/gnet/errors"
)

// CRLFByte represents a byte of CRLF.
Expand Down Expand Up @@ -91,7 +93,7 @@ func (cc *LineBasedFrameCodec) Decode(c Conn) ([]byte, error) {
buf := c.Read()
idx := bytes.IndexByte(buf, CRLFByte)
if idx == -1 {
return nil, errCRLFNotFound
return nil, errorset.ErrCRLFNotFound
}
c.ShiftN(idx + 1)
return buf[:idx], nil
Expand All @@ -112,7 +114,7 @@ func (cc *DelimiterBasedFrameCodec) Decode(c Conn) ([]byte, error) {
buf := c.Read()
idx := bytes.IndexByte(buf, cc.delimiter)
if idx == -1 {
return nil, errDelimiterNotFound
return nil, errorset.ErrDelimiterNotFound
}
c.ShiftN(idx + 1)
return buf[:idx], nil
Expand All @@ -126,7 +128,7 @@ func NewFixedLengthFrameCodec(frameLength int) *FixedLengthFrameCodec {
// Encode ...
func (cc *FixedLengthFrameCodec) Encode(c Conn, buf []byte) ([]byte, error) {
if len(buf)%cc.frameLength != 0 {
return nil, errInvalidFixedLength
return nil, errorset.ErrInvalidFixedLength
}
return buf, nil
}
Expand All @@ -135,7 +137,7 @@ func (cc *FixedLengthFrameCodec) Encode(c Conn, buf []byte) ([]byte, error) {
func (cc *FixedLengthFrameCodec) Decode(c Conn) ([]byte, error) {
size, buf := c.ReadN(cc.frameLength)
if size == 0 {
return nil, errUnexpectedEOF
return nil, errorset.ErrUnexpectedEOF
}
c.ShiftN(size)
return buf, nil
Expand Down Expand Up @@ -183,7 +185,7 @@ func (cc *LengthFieldBasedFrameCodec) Encode(c Conn, buf []byte) (out []byte, er
}

if length < 0 {
return nil, errTooLessLength
return nil, errorset.ErrTooLessLength
}

switch cc.encoderConfig.LengthFieldLength {
Expand All @@ -210,7 +212,7 @@ func (cc *LengthFieldBasedFrameCodec) Encode(c Conn, buf []byte) (out []byte, er
out = make([]byte, 8)
cc.encoderConfig.ByteOrder.PutUint64(out, uint64(length))
default:
return nil, errUnsupportedLength
return nil, errorset.ErrUnsupportedLength
}

out = append(out, buf...)
Expand Down Expand Up @@ -241,7 +243,7 @@ func (cc *LengthFieldBasedFrameCodec) Decode(c Conn) ([]byte, error) {
if cc.decoderConfig.LengthFieldOffset > 0 { // discard header(offset)
header, err = in.readN(cc.decoderConfig.LengthFieldOffset)
if err != nil {
return nil, errUnexpectedEOF
return nil, errorset.ErrUnexpectedEOF
}
}

Expand All @@ -254,7 +256,7 @@ func (cc *LengthFieldBasedFrameCodec) Decode(c Conn) ([]byte, error) {
msgLength := int(frameLength) + cc.decoderConfig.LengthAdjustment
msg, err := in.readN(msgLength)
if err != nil {
return nil, errUnexpectedEOF
return nil, errorset.ErrUnexpectedEOF
}

fullMessage := make([]byte, len(header)+len(lenBuf)+msgLength)
Expand All @@ -270,35 +272,35 @@ func (cc *LengthFieldBasedFrameCodec) getUnadjustedFrameLength(in *innerBuffer)
case 1:
b, err := in.readN(1)
if err != nil {
return nil, 0, errUnexpectedEOF
return nil, 0, errorset.ErrUnexpectedEOF
}
return b, uint64(b[0]), nil
case 2:
lenBuf, err := in.readN(2)
if err != nil {
return nil, 0, errUnexpectedEOF
return nil, 0, errorset.ErrUnexpectedEOF
}
return lenBuf, uint64(cc.decoderConfig.ByteOrder.Uint16(lenBuf)), nil
case 3:
lenBuf, err := in.readN(3)
if err != nil {
return nil, 0, errUnexpectedEOF
return nil, 0, errorset.ErrUnexpectedEOF
}
return lenBuf, readUint24(cc.decoderConfig.ByteOrder, lenBuf), nil
case 4:
lenBuf, err := in.readN(4)
if err != nil {
return nil, 0, errUnexpectedEOF
return nil, 0, errorset.ErrUnexpectedEOF
}
return lenBuf, uint64(cc.decoderConfig.ByteOrder.Uint32(lenBuf)), nil
case 8:
lenBuf, err := in.readN(8)
if err != nil {
return nil, 0, errUnexpectedEOF
return nil, 0, errorset.ErrUnexpectedEOF
}
return lenBuf, cc.decoderConfig.ByteOrder.Uint64(lenBuf), nil
default:
return nil, 0, errUnsupportedLength
return nil, 0, errorset.ErrUnsupportedLength
}
}

Expand Down
4 changes: 3 additions & 1 deletion codec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (
"encoding/binary"
"math/rand"
"testing"

"github.com/panjf2000/gnet/errors"
)

func TestLengthFieldBasedFrameCodecWith1(t *testing.T) {
Expand Down Expand Up @@ -227,7 +229,7 @@ func TestLengthFieldBasedFrameCodecWith8(t *testing.T) {

func TestFixedLengthFrameCodec_Encode(t *testing.T) {
codec := NewFixedLengthFrameCodec(8)
if data, err := codec.Encode(nil, make([]byte, 15)); data != nil || err != errInvalidFixedLength {
if data, err := codec.Encode(nil, make([]byte, 15)); data != nil || err != errors.ErrInvalidFixedLength {
panic("should have a error of invalid fixed length")
}
}
Expand Down
39 changes: 22 additions & 17 deletions errors.go → errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,33 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

package gnet
package errors

import "errors"

var (
// ErrUnsupportedProtocol occurs when trying to use protocol that is not supported.
ErrUnsupportedProtocol = errors.New("unsupported protocol on this platform")
ErrUnsupportedProtocol = errors.New("only unix, tcp/tcp4/tcp6, udp/udp4/udp6 are supported")
// ErrUnsupportedTCPProtocol occurs when trying to use an unsupported TCP protocol.
ErrUnsupportedTCPProtocol = errors.New("only tcp/tcp4/tcp6 are supported")
// ErrUnsupportedUDPProtocol occurs when trying to use an unsupported UDP protocol.
ErrUnsupportedUDPProtocol = errors.New("only udp/udp4/udp6 are supported")
// ErrUnsupportedUDSProtocol occurs when trying to use an unsupported Unix protocol.
ErrUnsupportedUDSProtocol = errors.New("only unix is supported")
// ErrUnsupportedPlatform occurs when running gnet on an unsupported platform.
ErrUnsupportedPlatform = errors.New("unsupported platform in gnet")

// errServerShutdown occurs when server is closing.
errServerShutdown = errors.New("server is going to be shutdown")
// errInvalidFixedLength occurs when the output data have invalid fixed length.
errInvalidFixedLength = errors.New("invalid fixed length of bytes")
// errUnexpectedEOF occurs when no enough data to read by codec.
errUnexpectedEOF = errors.New("there is no enough data")
// errDelimiterNotFound occurs when no such a delimiter is in input data.
errDelimiterNotFound = errors.New("there is no such a delimiter")
// errCRLFNotFound occurs when a CRLF is not found by codec.
errCRLFNotFound = errors.New("there is no CRLF")
// errUnsupportedLength occurs when unsupported lengthFieldLength is from input data.
errUnsupportedLength = errors.New("unsupported lengthFieldLength. (expected: 1, 2, 3, 4, or 8)")
// errTooLessLength occurs when adjusted frame length is less than zero.
errTooLessLength = errors.New("adjusted frame length is less than zero")
// ErrServerShutdown occurs when server is closing.
ErrServerShutdown = errors.New("server is going to be shutdown")
// ErrInvalidFixedLength occurs when the output data have invalid fixed length.
ErrInvalidFixedLength = errors.New("invalid fixed length of bytes")
// ErrUnexpectedEOF occurs when no enough data to read by codec.
ErrUnexpectedEOF = errors.New("there is no enough data")
// ErrDelimiterNotFound occurs when no such a delimiter is in input data.
ErrDelimiterNotFound = errors.New("there is no such a delimiter")
// ErrCRLFNotFound occurs when a CRLF is not found by codec.
ErrCRLFNotFound = errors.New("there is no CRLF")
// ErrUnsupportedLength occurs when unsupported lengthFieldLength is from input data.
ErrUnsupportedLength = errors.New("unsupported lengthFieldLength. (expected: 1, 2, 3, 4, or 8)")
// ErrTooLessLength occurs when adjusted frame length is less than zero.
ErrTooLessLength = errors.New("adjusted frame length is less than zero")
)
13 changes: 7 additions & 6 deletions eventloop_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"os"
"time"

"github.com/panjf2000/gnet/errors"
"github.com/panjf2000/gnet/internal/netpoll"
"golang.org/x/sys/unix"
)
Expand Down Expand Up @@ -65,7 +66,7 @@ func (el *eventloop) loopRun() {
go el.loopTicker()
}
switch err := el.poller.Polling(el.handleEvent); err {
case errServerShutdown:
case errors.ErrServerShutdown:
el.svr.logger.Infof("Event-loop(%d) is exiting normally on the signal error: %v", el.idx, err)
default:
el.svr.logger.Fatalf("Event-loop(%d) is exiting due to an unexpected error: %v", el.idx, err)
Expand Down Expand Up @@ -142,7 +143,7 @@ func (el *eventloop) loopRead(c *conn) error {
case Close:
return el.loopCloseConn(c, nil)
case Shutdown:
return errServerShutdown
return errors.ErrServerShutdown
}
if !c.opened {
return nil
Expand Down Expand Up @@ -192,7 +193,7 @@ func (el *eventloop) loopCloseConn(c *conn, err error) error {
delete(el.connections, c.fd)
el.calibrateCallback(el, -1)
if el.eventHandler.OnClosed(c, err) == Shutdown {
return errServerShutdown
return errors.ErrServerShutdown
}
c.releaseTCP()
} else {
Expand Down Expand Up @@ -232,7 +233,7 @@ func (el *eventloop) loopTicker() {
switch action {
case None:
case Shutdown:
err = errServerShutdown
err = errors.ErrServerShutdown
}
return
})
Expand All @@ -255,7 +256,7 @@ func (el *eventloop) handleAction(c *conn, action Action) error {
case Close:
return el.loopCloseConn(c, nil)
case Shutdown:
return errServerShutdown
return errors.ErrServerShutdown
default:
return nil
}
Expand All @@ -277,7 +278,7 @@ func (el *eventloop) loopReadUDP(fd int) error {
_ = c.sendTo(out)
}
if action == Shutdown {
return errServerShutdown
return errors.ErrServerShutdown
}
c.releaseUDP()
return nil
Expand Down
11 changes: 6 additions & 5 deletions eventloop_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"net"
"time"

"github.com/panjf2000/gnet/errors"
"github.com/panjf2000/gnet/pool/bytebuffer"
)

Expand Down Expand Up @@ -113,7 +114,7 @@ func (el *eventloop) loopRead(ti *tcpIn) (err error) {
case Close:
return el.loopCloseConn(c)
case Shutdown:
return errServerShutdown
return errors.ErrServerShutdown
}
if err != nil {
return el.loopError(c, err)
Expand Down Expand Up @@ -160,7 +161,7 @@ func (el *eventloop) loopTicker() {
el.svr.ticktock <- delay
switch action {
case Shutdown:
err = errServerShutdown
err = errors.ErrServerShutdown
}
return
}
Expand All @@ -178,7 +179,7 @@ func (el *eventloop) loopError(c *stdConn, err error) (e error) {
el.calibrateCallback(el, -1)
switch el.eventHandler.OnClosed(c, err) {
case Shutdown:
return errServerShutdown
return errors.ErrServerShutdown
}
c.releaseTCP()
} else {
Expand Down Expand Up @@ -206,7 +207,7 @@ func (el *eventloop) handleAction(c *stdConn, action Action) error {
case Close:
return el.loopCloseConn(c)
case Shutdown:
return errServerShutdown
return errors.ErrServerShutdown
default:
return nil
}
Expand All @@ -220,7 +221,7 @@ func (el *eventloop) loopReadUDP(c *stdConn) error {
}
switch action {
case Shutdown:
return errServerShutdown
return errors.ErrServerShutdown
}
c.releaseUDP()
return nil
Expand Down
3 changes: 2 additions & 1 deletion gnet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"testing"
"time"

"github.com/panjf2000/gnet/errors"
"github.com/panjf2000/gnet/pool/bytebuffer"
"github.com/panjf2000/gnet/pool/goroutine"
"github.com/valyala/bytebufferpool"
Expand Down Expand Up @@ -575,7 +576,7 @@ func startClient(network, addr string, multicore, async bool) {
}

func must(err error) {
if err != nil && err != ErrUnsupportedProtocol {
if err != nil && err != errors.ErrUnsupportedProtocol {
panic(err)
}
}
Expand Down
3 changes: 0 additions & 3 deletions internal/reuseport/reuseport.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,9 @@
package reuseport

import (
"errors"
"net"
)

var errUnsupportedProtocol = errors.New("only unix, tcp/tcp4/tcp6, udp/udp4/udp6 are supported")

// TCPSocket calls tcpReusablePort.
func TCPSocket(proto, addr string, reusePort bool) (int, net.Addr, error) {
return tcpReusablePort(proto, addr, reusePort)
Expand Down
11 changes: 4 additions & 7 deletions internal/reuseport/tcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,14 @@
package reuseport

import (
"errors"
"net"
"os"

"github.com/panjf2000/gnet/errors"
"golang.org/x/sys/unix"
)

var (
listenerBacklogMaxSize = maxListenerBacklog()
errUnsupportedTCPProtocol = errors.New("only tcp/tcp4/tcp6 are supported")
)
var listenerBacklogMaxSize = maxListenerBacklog()

func getTCPSockaddr(proto, addr string) (sa unix.Sockaddr, family int, tcpAddr *net.TCPAddr, err error) {
var tcpVersion string
Expand Down Expand Up @@ -83,7 +80,7 @@ func getTCPSockaddr(proto, addr string) (sa unix.Sockaddr, family int, tcpAddr *

sa, family = sa6, unix.AF_INET6
default:
err = errUnsupportedProtocol
err = errors.ErrUnsupportedProtocol
}

return
Expand All @@ -107,7 +104,7 @@ func determineTCPProto(proto string, addr *net.TCPAddr) (string, error) {
return proto, nil
}

return "", errUnsupportedTCPProtocol
return "", errors.ErrUnsupportedTCPProtocol
}

// tcpReusablePort creates an endpoint for communication and returns a file descriptor that refers to that endpoint.
Expand Down
Loading

0 comments on commit 1c2f4b0

Please sign in to comment.