我们目前为我们的 RPC 使用节俭服务器。我们希望支持的调用之一是将读取文件的结果返回到直接的 ByteBuffer 中。THRIFT-883 说这不受支持(因为不支持 ByteBuffer.array()),所以想知道如果我们切换到 finagle 是否可以支持直接 ByteBuffers?
finagle 是否克服了这个限制,还是它也面临这个限制?
示例代码:
节约
struct BlockRequest {
1: i64 blockId,
2: i64 offset,
3: i64 len
}
struct BlockResponse {
1: i64 blockId
2: i64 offset
3: i64 len
4: binary data
}
service DataService {
BlockResponse requestBlock(1: BlockRequest request)
throws (1: BlockInfoException bie, 2: FileDoesNotExistException fdne)
}
爪哇
@Override
public BlockResponse requestBlock(BlockRequest request) throws BlockInfoException,
FileDoesNotExistException, TException {
final long blockId = request.blockId;
final long offset = request.offset;
final long len = request.len;
...
final RandomAccessFile file = new RandomAccessFile(filePath, "r");
long fileLength = file.length();
final long readLen;
if (len == -1) {
readLen = fileLength - offset;
} else {
readLen = len;
}
FileChannel channel = file.getChannel();
final MappedByteBuffer data = channel.map(FileChannel.MapMode.READ_ONLY, offset, readLen);
final BlockResponse response = new BlockResponse();
response.setBlockId(blockId);
response.setOffset(offset);
response.setLen(readLen);
response.setData(data);
return response
...