smallnest / rpcx

Best microservices framework in Go, like alibaba Dubbo, but with more features, Scale easily. Try it. Test it. If you feel it's better, use it! 𝐉𝐚𝐯𝐚有𝐝𝐮𝐛𝐛𝐨, 𝐆𝐨𝐥𝐚𝐧𝐠有𝐫𝐩𝐜𝐱! build for cloud!
https://rpcx.io
Other
8.08k stars 1.16k forks source link

编码方式为 protocol.SerializeNone时调用异常 #809

Closed shinxiang closed 1 year ago

shinxiang commented 1 year ago

我想实现的目的是, 不使用序列化,直接传递 []byte 数据流,代码如下: server.go

type Arith struct{}

func (t *Arith) Echo(ctx context.Context, args []byte, reply *[]byte) error {
    copy(*reply, args)
    log.Printf("%v, %v", args, reply)
    return nil
}

func main() {
    s := server.NewServer()
    s.RegisterName("Arith", new(Arith), "weight=3")
    s.Serve("tcp", "localhost:8972")
}

client.go

func main() {
    d, _ := client.NewPeer2PeerDiscovery("tcp@localhost:8972", "")
    option := client.DefaultOption
    option.SerializeType = protocol.SerializeNone
    xclient := client.NewXClient("Arith", client.Failtry, client.RandomSelect, d, option)
    defer xclient.Close()

    args := []byte{0x00, 0x01, 0x02}
    reply := make([]byte, 0)
    xclient.Call(context.Background(), "Echo", args, &reply)
    log.Printf("%v, %v", args, reply)
}

server.go代码调试时 Echo函数参数reply 为nil

smallnest commented 1 year ago

copy内建函数使用时,要求dst必须预分配足够的容量,比如: d2618ff47476f4541345b5e7af9c61c4

shinxiang commented 1 year ago

按你的建议,更正server.go代码

func (t *Arith) Echo(ctx context.Context, args []byte, reply *[]byte) error {
    *reply = make([]byte, 1024)
    n := copy(*reply, args)
    *reply = (*reply)[:n]
    return nil
}

依然报错,信息如下:

panic: reflect: reflect.Value.SetBytes using unaddressable value

goroutine 46 [running]:
reflect.flag.mustBeAssignableSlow(0x97)
        C:/Program Files/Go/src/reflect/value.go:262 +0xbd
reflect.flag.mustBeAssignable(0x97)
        C:/Program Files/Go/src/reflect/value.go:249 +0x4e
reflect.Value.SetBytes({0x1351a20, 0xc0000094e8, 0x97}, {0xc000374158, 0x3, 0x3})
        C:/Program Files/Go/src/reflect/value.go:2259 +0x3e
github.com/smallnest/rpcx/codec.ByteCodec.Decode({}, {0xc000374158, 0x3, 0x3}, {0x1351a20, 0xc0000094e8})
        C:/Users/Admin/go/pkg/mod/github.com/smallnest/rpcx@v1.8.11/codec/codec.go:42 +0x9b
github.com/smallnest/rpcx/client.(*Client).input(0xc00015a620)
        C:/Users/Admin/go/pkg/mod/github.com/smallnest/rpcx@v1.8.11/client/client.go:694 +0xbc4
created by github.com/smallnest/rpcx/client.(*Client).Connect
        C:/Users/Admin/go/pkg/mod/github.com/smallnest/rpcx@v1.8.11/client/connection.go:70 +0x94c
smallnest commented 1 year ago

看日志是你的client端代码调用的时候使用unaddressable值

smallnest commented 1 year ago

如果你是按照你最开始代码修改的,是没问题的,我测试过

shinxiang commented 1 year ago

问题已经解决了,谢谢!