Skip to content
This repository has been archived by the owner on Jun 23, 2022. It is now read-only.

rpc: minor refactoring on message_reader #252

Merged
merged 2 commits into from
May 16, 2019
Merged

Conversation

neverchanje
Copy link
Contributor

@neverchanje neverchanje commented May 16, 2019

主要补充了 message_reader 的注释和单测,这个类是一个错误的设计被放在一个正确的地方。
它本质上是用来从数据流中读入数据的,和 message 没有关系,却被叫做 message_reader。
它可以类比 sofa-pbrpc readbuffer 或者 brpc IOBuf,但实现非常粗糙,会产生较多内存拷贝,后续可以在此处进行优化。

rpc_write_stream 删除了一个构造函数,这个构造函数会造成内存泄漏,好在我们没有使用到它。

@@ -68,10 +60,19 @@ class message_reader
// discard read data
void truncate_read() { _buffer_occupied = 0; }

void consume_buffer(size_t sz)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个函数什么时候用啊?

Copy link
Contributor Author

@neverchanje neverchanje May 16, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://github.com/XiaoMi/rdsn/blob/master/src/core/tools/common/dsn_message_parser.cpp#L80

你看这行代码,reader 读完数据之后把读完的 buffer 释放掉,怎么释放的?

reader->_buffer = buf.range(msg_sz);
reader->_buffer_occupied -= msg_sz;

这段代码就是 repeat your code 的典型,consume_buffer 这个函数就是做的这个事情,后面重构可以把上面的代码用 consume_buffer 代替

reader->consume_buffer(msg_sz);

我这边后面的 PR 会用到这个函数。

Copy link
Contributor

@hycdong hycdong May 16, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

嗯,好的,blob的range函数支持传negative的参数(https://github.com/XiaoMi/rdsn/blob/3989922cd3a99c7ce357af8de2d504bd6a5dd1af/include/dsn/utility/blob.h#L115)
不知道有没有更通用的写法,另外加下注释吧

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

注释已加

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants