Skip to content

feat: 优化缓冲发送逻辑,减少缓冲发送小数据包的频繁系统调用#358

Merged
aceld merged 2 commits into
aceld:masterfrom
EquentR:feat-perf
Dec 27, 2024
Merged

feat: 优化缓冲发送逻辑,减少缓冲发送小数据包的频繁系统调用#358
aceld merged 2 commits into
aceld:masterfrom
EquentR:feat-perf

Conversation

@EquentR

@EquentR EquentR commented Dec 22, 2024

Copy link
Copy Markdown
Contributor

fix: #357

Comment thread znet/connection.go Outdated
if c.isClosed() == true {
return errors.New("connection closed when send msg")
}
c.bufMu.Lock()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

只会有一个线程往缓冲区写入,写到 bufio 应该不需要锁

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

我怕的是同时发生send和sendbuf两个方法调用,然后sendbuf刚好满了,触发了tcp的Write,会不会造成底层连接的脏写?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

还有就是bufio的flush时和bufio的write时,bufio底层没有锁控,看gpt的说法是最好加个锁

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

我怕的是同时发生send和sendbuf两个方法调用,然后sendbuf刚好满了,触发了tcp的Write,会不会造成底层连接的脏写?

可以在 sendbuf() 的时候调用一下 Available() 看一下容量如果写入的 size 大于容量可以手动调用一次 flush() 这里会等 send() 的锁结束,然后 bufio 确实底层没有锁,但是 flush()witre() 目前写法应该不会一起发生的,在一个 for{ select{} } 里,不过加锁也没大问题,只是我想尽量不要影响立即发送的 send()

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

好像确实在一个for select中不会发生这种问题,但是在发送前调用available手动flush感觉有点在重复干bufio.write()的活,我再看看,尽量减少锁的使用吧

@YanHeDoki

Copy link
Copy Markdown
Collaborator

@aceld sendbuf 这里我记得好像是因为什么问题改成这样子的,我觉得是可以优化的,你有空可以看下

@aceld

aceld commented Dec 27, 2024

Copy link
Copy Markdown
Owner

@aceld sendbuf 这里我记得好像是因为什么问题改成这样子的,我觉得是可以优化的,你有空可以看下

@YanHeDoki 有点记不住了,不过我看了下PR的代码,这里是在io层统一出口处,加了buffer,目的是放置多次走send。理论上应该问题不大,我看那个锁的部分也去掉了。

@aceld

aceld commented Dec 27, 2024

Copy link
Copy Markdown
Owner

@EquentR 感谢提交PR!

@aceld
aceld merged commit 9f12ff3 into aceld:master Dec 27, 2024
@EquentR

EquentR commented Jan 3, 2025

Copy link
Copy Markdown
Contributor Author

@EquentR 感谢提交PR!

感谢您的认可!😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SendToQueue这个方式的发送是否没有起到缓冲作用?

3 participants