zrpc timeout & unit tests (#573)

* zrpc timeout & unit tests
This commit is contained in:
Kevin Wan
2021-03-19 18:41:26 +08:00
committed by GitHub
parent 3c6951577d
commit 4884a7b3c6
4 changed files with 119 additions and 3 deletions

View File

@@ -18,6 +18,27 @@ func TimeoutInterceptor(timeout time.Duration) grpc.UnaryClientInterceptor {
ctx, cancel := contextx.ShrinkDeadline(ctx, timeout)
defer cancel()
return invoker(ctx, method, req, reply, cc, opts...)
done := make(chan error)
panicChan := make(chan interface{}, 1)
go func() {
defer func() {
if p := recover(); p != nil {
panicChan <- p
}
}()
done <- invoker(ctx, method, req, reply, cc, opts...)
close(done)
}()
select {
case p := <-panicChan:
panic(p)
case err := <-done:
return err
case <-ctx.Done():
return ctx.Err()
}
}
}