add peer in context

This commit is contained in:
Asim Aslam 2019-06-27 14:53:01 +01:00
parent b4b76d452a
commit d8e998ad85

View File

@ -28,6 +28,7 @@ import (
"google.golang.org/grpc/credentials" "google.golang.org/grpc/credentials"
"google.golang.org/grpc/encoding" "google.golang.org/grpc/encoding"
"google.golang.org/grpc/metadata" "google.golang.org/grpc/metadata"
"google.golang.org/grpc/peer"
"google.golang.org/grpc/status" "google.golang.org/grpc/status"
) )
@ -203,6 +204,12 @@ func (g *grpcServer) handler(srv interface{}, stream grpc.ServerStream) error {
// create new context // create new context
ctx := meta.NewContext(stream.Context(), md) ctx := meta.NewContext(stream.Context(), md)
// get peer from context
if p, ok := peer.FromContext(stream.Context()); ok {
md["Remote"] = p.Addr.String()
ctx = peer.NewContext(ctx, p)
}
// set the timeout if we have it // set the timeout if we have it
if len(to) > 0 { if len(to) > 0 {
if n, err := strconv.ParseUint(to, 10, 64); err == nil { if n, err := strconv.ParseUint(to, 10, 64); err == nil {