grpc go : how to know in server side, when client closes the connection

5,202 views
Skip to first unread message

vim...@gmail.com

unread,
Oct 3, 2016, 2:13:47 AM10/3/16
to grpc.io
I am using grpc go

i have an rpc which looks roughly like this

196 service MyService {
197   // Operation 1
198   rpc Operation1(OperationRequest) returns (OperationResponse) {
199       option (google.api.http) = {
200         post: "/apiver/myser/oper1"
201         body: "*"
202     };
203   }


Client connects by using grpc.Dial() method

When a client connects, the server does some book keeping. when the client disconnects, the bookkeeping needs to be removed.

is there any callback that can be registered which can be used to know the session is getting closed.

Thanks,
Regards,
Vimal

Qi Zhao

unread,
Oct 3, 2016, 4:31:56 PM10/3/16
to vim...@gmail.com, grpc.io
More info is needed. How do you do per-connection book keeping on the server side in the first place? The only way I can think of to do per-connection book keeping on server side is to make a custom net.Listener. If you do this way, it is not hard to detect whether the connections accepted by this listener is closed by the client or not.

--
You received this message because you are subscribed to the Google Groups "grpc.io" group.
To unsubscribe from this group and stop receiving emails from it, send an email to grpc-io+unsubscribe@googlegroups.com.
To post to this group, send email to grp...@googlegroups.com.
Visit this group at https://groups.google.com/group/grpc-io.
To view this discussion on the web visit https://groups.google.com/d/msgid/grpc-io/3bfd5b3d-5468-4728-9e60-042e49552069%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Thanks,
-Qi

vim...@gmail.com

unread,
Oct 4, 2016, 10:56:37 AM10/4/16
to grpc.io, vim...@gmail.com

clients sets a "token":token  pair in the auth info when it starts a session with the server.  server obtains the key:value and saves it.
i want to be able to know in the server when the session/stream (not too sure of the grpc terminology) with the client is closed. so that the token can be removed, as it is applicable only for that session.

thanks,
Vimal


vim...@gmail.com

unread,
Oct 6, 2016, 5:32:17 AM10/6/16
to grpc.io, vim...@gmail.com

hi Qi,

is it supported? i tried using ctx.Done() for this purpose, but found that this channel close is limited to one operation invocation. so cannot use it.

Is there some channel i can listen to at the Transport level? 

I found below code in google.golang.org/grpc/server.go
will the print statement in line 420 be printed when one connection closes?

411 func (s *Server) serveStreams(st transport.ServerTransport) {
412     defer s.removeConn(st)
413     defer st.Close()
414     var wg sync.WaitGroup
415     st.HandleStreams(func(stream *transport.Stream) {
416         wg.Add(1)
417         go func() {
418             defer wg.Done()
419             s.handleStream(st, stream, s.traceInfo(st, stream))
420             fmt.Printf("grpc/server.go:Server.serverStream.$handleStream finished")
421         }()
422     })
423     wg.Wait()
424 }


can some notification be generated from this method.

497 func (s *Server) removeConn(c io.Closer) {
498     s.mu.Lock()
499     defer s.mu.Unlock()
500     if s.conns != nil {
501         delete(s.conns, c)
502         s.cv.Signal()
503     }
504 }



thanks,
Regards,
Vimal


vim...@gmail.com

unread,
Oct 7, 2016, 6:00:55 AM10/7/16
to grpc.io, vim...@gmail.com

i am able to locate the place where connection is removed. the print on line 504 is printed once per connection (irrespective of number of operations in it)


498 func (s *Server) removeConn(c io.Closer) {
499     s.mu.Lock()
500     defer s.mu.Unlock()
501     if s.conns != nil {
502         delete(s.conns, c)
503         c.Close()
504         fmt.Printf("grpc/server.go: Server.removeConn() finished \n")
505         s.cv.Signal()
506     }
507 }

line 503 also invokes "Close()" on the connection, it should invoke the below method:


144 func (ht *serverHandlerTransport) Close() error {
145     fmt.Printf("grpc/transport/handler_server.go:serverHandlerTransport.Close()")
146     ht.closeOnce.Do(ht.closeCloseChanOnce)
147     return nil
148 }
149
150 func (ht *serverHandlerTransport) closeCloseChanOnce() {
151     close(ht.closedCh)
152 }


but i  am not able to see this invocation. Am i checking the correct "Close()" method ?

moreover, what can grpc applications do to read from this channel ?

Thanks,
Vimal

vim...@gmail.com

unread,
Oct 7, 2016, 6:14:56 AM10/7/16
to grpc.io, vim...@gmail.com, zh...@google.com
hello Qi,

Can you have a look at this thread pls.

regards,
Vimal


On Tuesday, 4 October 2016 02:01:56 UTC+5:30, Qi Zhao wrote:
More info is needed. How do you do per-connection book keeping on the server side in the first place? The only way I can think of to do per-connection book keeping on server side is to make a custom net.Listener. If you do this way, it is not hard to detect whether the connections accepted by this listener is closed by the client or not.
On Sun, Oct 2, 2016 at 11:13 PM, <vim...@gmail.com> wrote:
I am using grpc go

i have an rpc which looks roughly like this

196 service MyService {
197   // Operation 1
198   rpc Operation1(OperationRequest) returns (OperationResponse) {
199       option (google.api.http) = {
200         post: "/apiver/myser/oper1"
201         body: "*"
202     };
203   }


Client connects by using grpc.Dial() method

When a client connects, the server does some book keeping. when the client disconnects, the bookkeeping needs to be removed.

is there any callback that can be registered which can be used to know the session is getting closed.

Thanks,
Regards,
Vimal

--
You received this message because you are subscribed to the Google Groups "grpc.io" group.
To unsubscribe from this group and stop receiving emails from it, send an email to grpc-io+u...@googlegroups.com.

To post to this group, send email to grp...@googlegroups.com.
Visit this group at https://groups.google.com/group/grpc-io.
To view this discussion on the web visit https://groups.google.com/d/msgid/grpc-io/3bfd5b3d-5468-4728-9e60-042e49552069%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Thanks,
-Qi

Qi Zhao

unread,
Oct 7, 2016, 1:31:52 PM10/7/16
to Vimal K, grpc.io
You checked the wrong transport file. The invoked code is in transport/http2_server.go. 

moreover, what can grpc applications do to read from this channel ?
I actually already provided a solution to you in my first reply. Does it work for you? 

Thanks,
Vimal

--
You received this message because you are subscribed to the Google Groups "grpc.io" group.
To unsubscribe from this group and stop receiving emails from it, send an email to grpc-io+unsubscribe@googlegroups.com.
To post to this group, send email to grp...@googlegroups.com.
Visit this group at https://groups.google.com/group/grpc-io.

For more options, visit https://groups.google.com/d/optout.



--
Thanks,
-Qi

vim...@gmail.com

unread,
Oct 11, 2016, 7:59:50 AM10/11/16
to grpc.io, vim...@gmail.com



moreover, what can grpc applications do to read from this channel ?
I actually already provided a solution to you in my first reply. Does it work for you? 

The solution you provided doesnt work for me. The key for book keeping is obtained from metadata.FromContext(ctx) which is set by grpc client. i can trap net.Listener.Accept but that doesnt tell me about this metadata.
So i need if grpc can tell when one connection is closed by client, and the associated context metadata, so that i can remove the entry

regards,
Vimal

Qi Zhao

unread,
Oct 11, 2016, 2:25:40 PM10/11/16
to Vimal K, grpc.io
On Tue, Oct 11, 2016 at 4:59 AM, <vim...@gmail.com> wrote:



moreover, what can grpc applications do to read from this channel ?
I actually already provided a solution to you in my first reply. Does it work for you? 

The solution you provided doesnt work for me. The key for book keeping is obtained from metadata.FromContext(ctx) which is set by grpc client.
This is actually problematic because one grpc.ClientConn may have multiple underlying network connections so that you may set the same key for different network connections and clearly the server has no way to differentiate connections via the metadata key. 
i can trap net.Listener.Accept but that doesnt tell me about this metadata.
So i need if grpc can tell when one connection is closed by client, and the associated context metadata, so that i can remove the entry

regards,
Vimal

--
You received this message because you are subscribed to the Google Groups "grpc.io" group.
To unsubscribe from this group and stop receiving emails from it, send an email to grpc-io+unsubscribe@googlegroups.com.
To post to this group, send email to grp...@googlegroups.com.
Visit this group at https://groups.google.com/group/grpc-io.

For more options, visit https://groups.google.com/d/optout.



--
Thanks,
-Qi
Reply all
Reply to author
Forward
0 new messages