use of io.grpc.stub.ClientCallStreamObserver in project pinpoint by naver.
the class HelloWorldStreamClient method greet.
/**
* Say hello to server.
*/
public void greet(final int callCount) throws InterruptedException {
final CountDownLatch done = new CountDownLatch(1);
ClientResponseObserver<io.grpc.examples.manualflowcontrol.HelloRequest, io.grpc.examples.manualflowcontrol.HelloReply> clientResponseObserver = new ClientResponseObserver<io.grpc.examples.manualflowcontrol.HelloRequest, io.grpc.examples.manualflowcontrol.HelloReply>() {
ClientCallStreamObserver<io.grpc.examples.manualflowcontrol.HelloRequest> requestStream;
@Override
public void beforeStart(final ClientCallStreamObserver<io.grpc.examples.manualflowcontrol.HelloRequest> requestStream) {
this.requestStream = requestStream;
// Set up manual flow control for the response stream. It feels backwards to configure the response
// stream's flow control using the request stream's observer, but this is the way it is.
requestStream.disableAutoInboundFlowControl();
// Set up a back-pressure-aware producer for the request stream. The onReadyHandler will be invoked
// when the consuming side has enough buffer space to receive more messages.
//
// Messages are serialized into a transport-specific transmit buffer. Depending on the size of this buffer,
// MANY messages may be buffered, however, they haven't yet been sent to the server. The server must call
// request() to pull a buffered message from the client.
//
// Note: the onReadyHandler's invocation is serialized on the same thread pool as the incoming
// StreamObserver'sonNext(), onError(), and onComplete() handlers. Blocking the onReadyHandler will prevent
// additional messages from being processed by the incoming StreamObserver. The onReadyHandler must return
// in a timely manor or else message processing throughput will suffer.
requestStream.setOnReadyHandler(new Runnable() {
// An iterator is used so we can pause and resume iteration of the request data.
Iterator<String> iterator = names().iterator();
@Override
public void run() {
int count = 0;
// Start generating values from where we left off on a non-gRPC thread.
while (requestStream.isReady()) {
if (iterator.hasNext() && callCount > count) {
// Send more messages if there are more messages to send.
String name = iterator.next();
logger.info("--> " + name);
io.grpc.examples.manualflowcontrol.HelloRequest request = io.grpc.examples.manualflowcontrol.HelloRequest.newBuilder().setName(name).build();
requestStream.onNext(request);
count++;
} else {
// Signal completion if there is nothing left to send.
requestStream.onCompleted();
}
}
}
});
}
@Override
public void onNext(io.grpc.examples.manualflowcontrol.HelloReply value) {
logger.info("<-- " + value.getMessage());
// Signal the sender to send one message.
requestStream.request(1);
}
@Override
public void onError(Throwable t) {
t.printStackTrace();
done.countDown();
}
@Override
public void onCompleted() {
logger.info("All Done");
done.countDown();
}
};
// Note: clientResponseObserver is handling both request and response stream processing.
stub.sayHelloStreaming(clientResponseObserver);
done.await();
}
use of io.grpc.stub.ClientCallStreamObserver in project alluxio by Alluxio.
the class BlockInStreamTest method before.
@Before
public void before() throws Exception {
BlockWorkerClient workerClient = Mockito.mock(BlockWorkerClient.class);
ClientCallStreamObserver requestObserver = Mockito.mock(ClientCallStreamObserver.class);
when(requestObserver.isReady()).thenReturn(true);
when(workerClient.openLocalBlock(any(StreamObserver.class))).thenAnswer(new Answer() {
public Object answer(InvocationOnMock invocation) {
mResponseObserver = invocation.getArgument(0, StreamObserver.class);
return requestObserver;
}
});
doAnswer(invocation -> {
mResponseObserver.onNext(OpenLocalBlockResponse.newBuilder().setPath("/tmp").build());
mResponseObserver.onCompleted();
return null;
}).when(requestObserver).onNext(any(OpenLocalBlockRequest.class));
mMockContext = Mockito.mock(FileSystemContext.class);
when(mMockContext.acquireBlockWorkerClient(ArgumentMatchers.any(WorkerNetAddress.class))).thenReturn(new NoopClosableResource<>(workerClient));
when(mMockContext.getClientContext()).thenReturn(ClientContext.create(mConf));
when(mMockContext.getClusterConf()).thenReturn(mConf);
mInfo = new BlockInfo().setBlockId(1);
mOptions = new InStreamOptions(new URIStatus(new FileInfo().setBlockIds(Collections.singletonList(1L))), mConf);
}
use of io.grpc.stub.ClientCallStreamObserver in project grpc-java by grpc.
the class AbstractInteropTest method clientCompressedStreaming.
/**
* Tests client per-message compression for streaming calls. The Java API does not support
* inspecting a message's compression level, so this is primarily intended to run against a gRPC
* C++ server.
*/
public void clientCompressedStreaming(boolean probe) throws Exception {
final StreamingInputCallRequest expectCompressedRequest = StreamingInputCallRequest.newBuilder().setExpectCompressed(BoolValue.newBuilder().setValue(true)).setPayload(Payload.newBuilder().setBody(ByteString.copyFrom(new byte[27182]))).build();
final StreamingInputCallRequest expectUncompressedRequest = StreamingInputCallRequest.newBuilder().setExpectCompressed(BoolValue.newBuilder().setValue(false)).setPayload(Payload.newBuilder().setBody(ByteString.copyFrom(new byte[45904]))).build();
final StreamingInputCallResponse goldenResponse = StreamingInputCallResponse.newBuilder().setAggregatedPayloadSize(73086).build();
StreamRecorder<StreamingInputCallResponse> responseObserver = StreamRecorder.create();
StreamObserver<StreamingInputCallRequest> requestObserver = asyncStub.streamingInputCall(responseObserver);
if (probe) {
// Send a non-compressed message with expectCompress=true. Servers supporting this test case
// should return INVALID_ARGUMENT.
requestObserver.onNext(expectCompressedRequest);
responseObserver.awaitCompletion(operationTimeoutMillis(), TimeUnit.MILLISECONDS);
Throwable e = responseObserver.getError();
assertNotNull("expected INVALID_ARGUMENT", e);
assertEquals(Status.INVALID_ARGUMENT.getCode(), Status.fromThrowable(e).getCode());
}
// Start a new stream
responseObserver = StreamRecorder.create();
@SuppressWarnings("unchecked") ClientCallStreamObserver<StreamingInputCallRequest> clientCallStreamObserver = (ClientCallStreamObserver) asyncStub.withCompression("gzip").streamingInputCall(responseObserver);
clientCallStreamObserver.setMessageCompression(true);
clientCallStreamObserver.onNext(expectCompressedRequest);
clientCallStreamObserver.setMessageCompression(false);
clientCallStreamObserver.onNext(expectUncompressedRequest);
clientCallStreamObserver.onCompleted();
responseObserver.awaitCompletion();
assertSuccess(responseObserver);
assertEquals(goldenResponse, responseObserver.firstValue().get());
}
use of io.grpc.stub.ClientCallStreamObserver in project pinpoint by naver.
the class SpanClientMock method newSpanStream.
private StreamObserver<PSpanMessage> newSpanStream() {
System.out.println("### ");
System.out.println("NEW SpanStream");
System.out.println("###");
StreamId spanId = StreamId.newStreamId("SpanStream");
StreamEventListener<PSpanMessage> listener = new StreamEventListener<PSpanMessage>() {
@Override
public void start(ClientCallStreamObserver<PSpanMessage> requestStream) {
spanStreamReconnector.reset();
}
@Override
public void onError(Throwable t) {
spanStreamReconnector.reconnect();
}
@Override
public void onCompleted() {
spanStreamReconnector.reconnect();
}
};
ResponseStreamObserver<PSpanMessage, Empty> responseStreamObserver = new ResponseStreamObserver<PSpanMessage, Empty>(listener);
return spanStub.sendSpan(responseStreamObserver);
}
use of io.grpc.stub.ClientCallStreamObserver in project pinpoint by naver.
the class DefaultStreamTask method start.
@Override
public void start() {
this.latch = new CountDownLatch(1);
StreamJob<ReqT> job = new StreamJob<ReqT>() {
@Override
public Future<?> start(final ClientCallStreamObserver<ReqT> requestStream) {
Runnable runnable = DefaultStreamTask.this.newRunnable(requestStream, latch);
StreamExecutor<ReqT> streamExecutor = streamExecutorFactory.newStreamExecutor();
return streamExecutor.execute(runnable);
}
@Override
public String toString() {
return streamId.toString();
}
};
this.stream = clientStreamingService.newStream(job);
}
Aggregations