0

I try to write a kv-raft server code in Go. (refer this article).

However I found there is problem if I want using context.Context in my RPC call parameter

// Init of my RPC server
kv := new(KVRaft)
rpcs := rpc.NewServer()
rpcs.Register(kv)
....


//My RPC call function
func (kv *KVRaft) Msg(args *context.Context, reply *MsgReply) error {
    log.Println("[MSG]", *args)
    return nil
}

Code could be compiled but when I trying to run

c, _ := rpc.Dial("tcp", ":1234")
var reply MsgReply{}

c.Call("KVRaft.Msg", someContext, &reply)

It will pop-up error as gob: type not registered for interface: context.emptyCtx

This code works well if I replace *context.Context to *string. I googled it could use gob.Register(), but I got compile failed on gob.Register(context.Context{})

invalid type for composite literal: "golang.org/x/net/context".Context

Any idea how could I pass context.Context as gob RPC call parameter?

[update]

I try to use gob.Register(context.Background()) but it will get empty context when receive RPC call.

[update] Currently my repo here, but still ongoing.

Evan Lin
  • 1,272
  • 1
  • 12
  • 25
  • 1
    `context.Context` is an interface, not a struct. You don't need to register an interface for gob to encode it. You should register the type(s) you're putting inside your context and encode those, putting them into a *new* context on the remote end. – elithrar Mar 10 '16 at 01:14
  • @elithrar Thanks, I try to use gob.Encode but it still got error on `gob: type not registered for interface: context.emptyCtx`. Any more direction how I trying to find a way to encode it? – Evan Lin Mar 10 '16 at 02:14
  • Hi @EvanLin! Did you get any explanation ever since? Thx – Big_Boulard Jul 26 '23 at 06:39
  • @elithrar could you provide a few lines to illustrate? – Big_Boulard Jul 26 '23 at 16:09

0 Answers0