Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: set the correct rpc response type when meet an error #102

Merged
merged 2 commits into from
Sep 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 23 additions & 16 deletions internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,33 +73,40 @@ func recoverPanic() {
}
}

func dispatchRPC(ty byte, in []byte, conn net.Conn) *flatbuffers.Builder {
func dispatchRPC(ty byte, in []byte, conn net.Conn) (*flatbuffers.Builder, byte) {
var err error
var bd *flatbuffers.Builder
hl, ok := typeHandlerMap[ty]
if !ok {
err = UnknownType{ty}
} else {
bd, err = hl(in, conn)
log.Warnf("unknown rpc type: %d", ty)
return generateErrorReport(UnknownType{ty}), util.RPCError
}

bd, err = hl(in, conn)
if err != nil {
bd = generateErrorReport(err)
} else {
bd = checkIfDataTooLarge(bd)
return generateErrorReport(err), util.RPCError
}

replaced, ok := checkIfDataTooLarge(bd)
if !ok {
return replaced, util.RPCError
}
return bd

return bd, ty
}

func checkIfDataTooLarge(bd *flatbuffers.Builder) *flatbuffers.Builder {
func checkIfDataTooLarge(bd *flatbuffers.Builder) (*flatbuffers.Builder, bool) {
out := bd.FinishedBytes()
size := len(out)
if size > util.MaxDataSize {
err := fmt.Errorf("the max length of data is %d but got %d", util.MaxDataSize, size)
util.PutBuilder(bd)
bd = generateErrorReport(err)
if size < util.MaxDataSize {
return bd, true
}
return bd

err := fmt.Errorf("the max length of data is %d but got %d", util.MaxDataSize, size)
util.PutBuilder(bd)
bd = generateErrorReport(err)

return bd, false
}

func handleConn(c net.Conn) {
Expand Down Expand Up @@ -129,11 +136,11 @@ func handleConn(c net.Conn) {
break
}

bd := dispatchRPC(ty, buf, c)
bd, respTy := dispatchRPC(ty, buf, c)
out := bd.FinishedBytes()
size := len(out)
binary.BigEndian.PutUint32(header, uint32(size))
header[0] = ty
header[0] = respTy

n, err = c.Write(header)
if err != nil {
Expand Down
19 changes: 18 additions & 1 deletion internal/server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
hrc "github.com/api7/ext-plugin-proto/go/A6/HTTPReqCall"
"github.com/stretchr/testify/assert"

"github.com/apache/apisix-go-plugin-runner/internal/plugin"
"github.com/apache/apisix-go-plugin-runner/internal/util"
)

Expand All @@ -52,10 +53,22 @@ func TestGetConfCacheTTL(t *testing.T) {
}

func TestDispatchRPC_UnknownType(t *testing.T) {
bd := dispatchRPC(126, []byte(""), nil)
bd, ty := dispatchRPC(126, []byte(""), nil)
err := UnknownType{126}
expectBd := ReportError(err)
assert.Equal(t, expectBd.FinishedBytes(), bd.FinishedBytes())
assert.Equal(t, ty, byte(util.RPCError))
}

func TestDispatchRPC_KnownType(t *testing.T) {
bd := util.GetBuilder()
hrc.ReqStart(bd)
hrc.ReqAddConfToken(bd, 1)
r := hrc.ReqEnd(bd)
bd.Finish(r)

_, ty := dispatchRPC(util.RPCHTTPReqCall, bd.FinishedBytes(), nil)
assert.Equal(t, ty, byte(util.RPCError))
}

func TestDispatchRPC_OutTooLarge(t *testing.T) {
Expand Down Expand Up @@ -117,3 +130,7 @@ func TestRun(t *testing.T) {
_, err = os.Stat(path)
assert.True(t, os.IsNotExist(err))
}

func init() {
plugin.InitConfCache(time.Second)
}