Hi , I am new to gRPC.
I wonder how gRPC auto reconnect client and server (if server is down and restart ?)
This is my server code , I try to build a bi-directional stream
rpc biStream(stream Int) returns (stream Int) {}
The server side listens int stream from client and double the value and return to client.
This is server code :
@Override
public StreamObserver<Int> biStream(StreamObserver<Int> responseObserver) {
return new StreamObserver<Int>() {
@Override
public void onNext(Int anInt) {
try {
TimeUnit.NANOSECONDS.sleep(RandomUtils.nextInt(100 , 1000));
} catch (InterruptedException ignored) {
}
int value = anInt.getValue();
responseObserver.onNext(Int.newBuilder().setValue(value*2).build());
}
@Override
public void onError(Throwable throwable) {
}
@Override
public void onCompleted() {
responseObserver.onCompleted();
}
};
}
};
And this is client code :
public void biStreaming(int count) throws InterruptedException {
final CountDownLatch finishLatch = new CountDownLatch(1);
StreamObserver<Int> respondObserver = new StreamObserver<Int>() {
@Override
public void onNext(Int anInt) {
}
@Override
public void onError(Throwable throwable) {
logger.error(throwable.getMessage());
finishLatch.countDown();
}
@Override
public void onCompleted() {
finishLatch.countDown();
}
};
StreamObserver<Int> requestObserver = asyncStub.biStream(respondObserver);
for(int i=0 ; i <count ; i++) {
int rand = RandomUtils.nextInt(1, 101);
TimeUnit.SECONDS.sleep(RandomUtils.nextInt(1,10));
requestObserver.onNext(Int.newBuilder().setValue(rand).build());
}
requestObserver.onCompleted();
finishLatch.await(1, TimeUnit.MINUTES);
}
When running server , and starting the client , the client starts sending random int to server.
[main] INFO d.i.GrpcClient - sending 36 to server
[grpc-default-executor-1] INFO d.i.GrpcClient - get 72 from server
[main] INFO d.i.GrpcClient - sending 85 to server
[grpc-default-executor-1] INFO d.i.GrpcClient - get 170 from server
[main] INFO d.i.GrpcClient - sending 33 to server
[grpc-default-executor-1] INFO d.i.GrpcClient - get 66 from server
And I Ctrl-C the server side , the client shows :
[grpc-default-executor-1] ERROR d.i.GrpcClient - INTERNAL: Connection closed with unknown cause
[main] INFO d.i.GrpcClient - sending 26 to server
Then I restart the server , but it seems client cannot rebuild the stream.
[main] INFO d.i.GrpcClient - sending 34 to server
[main] INFO d.i.GrpcClient - sending 5 to server
[main] INFO d.i.GrpcClient - sending 62 to server
Anyway to recover a stream ?
Thanks.