Java-Grpc question about ServerCall.Listener thread-safe

50 views
Skip to first unread message

Joe Qiao

unread,
Jan 9, 2018, 10:16:36 PM1/9/18
to grpc.io
Hi,

I want to implement a server interceptor to collect how much time used in server side from message received to process complete. 
so i write a server interceptor which return a SimpleForwardingServerCallListener which have a long field. then i record the start time in the onMessage method, set it to the long field and calculate the total time used in onComplete by using this field value.
it's basically like this:

return new SimpleForwardingServerCallListener(...){
      private long start;

     public void onMesage(...){
       start = System.currentTimeMillis();
       .....
    }


    public void onComplete(...){
       log.info("time used:", System.currentTimeMillis() - start);
       .....
   }

}

is this implementation correct? is there any thread-safe issue here? 
Thanks!

Penn (Dapeng) Zhang

unread,
Jan 18, 2018, 1:20:06 PM1/18/18
to grpc.io
Assuming it is not a client streaming call. Your implementation looks fine, except the error/onCancel handling. Also do remember to call delegate().onMessage and delegate().onComplete at the end of each method. It is thread-safe.

Carl Mastrangelo

unread,
Jan 23, 2018, 7:40:17 PM1/23/18
to grpc.io
It would be a good idea to use System.nanoTime() instead, which is the clock designed for measure how long something took.   System.currentTimeMillis() is meant for telling the current time, which is allowed to drift. 


On Tuesday, January 9, 2018 at 7:16:36 PM UTC-8, Joe Qiao wrote:
Reply all
Reply to author
Forward
0 new messages