Skip to content

Commit

Permalink
fix: limit rpc package data size by user's config rather than DEFAULT…
Browse files Browse the repository at this point in the history
…_LEN

  * Dubbo recommand package data length not greater than 8MiB.
  * If package data length greater than 8MiB, generate warnning.
  * If package data lenght greater than user's config, generate exception.

Signed-off-by: stonex <1479765922@qq.com>
  • Loading branch information
stonex committed Apr 20, 2022
1 parent 605764e commit 0e1dad9
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 8 deletions.
5 changes: 3 additions & 2 deletions protocol/dubbo/hessian2/hessian_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,9 @@ func packRequest(service Service, header DubboHeader, req interface{}) ([]byte,
END:
byteArray = encoder.Buffer()
pkgLen = len(byteArray)
if pkgLen > int(DEFAULT_LEN) { // 8M
return nil, perrors.Errorf("Data length %d too large, max payload %d", pkgLen, DEFAULT_LEN)
if pkgLen > int(DEFAULT_LEN) { // recommand 8M
logger.Warnf("Data length %d too large, recommand max payload %d. "+
"Dubbo java can't handle the package which size greater than %d!!!", pkgLen, DEFAULT_LEN, DEFAULT_LEN)
}
// byteArray{body length}
binary.BigEndian.PutUint32(byteArray[12:], uint32(pkgLen-HEADER_LENGTH))
Expand Down
5 changes: 3 additions & 2 deletions protocol/dubbo/hessian2/hessian_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,9 @@ func packResponse(header DubboHeader, ret interface{}) ([]byte, error) {
byteArray = encoder.Buffer()
byteArray = hessian.EncNull(byteArray) // if not, "java client" will throw exception "unexpected end of file"
pkgLen := len(byteArray)
if pkgLen > int(DEFAULT_LEN) { // 8M
return nil, perrors.Errorf("Data length %d too large, max payload %d", pkgLen, DEFAULT_LEN)
if pkgLen > int(DEFAULT_LEN) { // recommand 8M
logger.Warnf("Data length %d too large, recommand max payload %d. "+
"Dubbo java can't handle the package which size greater than %d!!!", pkgLen, DEFAULT_LEN, DEFAULT_LEN)
}
// byteArray{body length}
binary.BigEndian.PutUint32(byteArray[12:], uint32(pkgLen-HEADER_LENGTH))
Expand Down
10 changes: 6 additions & 4 deletions protocol/dubbo/impl/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,9 @@ func packRequest(p DubboPackage, serializer Serializer) ([]byte, error) {
return nil, err
}
pkgLen = len(body)
if pkgLen > int(DEFAULT_LEN) { // 8M
return nil, perrors.Errorf("Data length %d too large, max payload %d", pkgLen, DEFAULT_LEN)
if pkgLen > int(DEFAULT_LEN) { // recommand 8M
logger.Warnf("Data length %d too large, recommand max payload %d. "+
"Dubbo java can't handle the package which size greater than %d!!!", pkgLen, DEFAULT_LEN, DEFAULT_LEN)
}
byteArray = append(byteArray, body...)
}
Expand Down Expand Up @@ -269,8 +270,9 @@ func packResponse(p DubboPackage, serializer Serializer) ([]byte, error) {
}

pkgLen := len(body)
if pkgLen > int(DEFAULT_LEN) { // 8M
return nil, perrors.Errorf("Data length %d too large, max payload %d", pkgLen, DEFAULT_LEN)
if pkgLen > int(DEFAULT_LEN) { // recommand 8M
logger.Warnf("Data length %d too large, recommand max payload %d. "+
"Dubbo java can't handle the package which size greater than %d!!!", pkgLen, DEFAULT_LEN, DEFAULT_LEN)
}
// byteArray{body length}
binary.BigEndian.PutUint32(byteArray[12:], uint32(pkgLen))
Expand Down
23 changes: 23 additions & 0 deletions remoting/getty/readwriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (

import (
"dubbo.apache.org/dubbo-go/v3/common/logger"
"dubbo.apache.org/dubbo-go/v3/protocol/dubbo/impl"
"dubbo.apache.org/dubbo-go/v3/remoting"
)

Expand Down Expand Up @@ -61,8 +62,14 @@ func (p *RpcClientPackageHandler) Read(ss getty.Session, data []byte) (interface
// Write send the data to server
func (p *RpcClientPackageHandler) Write(ss getty.Session, pkg interface{}) ([]byte, error) {
req, ok := pkg.(*remoting.Request)
maxBufLength := clientConf.GettySessionParam.MaxMsgLen + impl.HEADER_LENGTH
if ok {
buf, err := (p.client.codec).EncodeRequest(req)
bufLength := buf.Len()
if bufLength > maxBufLength {
logger.Errorf("Data length %d too large, max payload %d", bufLength-impl.HEADER_LENGTH, clientConf.GettySessionParam.MaxMsgLen)
return nil, perrors.Errorf("Data length %d too large, max payload %d", bufLength-impl.HEADER_LENGTH, clientConf.GettySessionParam.MaxMsgLen)
}
if err != nil {
logger.Warnf("binary.Write(req{%#v}) = err{%#v}", req, perrors.WithStack(err))
return nil, perrors.WithStack(err)
Expand All @@ -73,6 +80,11 @@ func (p *RpcClientPackageHandler) Write(ss getty.Session, pkg interface{}) ([]by
res, ok := pkg.(*remoting.Response)
if ok {
buf, err := (p.client.codec).EncodeResponse(res)
bufLength := buf.Len()
if bufLength > maxBufLength {
logger.Errorf("Data length %d too large, max payload %d", bufLength-impl.HEADER_LENGTH, clientConf.GettySessionParam.MaxMsgLen)
return nil, perrors.Errorf("Data length %d too large, max payload %d", bufLength-impl.HEADER_LENGTH, clientConf.GettySessionParam.MaxMsgLen)
}
if err != nil {
logger.Warnf("binary.Write(res{%#v}) = err{%#v}", req, perrors.WithStack(err))
return nil, perrors.WithStack(err)
Expand Down Expand Up @@ -112,8 +124,14 @@ func (p *RpcServerPackageHandler) Read(ss getty.Session, data []byte) (interface
// Write send the data to client
func (p *RpcServerPackageHandler) Write(ss getty.Session, pkg interface{}) ([]byte, error) {
res, ok := pkg.(*remoting.Response)
maxBufLength := srvConf.GettySessionParam.MaxMsgLen + impl.HEADER_LENGTH
if ok {
buf, err := (p.server.codec).EncodeResponse(res)
bufLength := buf.Len()
if bufLength > maxBufLength {
logger.Errorf("Data length %d too large, max payload %d", bufLength-impl.HEADER_LENGTH, srvConf.GettySessionParam.MaxMsgLen)
return nil, perrors.Errorf("Data length %d too large, max payload %d", bufLength-impl.HEADER_LENGTH, srvConf.GettySessionParam.MaxMsgLen)
}
if err != nil {
logger.Warnf("binary.Write(res{%#v}) = err{%#v}", res, perrors.WithStack(err))
return nil, perrors.WithStack(err)
Expand All @@ -124,6 +142,11 @@ func (p *RpcServerPackageHandler) Write(ss getty.Session, pkg interface{}) ([]by
req, ok := pkg.(*remoting.Request)
if ok {
buf, err := (p.server.codec).EncodeRequest(req)
bufLength := buf.Len()
if bufLength > maxBufLength {
logger.Errorf("Data length %d too large, max payload %d", bufLength-impl.HEADER_LENGTH, srvConf.GettySessionParam.MaxMsgLen)
return nil, perrors.Errorf("Data length %d too large, max payload %d", bufLength-impl.HEADER_LENGTH, srvConf.GettySessionParam.MaxMsgLen)
}
if err != nil {
logger.Warnf("binary.Write(req{%#v}) = err{%#v}", res, perrors.WithStack(err))
return nil, perrors.WithStack(err)
Expand Down

0 comments on commit 0e1dad9

Please sign in to comment.