2017-06-07 46 views
1

RouteChat example這是一個雙向rpc,似乎是在write上緩衝消息。有沒有辦法強制寫入輸出流立即沒有緩衝?如何在grpc雙向rpc中發送消息,而不從grpc節點客戶端緩衝

+0

您在這裏面臨的問題是什麼?可寫流緩衝區是否填滿?在網絡連接飽和之前,您是否看到緩衝區被備份?你在看到消息和看到消息之間看到的延遲太多了嗎?或者是別的什麼?這是你用這個例子或你自己的代碼觀察到的問題嗎? – murgatroid99

+0

我看到寫入消息和在服務器上看到它之間的延遲。具體來說,所有寫在for循環[這裏](https://github.com/grpc/grpc/blob/v1.3.2/examples/node/static_codegen/route_guide/route_guide_client.js#L215)的消息都會發送到服務器時[call.end()](https://github.com/grpc/grpc/blob/v1.3.2/examples/node/static_codegen/route_guide/route_guide_client.js#L227)被調用,而不是被寫入immidiately在[call.write(notemsg)](https://github.com/grpc/grpc/blob/v1.3.2/examples/node/static_codegen/route_guide/route_guide_client.js#L225) –

回答

1

您看到此緩衝行爲的原因是因爲寫入實際上是異步完成的,所以服務器代碼需要定期在寫入之間進行其他異步操作,以確保在計算後儘快發送它們。您可以利用write的可選回調參數在發送最後一個消息後立即發送每條消息。該示例中的代碼可以改爲使用async.each

async.each(notes, function(note, next) { 
    console.log('Sending message "' + note.message + '" at ' + 
     note.location.latitude + ', ' + note.location.longitude); 
    var noteMsg = new messages.RouteNote(); 
    noteMsg.setMessage(note.message); 
    var location = new messages.Point(); 
    location.setLatitude(note.location.latitude); 
    location.setLongitude(note.location.longitude); 
    noteMsg.setLocation(location); 
    // Note the use of the next callback here 
    call.write(noteMsg, next); 
}, function() { 
    // End the stream after all messages have been sent 
    call.end(); 
});