Skip to content

Instantly share code, notes, and snippets.

@mostroverkhov
Created June 15, 2021 18:05
Show Gist options
  • Save mostroverkhov/63cb3a53804c008a72f9e6f07334298a to your computer and use it in GitHub Desktop.
Save mostroverkhov/63cb3a53804c008a72f9e6f07334298a to your computer and use it in GitHub Desktop.
public static void main(String[] args) throws Exception {
CompressorRegistry compressorRegistry = CompressorRegistry.newEmptyInstance();
compressorRegistry.register(Codec.Identity.NONE);
DecompressorRegistry decompressorRegistry =
DecompressorRegistry.emptyInstance().with(Codec.Identity.NONE, true);
ManagedChannel channel =
NettyChannelBuilder.forAddress("localhost", 9099)
.keepAliveTime(5, TimeUnit.SECONDS)
.keepAliveTimeout(30, TimeUnit.SECONDS)
.compressorRegistry(compressorRegistry)
.decompressorRegistry(decompressorRegistry)
.usePlaintext()
//.flowControlWindow(1_000_000)
.build();
TestServiceGrpc.TestServiceStub client = TestServiceGrpc.newStub(channel);
client.bidiStream(
new ClientResponseObserver<Request, Response>() {
ClientCallStreamObserver<Request> requestStream;
int windowSize = WINDOW_SIZE;
@Override
public void beforeStart(ClientCallStreamObserver<Request> requestStream) {
this.requestStream = requestStream;
Request request = Request.newBuilder().setTimestamp(System.nanoTime()).build();
requestStream.disableAutoRequestWithInitial(WINDOW_SIZE);
requestStream.setOnReadyHandler(
() -> {
while (requestStream.isReady()) {
requestStream.onNext(request);
}
});
}
@Override
public void onNext(Response value) {
if (--windowSize == WINDOW_SIZE / 2) {
windowSize += WINDOW_SIZE;
requestStream.request(WINDOW_SIZE);
}
}
@Override
public void onError(Throwable t) {
logger.error("Bidi stream terminated with error", t);
channel.shutdownNow();
}
@Override
public void onCompleted() {
logger.info("Bidi stream completed");
channel.shutdownNow();
}
});
channel.awaitTermination(365, TimeUnit.DAYS);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment