Skip to content

Instantly share code, notes, and snippets.

@buchgr
Created April 23, 2015 19:45
Show Gist options
  • Save buchgr/b11f5e59c7444f5b8032 to your computer and use it in GitHub Desktop.
Save buchgr/b11f5e59c7444f5b8032 to your computer and use it in GitHub Desktop.
// Basically I want to initiate the stream from the client and then whenever I get a message from the server
// I want to respond immediately over the same stream.
private Future<Histogram> doStreamingCalls(Channel channel, final SimpleRequest request,
final long endTime) {
final TestServiceStub stub = TestServiceGrpc.newStub(channel);
final Histogram histogram = new Histogram(HISTOGRAM_MAX_VALUE, HISTOGRAM_PRECISION);
final HistogramFuture future = new HistogramFuture(histogram);
final StreamObserver<SimpleRequest> requestObserver;
requestObserver = stub.streamingCall(new StreamObserver<SimpleResponse>() {
long lastCall = System.nanoTime();
@Override
public void onValue(SimpleResponse value) {
long now = System.nanoTime();
// Record the latencies in microseconds
histogram.recordValue((now - lastCall) / 1000);
lastCall = now;
if (endTime > now) {
requestObserver.onValue(request);
} else {
requestObserver.onCompleted();
}
}
@Override
public void onError(Throwable t) {
}
@Override
public void onCompleted() {
future.done();
}
});
requestObserver.onValue(request);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment