使用JSON-RPC进行数据编组时出现错误 – 我是不是太蠢了?

huangapple go评论76阅读模式
英文:

Go bug when marshalling data using JSON-RPC - am I being dumb?

问题

所以我正在使用类似以下代码的代码,并且无法使FooHolder中的RPC正常工作,其中它保存符合接口的对象。我可能漏掉了什么,但看起来应该可以工作-使用reflect包时,它正确识别类型,但在函数返回时出现了问题...

package main

import (
	"log"
	"net"
	"net/rpc"
	"net/rpc/jsonrpc"
	"reflect"
)

type FooInterface interface {
	DoTheBartman(in *string, out *string) error
	DoThis(in *[]string, out *[]string) error
	NoProblems(in *int, out *int) error
}

type Foo struct {
	wibble string
}

func (f *Foo) DoTheBartman(in *string, out *string) error {
	log.Println("do the bartman")
	*out = "^^[ " + *in + "]^^"
	return nil
}

func (f *Foo) DoThis(in *[]string, out *[]string) error {
	log.Println("Doing this")
	log.Println("Type in - ", reflect.TypeOf(in))
	log.Println("Type *in - ", reflect.TypeOf(*in))
	log.Println("Type out - ", reflect.TypeOf(out))
	log.Println("Type *out - ", reflect.TypeOf(*out))

	*out = append(*in, "Hello")

	return nil
}

type FooHolder struct {
	TheFoos []FooInterface
}

func (fh *FooHolder) GetFoos(in *[]Foo, out *[]Foo) error {
	log.Println("Getting Foos...")
	log.Println("Type in - ", reflect.TypeOf(in))
	log.Println("Type *in - ", reflect.TypeOf(*in))
	log.Println("Type out - ", reflect.TypeOf(out))
	log.Println("Type *out - ", reflect.TypeOf(*out))

	return nil // This fails but with a nil... what's up with that.
}

func (f *Foo) NoProblems(in *int, out *int) error {
	log.Println("No Problems doing maths")
	log.Println("Type in - ", reflect.TypeOf(in))
	log.Println("Type *in - ", reflect.TypeOf(*in))
	log.Println("Type out - ", reflect.TypeOf(out))
	log.Println("Type *out - ", reflect.TypeOf(*out))

	*out = 42 - (*in)
	return nil
}

// This all works just fine
func startServer(f FooInterface) {
	server := rpc.NewServer()
	server.Register(f)

	l, e := net.Listen("tcp", ":8222")
	if e != nil {
		log.Fatal("listen error:", e)
	}

	for {
		conn, err := l.Accept()
		if err != nil {
			log.Fatal(err)
		}
		log.Println("Incoming!")

		go server.ServeCodec(jsonrpc.NewServerCodec(conn))
	}
}

// It starts to go wrong here...
func startFooHolderServer(f *FooHolder) {
	server := rpc.NewServer()
	server.Register(f)

	l, e := net.Listen("tcp", ":8222")
	if e != nil {
		log.Fatal("listen error:", e)
	}

	for {
		conn, err := l.Accept()
		if err != nil {
			log.Fatal(err)
		}
		log.Println("Incoming!")

		go server.ServeCodec(jsonrpc.NewServerCodec(conn))
	}
}

func main() {

	foo1 := &Foo{}

	fooHolder := &FooHolder{}

	fooHolder.TheFoos = append(fooHolder.TheFoos, foo1)

	// go startServer(foo1)
	go startFooHolderServer(fooHolder)

	conn, err := net.Dial("tcp", "localhost:8222")

	if err != nil {
		panic(err)
	}
	defer conn.Close()

	c := jsonrpc.NewClient(conn)

	var foo []Foo
	log.Println("Type - ", reflect.TypeOf(foo))
	log.Println("Type - ", reflect.TypeOf(&foo))
	err = c.Call("FooHolder.GetFoos", foo, &foo)
	if err != nil {
		log.Fatal("RPC error:", err)
	}

	// No Problems?
	var baz int
	err = c.Call("Foo.NoProblems", baz, &baz)
	if err != nil {
	    log.Fatal("RPC error:", err)
	}
	log.Println("Yay - baz is now", baz)

	stringArg := "Put stuff around me"
	err = c.Call("Foo.DoTheBartman", stringArg, &stringArg)
	if err != nil {
		log.Fatal("RPC error:", err)
	}
	log.Println("Yay - stringArg is now", stringArg)

	// Also No Problem
	bar := append([]string{"Hello"}, stringArg)
	log.Println("Type - ", reflect.TypeOf(bar))
	err = c.Call("Foo.DoThis", bar, &bar)
	if err != nil {
		log.Fatal("RPC error:", err)
	}
	log.Println("Yay - bar is now", bar)
}

输出:

rpc-demo $ go run demo.go 
2015/09/05 20:48:32 Incoming!
2015/09/05 20:48:32 Type -  []main.Foo
2015/09/05 20:48:32 Type -  *[]main.Foo
2015/09/05 20:48:32 Getting Foos...
2015/09/05 20:48:32 Type in -  *[]main.Foo
2015/09/05 20:48:32 Type *in -  []main.Foo
2015/09/05 20:48:32 Type out -  *[]main.Foo
2015/09/05 20:48:32 Type *out -  []main.Foo
2015/09/05 20:48:32 RPC error:invalid error <nil>
exit status 1
rpc-demo $
有人遇到过类似的问题吗?如果我声明一个新类型并将其传递,也没有帮助,我看不到任何前进的方法。
*顺便说一句,但可能相关的是,我将一个Go应用程序编译为存档,并将其构建为C程序,然后将接口更改为与Foo而不是Foo接口匹配,这样就可以工作了!奇怪。*
<details>
<summary>英文:</summary>
So I&#39;m working with code similar to the following and can&#39;t get the RPC to work in the FooHolder where it holds objects that meets an interface. I might be missing something but it looks like it should work - using the reflect package it correctly identifies the types but there&#39;s something wrong when the function returns....
package main
import (
&quot;log&quot;
&quot;net&quot;
&quot;net/rpc&quot;
&quot;net/rpc/jsonrpc&quot;
&quot;reflect&quot;
)
type FooInterface interface {
DoTheBartman(in *string, out *string) error
DoThis(in *[]string, out *[]string) error
NoProblems(in *int, out *int) error
}
type Foo struct {
wibble string
}
func (f *Foo) DoTheBartman(in *string, out *string) error {
log.Println(&quot;do the bartman&quot;)
*out = &quot;^^[ &quot; + *in + &quot;]^^&quot;
return nil
}
func (f *Foo) DoThis(in *[]string, out *[]string) error {
log.Println(&quot;Doing this&quot;)
log.Println(&quot;Type in - &quot;, reflect.TypeOf(in))
log.Println(&quot;Type *in - &quot;, reflect.TypeOf(*in))
log.Println(&quot;Type out - &quot;, reflect.TypeOf(out))
log.Println(&quot;Type *out - &quot;, reflect.TypeOf(*out))
*out = append(*in, &quot;Hello&quot;)
return nil
}
type FooHolder struct {
TheFoos []FooInterface
}
func (fh *FooHolder) GetFoos(in *[]Foo, out *[]Foo) error {
log.Println(&quot;Getting Foos...&quot;)
log.Println(&quot;Type in - &quot;, reflect.TypeOf(in))
log.Println(&quot;Type *in - &quot;, reflect.TypeOf(*in))
log.Println(&quot;Type out - &quot;, reflect.TypeOf(out))
log.Println(&quot;Type *out - &quot;, reflect.TypeOf(*out))
return nil // This fails but with a nil... what&#39;s up with that.
}
^^ This is the function that fails, not sure why? It returns a nil (and it&#39;s reported as a nil but errors out with a exit 1
func (f *Foo) NoProblems(in *int, out *int) error {
log.Println(&quot;No Problems doing maths&quot;)
log.Println(&quot;Type in - &quot;, reflect.TypeOf(in))
log.Println(&quot;Type *in - &quot;, reflect.TypeOf(*in))
log.Println(&quot;Type out - &quot;, reflect.TypeOf(out))
log.Println(&quot;Type *out - &quot;, reflect.TypeOf(*out))
*out = 42 - (*in)
return nil
}
// This all works just fine
func startServer(f FooInterface) {
server := rpc.NewServer()
server.Register(f)
l, e := net.Listen(&quot;tcp&quot;, &quot;:8222&quot;)
if e != nil {
log.Fatal(&quot;listen error:&quot;, e)
}
for {
conn, err := l.Accept()
if err != nil {
log.Fatal(err)
}
log.Println(&quot;Incoming!&quot;)
go server.ServeCodec(jsonrpc.NewServerCodec(conn))
}
}
// It starts to go wrong here...
func startFooHolderServer(f *FooHolder) {
server := rpc.NewServer()
server.Register(f)
l, e := net.Listen(&quot;tcp&quot;, &quot;:8222&quot;)
if e != nil {
log.Fatal(&quot;listen error:&quot;, e)
}
for {
conn, err := l.Accept()
if err != nil {
log.Fatal(err)
}
log.Println(&quot;Incoming!&quot;)
go server.ServeCodec(jsonrpc.NewServerCodec(conn))
}
}
func main() {
foo1 := &amp;Foo{}
fooHolder := &amp;FooHolder{}
fooHolder.TheFoos = append(fooHolder.TheFoos, foo1)
// go startServer(foo1)
go startFooHolderServer(fooHolder)
conn, err := net.Dial(&quot;tcp&quot;, &quot;localhost:8222&quot;)
if err != nil {
panic(err)
}
defer conn.Close()
c := jsonrpc.NewClient(conn)
var foo []Foo
log.Println(&quot;Type - &quot;, reflect.TypeOf(foo))
log.Println(&quot;Type - &quot;, reflect.TypeOf(&amp;foo))
err = c.Call(&quot;FooHolder.GetFoos&quot;, foo, &amp;foo)
if err != nil {
log.Fatal(&quot;RPC error:&quot;, err)
}
// No Problems?
var baz int
err = c.Call(&quot;Foo.NoProblems&quot;, baz, &amp;baz)
if err != nil {
log.Fatal(&quot;RPC error:&quot;, err)
}
log.Println(&quot;Yay - baz is now&quot;, baz)
stringArg := &quot;Put stuff around me&quot;
err = c.Call(&quot;Foo.DoTheBartman&quot;, stringArg, &amp;stringArg)
if err != nil {
log.Fatal(&quot;RPC error:&quot;, err)
}
log.Println(&quot;Yay - stringArg is now&quot;, stringArg)
// Also No Problem
bar := append([]string{&quot;Hello&quot;}, stringArg)
log.Println(&quot;Type - &quot;, reflect.TypeOf(bar))
err = c.Call(&quot;Foo.DoThis&quot;, bar, &amp;bar)
if err != nil {
log.Fatal(&quot;RPC error:&quot;, err)
}
log.Println(&quot;Yay - bar is now&quot;, bar)
}
Output:
rpc-demo $ go run demo.go 
2015/09/05 20:48:32 Incoming!
2015/09/05 20:48:32 Type -  []main.Foo
2015/09/05 20:48:32 Type -  *[]main.Foo
2015/09/05 20:48:32 Getting Foos...
2015/09/05 20:48:32 Type in -  *[]main.Foo
2015/09/05 20:48:32 Type *in -  []main.Foo
2015/09/05 20:48:32 Type out -  *[]main.Foo
2015/09/05 20:48:32 Type *out -  []main.Foo
2015/09/05 20:48:32 RPC error:invalid error &lt;nil&gt;
exit status 1
rpc-demo $ 
Has anyone experienced similar - it doesn&#39;t help if I declare a new type and throw that around either and I&#39;m not seeing a way forward.
*Tangentially, but possibly related, I compiled a Go app to archive and built it into a C program before changing the interface to match a Foo rather than a Foo interface and that worked! Weird.*
</details>
# 答案1
**得分**: 1
这是引发错误的代码行:https://github.com/golang/go/blob/master/src/net/rpc/jsonrpc/client.go#L89
这是客户端代码,你可以看到它在`c.resp.Error != nil || c.resp.Result == nil`条件下被执行。
现在你没有返回任何错误,所以问题不在这里。
我认为问题是你没有设置任何结果。`GetFoos`函数将`out`指针保持为nil。将`out`设置为非nil值,应该可以解决问题。
<details>
<summary>英文:</summary>
Here is the line throwing that error : https://github.com/golang/go/blob/master/src/net/rpc/jsonrpc/client.go#L89
It is client-side, and you can see it is reached if `c.resp.Error != nil || c.resp.Result == nil`.
Now you don&#39;t return any error, so the problem is not here.
I believe the problem is that you do not set any result. `GetFoos` leaves the `out` pointer nil. Set `out` to a non-nil value and it should work.
</details>

huangapple
  • 本文由 发表于 2015年9月5日 17:00:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/32411270.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定