fix: standard conn return non nil err with data #829
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What type of PR is this?
fix
Check the PR title.
<type>(optional scope): <description>
.(Optional) Translate the PR title into Chinese.
修复标准库连接同时返回了数据和错误时没有正确处理数据的 bug
(Optional) More detail description for this PR(en: English/zh: Chinese).
en: When both data and an error are returned by a connection, prioritizing the data over the error is recommended, consistent with the
io.Reader
best practices.standard Conn
adds anerr
field to store the error that occurs whenconn.Read
returns n > 0 & err != nil.conn.Read
returns n > 0 & err != nil,fill
returns directly regardless of how much data has been filled. The upper layer determines whether the data is complete by using theConn.Len()
method. When callingfill(1)
, there is no need for judgment, becausefill
can only return 0, err.conn.read
infill
, check whether the lastfill
returned an error. If it did, directly return that error to the upper layer.fill
, the upper layer usesc.Len()
to determine whether it has filled enough data. If it has, return the data from this call. If it has not, return the data already read and the error together.zh(optional): 当连接同时返回了数据和错误时,优先处理数据而不是错误,和
io.Reader
推荐做法一致。具体改动如下:standard Conn
增加 err 字段,用于存储conn.Read
同时返回 n > 0 & err != nil 时的 errconn.Read
同时返回 n > 0 & err != nil 时,fill
无论填了多少数据都直接返回,上层通过Conn.Len()
方法判断数据是否填充完整;调用 fill(1) 时不需要判断,因为 fill 只可能 return 0, err。fill
调用Conn.Read
之前之前先判断上次fill
是否返回了 err,如果返回了,则直接将该 err 返回给上层。fill
之后使用c.Len()
判断是否填充了足量的数据,如果够,则返回这次的数据;如果不够,则把已经读的数据和 err 一并返回(Optional) Which issue(s) this PR fixes:
#827
(Optional) The PR that updates user documentation: