我正在测试gRPC,其中包含一百万个项目的列表,并通过一个流发送这一百万个项目。
我在客户端上有这段代码:
在我的测试主机上:" Localhost",ipPort = 7777
var freshRates = [1.6,1.7,2.0]
var oldRates = [1.5,1.4,1.9]
var difference: [Double] = []
for (val1,val2) in zip(freshRates, oldRates){
difference.append(val2 - val1)
}
debugPrint(difference)
ManagedChannel comunicationChanel = ManagedChannelBuilder.forAddress(host, ipPort)
.enableFullStreamDecompression().compressorRegistry(CompressorRegistry.getDefaultInstance())
.decompressorRegistry(DecompressorRegistry.getDefaultInstance()).usePlaintext(true)
.maxInboundMessageSize(200888896).build();
ListMessageSRVStub asyncStub = ListMessageSRVGrpc.newStub(comunicationChanel);
但我有这个例外:
List<MessageValue> millionMessages = new ArrayList<MessageValue>();
for (long i = 0l; i < 1000000; i++) {
millionMessages.add(MessageValue.newBuilder().build());
}
long before = System.currentTimeMillis();
StreamObserver<MessageValue> requestObserver = asyncStub.recievetonm(responseObserverTonMessages);
long i = 0;
for (MessageValue messageValue : millionMessages) {
requestObserver.onNext(messageValue);
i++;
if (i % 50000 == 0) {
LOG.info("Sended: " + i);
}
}
requestObserver.onCompleted();
long total = System.currentTimeMillis() - before;
LOG.info("Time = " + total);
有什么方法可以解决发送数据的问题吗?
答案 0 :(得分:1)
你是否尝试过写慢速接收器:
public class GracefulWriteHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelActive(ChannelHandlerContext ctx) {
writeIfPossible(ctx.channel());
}
@Override
public void channelWritabilityChanged(ChannelHandlerContext ctx) {
writeIfPossible(ctx.channel());
}
private void writeIfPossible(Channel channel) {
while(needsToWrite && channel.isWritable()) {
channel.writeAndFlush(createMessage());
}
}
}
答案 1 :(得分:0)
我用自己的观察者来解决这个课程:
class OwnClientResponseObserver implements ClientResponseObserver<MessageValue, MessageValue> {
private ClientCallStreamObserver<MessageValue> requestStream;
private CountDownLatch countDownLatch;
Iterator<MessageValue> iterator;
public OwnClientResponseObserver(CountDownLatch countDownLatch, final Iterator<MessageValue> iterator) {
this.countDownLatch = countDownLatch;
this.iterator = iterator;
}
@Override
public void onNext(MessageValue value) {
LOG.info(value.toString());
}
@Override
public void onError(Throwable t) {
LOG.log(Level.SEVERE, "An savage error apears", t);
}
@Override
public void onCompleted() {
LOG.info("Finalized!!!");
}
@Override
public void beforeStart(ClientCallStreamObserver<MessageValue> requestStream) {
this.requestStream = requestStream;
this.requestStream.disableAutoInboundFlowControl();
this.requestStream.setOnReadyHandler(new Runnable() {
@Override
public void run() {
long i = 1L;
while (requestStream.isReady()) {
if (iterator.hasNext()) {
requestStream.onNext(iterator.next());
if (i % 1000 == 0) {
LOG.info("Checked in" + i);
}
i++;
} else {
requestStream.onCompleted();
countDownLatch.countDown();
}
}
}
});
}