Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Session and remote IP address in grpc-go

Tags:

grpc

grpc-go

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.

like image 573
DANG Fan Avatar asked Dec 28 '25 18:12

DANG Fan


2 Answers

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.

like image 176
Craig Tiller Avatar answered Dec 30 '25 22:12

Craig Tiller


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.

like image 44
Vitaly Isaev Avatar answered Dec 30 '25 23:12

Vitaly Isaev