Skip to content

Instantly share code, notes, and snippets.

@spikebike
Last active May 19, 2017 06:49
Show Gist options
  • Save spikebike/7f26cba6db345ab69c497415ce1fc8e9 to your computer and use it in GitHub Desktop.
Save spikebike/7f26cba6db345ab69c497415ce1fc8e9 to your computer and use it in GitHub Desktop.
Wow, everytime I think I'm done. I find another hurdle.
Client and server are identical for my uses. So both client and server
should track the public key of their peers. So Server/Client handshake
can track who I'm talking to, last time I saw them, IP address, and public key.
Now when an RPC call is made I want to use UnaryServerInterceptor and
StreamServerInterceptor to allow every call to check if the Peer has
been tracked, if not look up the public key and set the PeerID.
peerID, ok := ctx.Value(peerIDKey).(int)
if (ok == false ) {
pubKey=GetPubKey(...)
peerId=getClientOrAdd((pubKey)
}
<answer RPC knowing which peer I'm talking to>
I nice example of using context for this kind of metadata is:
https://blog.golang.org/context
In particular:
https://blog.golang.org/context/userip/userip.go
Which has:
func NewContext(ctx context.Context, userIP net.IP) context.Context {
return context.WithValue(ctx, userIPKey, userIP)
}
Problem is, my RPC calls trigger a ctx being passed to the Interceptor, but
then I can't change the ctx. So how do I make updates to the ctx?
My attempt is at https://github.com/spikebike/grpc-go/blob/ssh-style-auth/examples/route_guide/server/server.go,
but complains:
go run server/server.go
server/server.go:246: cannot assign to ctx.Value(0).(int)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment