Browse Source

Upgrade gnet and improve http server (#5553)

* Upgrade gnet

* Update modules

* Remove useless code and make some optimizations

* Fix test failure

* Fixed ci failure

* Upgrade go to 1.14 in docker
Andy Pan 5 years ago
parent
commit
73b356da0f

+ 1 - 1
frameworks/Go/gnet/gnet.dockerfile

@@ -1,4 +1,4 @@
-FROM golang:1.13
+FROM golang:1.14
 
 
 ENV GO111MODULE on
 ENV GO111MODULE on
 
 

+ 1 - 1
frameworks/Go/gnet/src/go.mod

@@ -2,4 +2,4 @@ module gnet
 
 
 go 1.14
 go 1.14
 
 
-require github.com/panjf2000/gnet v1.0.0
+require github.com/panjf2000/gnet v1.0.1

+ 2 - 2
frameworks/Go/gnet/src/go.sum

@@ -5,8 +5,8 @@ github.com/libp2p/go-reuseport v0.0.1 h1:7PhkfH73VXfPJYKQ6JwS5I/eVcoyYi9IMNGc6FW
 github.com/libp2p/go-reuseport v0.0.1/go.mod h1:jn6RmB1ufnQwl0Q1f+YxAj8isJgDCQzaaxIFYDhcYEA=
 github.com/libp2p/go-reuseport v0.0.1/go.mod h1:jn6RmB1ufnQwl0Q1f+YxAj8isJgDCQzaaxIFYDhcYEA=
 github.com/panjf2000/ants/v2 v2.3.1 h1:9iOZHO5XlSO1Gs5K7x06uDFy8bkicWlhOKGh/TufAZg=
 github.com/panjf2000/ants/v2 v2.3.1 h1:9iOZHO5XlSO1Gs5K7x06uDFy8bkicWlhOKGh/TufAZg=
 github.com/panjf2000/ants/v2 v2.3.1/go.mod h1:LtwNaBX6OeF5qRtQlaeGndalVwJlS2ueur7uwoAHbPA=
 github.com/panjf2000/ants/v2 v2.3.1/go.mod h1:LtwNaBX6OeF5qRtQlaeGndalVwJlS2ueur7uwoAHbPA=
-github.com/panjf2000/gnet v1.0.0 h1:rv8BChMSQFD0+kGN61yI0M8QEIqwMaAMxQRbFTqX1B8=
-github.com/panjf2000/gnet v1.0.0/go.mod h1:Ux2Nc2pRFNk57YpDhHaZ9jaB4taAiqMBkcAWe/mWxmI=
+github.com/panjf2000/gnet v1.0.1 h1:IhaLkjtdtJax5N1uwRUnCIbkDV8bIeLTvReShNFw4AI=
+github.com/panjf2000/gnet v1.0.1/go.mod h1:Ux2Nc2pRFNk57YpDhHaZ9jaB4taAiqMBkcAWe/mWxmI=
 github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
 github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
 github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
 github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
 github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
 github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=

+ 24 - 38
frameworks/Go/gnet/src/main.go

@@ -24,21 +24,14 @@ type httpServer struct {
 	*gnet.EventServer
 	*gnet.EventServer
 }
 }
 
 
