Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AsyncWrite需要等待数据被完全写出去 #328

Open
MoonShining opened this issue Feb 23, 2022 · 2 comments
Open

AsyncWrite需要等待数据被完全写出去 #328

MoonShining opened this issue Feb 23, 2022 · 2 comments
Assignees
Labels
enhancement New feature or request waiting for response waiting for the response from commenter

Comments

@MoonShining
Copy link

MoonShining commented Feb 23, 2022

Describe the bug

AsyncWrite接受一个callback, 看上去可以用来做这次AsyncWrite完成的通知。

但其实,需要写出去的数据在callback完成时可能还被buffer在内存里。

考虑流式写数据的场景,会循环调AsyncWrite,然后等AsyncWrite完成,伪代码如下

for {
   buf := make([]byte, 1024)
   reader.Read(buf)
   AsyncWrite(buf, callback)
  // 此时希望等待buf被全部写出去,然后再读下一块数据
}

如果往外写数据比较慢,就会导致内存一直增长, 需要有一种手段等待buf写真正完成

@MoonShining MoonShining added the bug Something isn't working label Feb 23, 2022
@panjf2000
Copy link
Owner

panjf2000 commented Feb 24, 2022

可以调用 Conn.Flush(),不过这个只能在 OnTraffic()/OnClose() 这些回调函数里调用,因为不是线程安全的。

@panjf2000 panjf2000 added waiting for response waiting for the response from commenter enhancement New feature or request and removed bug Something isn't working labels Feb 24, 2022
@JuniaWonter
Copy link

callback 为什么不把写错误也搞出来 ,func(cc gnet.Conn,err error) error像这样。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request waiting for response waiting for the response from commenter
Projects
None yet
Development

No branches or pull requests

3 participants