3

In grpc-go, when implementing a service, the service interface defines methods contains only Context and Request. From the source of the Context, it is as simple as

type Context interface {
    Deadline() (deadline time.Time, ok bool)

    Done() <-chan struct{}

    Err() error

    Value(key interface{}) interface{}
}

So I wonder if it is possible to get some metadata (including remote IP address and other data) to maintain a session.

Thanks.

Vitaly Isaev
  • 5,392
  • 6
  • 45
  • 64
DANG Fan
  • 854
  • 11
  • 21

2 Answers2

3

There's nothing that gRPC provides (in any language) that would be particularly robust as a session system across requests.

The streaming mechanism is great when you need to maintain context on a single server for clients: the stream callback's stack can point to whatever session information you need.

If you need state across separate RPC's (or across machines) you'll need to add your own session layer. You could do this by creating some unique id that you attach to (say) a 'my-session-id' metadata element when sending requests.

Craig Tiller
  • 131
  • 2
  • Isn't there already a unique ID associated with a channel, no? If I need to enforce my clients to attach a uid metadata when creating a channel, I will do it. But if grpc already has such a uid built-in, it would be preferable. thanks – Sulliwane Oct 28 '16 at 06:29
1

It's possible to retrieve remote IP address through the use of stats.Handler (see especially this struct https://github.com/grpc/grpc-go/blob/v1.20.x/stats/stats.go#L89).

grpc.Metadata is commonly used to store arbitrary information about sessions.

Vitaly Isaev
  • 5,392
  • 6
  • 45
  • 64