-var (
-	res         string
-	errMsg      = "Internal Server Error"
-	errMsgBytes = []byte(errMsg)
-)
+var res string
 
 
 type httpCodec struct {
 type httpCodec struct {
 	req request
 	req request
 }
 }
 
 
 func (hc *httpCodec) Encode(c gnet.Conn, buf []byte) (out []byte, err error) {
 func (hc *httpCodec) Encode(c gnet.Conn, buf []byte) (out []byte, err error) {
-	if c.Context() == nil {
-		return buf, nil
-	}
-	return appendResp(out, "500 Error", "", errMsg+"\n"), nil
+	return buf, nil
 }
 }
 
 
 func (hc *httpCodec) Decode(c gnet.Conn) (out []byte, err error) {
 func (hc *httpCodec) Decode(c gnet.Conn) (out []byte, err error) {
@@ -48,12 +41,7 @@ func (hc *httpCodec) Decode(c gnet.Conn) (out []byte, err error) {
 	// process the pipeline
 	// process the pipeline
 	var leftover []byte
 	var leftover []byte
 pipeline:
 pipeline:
-	leftover, err = parseReq(buf, &hc.req)
-	// bad thing happened
-	if err != nil {
-		c.SetContext(err)
-		return nil, err
-	} else if len(leftover) == len(buf) {
+	if leftover, _ = parseReq(buf, &hc.req); len(leftover) == len(buf) {
 		// request not ready, yet
 		// request not ready, yet
 		return
 		return
 	}
 	}
@@ -69,12 +57,6 @@ func (hs *httpServer) OnInitComplete(srv gnet.Server) (action gnet.Action) {
 }
 }
 
 
 func (hs *httpServer) React(frame []byte, c gnet.Conn) (out []byte, action gnet.Action) {
 func (hs *httpServer) React(frame []byte, c gnet.Conn) (out []byte, action gnet.Action) {
-	if c.Context() != nil {
-		// bad thing happened
-		out = errMsgBytes
-		action = gnet.Close
-		return
-	}
 	// handle the request
 	// handle the request
 	out = frame
 	out = frame
 	return
 	return
@@ -105,13 +87,13 @@ func main() {
 // appendHandle handles the incoming request and appends the response to
 // appendHandle handles the incoming request and appends the response to
 // the provided bytes, which is then returned to the caller.
 // the provided bytes, which is then returned to the caller.
 func appendHandle(b []byte, res string) []byte {
 func appendHandle(b []byte, res string) []byte {
-	return appendResp(b, "200 OK", "", res)
+	return appendResp(b, "200 OK", res)
 }
 }
 
 
 // appendResp will append a valid http response to the provide bytes.
 // appendResp will append a valid http response to the provide bytes.
 // The status param should be the code plus text such as "200 OK".
 // The status param should be the code plus text such as "200 OK".
 // The head parameter should be a series of lines ending with "\r\n" or empty.
 // The head parameter should be a series of lines ending with "\r\n" or empty.
-func appendResp(b []byte, status, head, body string) []byte {
+func appendResp(b []byte, status, body string) []byte {
 	b = append(b, "HTTP/1.1"...)
 	b = append(b, "HTTP/1.1"...)
 	b = append(b, ' ')
 	b = append(b, ' ')
 	b = append(b, status...)
 	b = append(b, status...)
@@ -124,11 +106,7 @@ func appendResp(b []byte, status, head, body string) []byte {
 	if len(body) > 0 {
 	if len(body) > 0 {
 		b = append(b, "Content-Length: "...)
 		b = append(b, "Content-Length: "...)
 		b = strconv.AppendInt(b, int64(len(body)), 10)
 		b = strconv.AppendInt(b, int64(len(body)), 10)
-		b = append(b, '\r', '\n')
-	}
-	b = append(b, head...)
-	b = append(b, '\r', '\n')
-	if len(body) > 0 {
+		b = append(b, '\r', '\n', '\r', '\n')
 		b = append(b, body...)
 		b = append(b, body...)
 	}
 	}
 	return b
 	return b
@@ -138,20 +116,28 @@ func b2s(b []byte) string {
 	return *(*string)(unsafe.Pointer(&b))
 	return *(*string)(unsafe.Pointer(&b))
 }
 }
 
 
+const (
+	contentLength = "Content-Length:"
+	contentLen    = len(contentLength)
+)
+
 // parseReq is a very simple http request parser. This operation
 // parseReq is a very simple http request parser. This operation
 // waits for the entire payload to be buffered before returning a
 // waits for the entire payload to be buffered before returning a
 // valid request.
 // valid request.
 func parseReq(data []byte, req *request) (leftover []byte, err error) {
 func parseReq(data []byte, req *request) (leftover []byte, err error) {
 	sdata := b2s(data)
 	sdata := b2s(data)
-	var i, s int
-	var head string
-	var clen int
-	var q = -1
+	dlen := len(sdata)
+	var (
+		i, s int
+		head string
+		clen int
+		q    = -1
+	)
 	// method, path, proto line
 	// method, path, proto line
-	for ; i < len(sdata); i++ {
+	for ; i < dlen; i++ {
 		if sdata[i] == ' ' {
 		if sdata[i] == ' ' {
 			req.method = sdata[s:i]
 			req.method = sdata[s:i]
-			for i, s = i+1, i+1; i < len(sdata); i++ {
+			for i, s = i+1, i+1; i < dlen; i++ {
 				if sdata[i] == '?' && q == -1 {
 				if sdata[i] == '?' && q == -1 {
 					q = i - s
 					q = i - s
 				} else if sdata[i] == ' ' {
 				} else if sdata[i] == ' ' {
@@ -161,7 +147,7 @@ func parseReq(data []byte, req *request) (leftover []byte, err error) {
 					} else {
 					} else {
 						req.path = sdata[s:i]
 						req.path = sdata[s:i]
 					}
 					}
-					for i, s = i+1, i+1; i < len(sdata); i++ {
+					for i, s = i+1, i+1; i < dlen; i++ {
 						if sdata[i] == '\n' && sdata[i-1] == '\r' {
 						if sdata[i] == '\n' && sdata[i-1] == '\r' {
 							req.proto = sdata[s:i]
 							req.proto = sdata[s:i]
 							i, s = i+1, i+1
 							i, s = i+1, i+1
@@ -178,7 +164,7 @@ func parseReq(data []byte, req *request) (leftover []byte, err error) {
 		return data, fmt.Errorf("malformed request")
 		return data, fmt.Errorf("malformed request")
 	}
 	}
 	head = sdata[:s]
 	head = sdata[:s]
-	for ; i < len(sdata); i++ {
+	for ; i < dlen; i++ {
 		if i > 1 && sdata[i] == '\n' && sdata[i-1] == '\r' {
 		if i > 1 && sdata[i] == '\n' && sdata[i-1] == '\r' {
 			line := sdata[s : i-1]
 			line := sdata[s : i-1]
 			s = i + 1
 			s = i + 1
@@ -194,8 +180,8 @@ func parseReq(data []byte, req *request) (leftover []byte, err error) {
 				}
 				}
 				return data[i:], nil
 				return data[i:], nil
 			}
 			}
-			if strings.HasPrefix(line, "Content-Length:") {
-				n, err := strconv.ParseInt(strings.TrimSpace(line[len("Content-Length:"):]), 10, 64)
+			if strings.HasPrefix(line, contentLength) {
+				n, err := strconv.ParseInt(strings.TrimSpace(line[contentLen:]), 10, 64)
 				if err == nil {
 				if err == nil {
 					clen = int(n)
 					clen = int(n)
 				}
 				}