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

希望添加调用 conn.AsyncWrite 后提供成功回调功能 #242

Closed
MashiroC opened this issue Sep 9, 2021 · 2 comments
Closed

希望添加调用 conn.AsyncWrite 后提供成功回调功能 #242

MashiroC opened this issue Sep 9, 2021 · 2 comments
Assignees
Labels
enhancement New feature or request proposal Proposal for this repo waiting for response waiting for the response from commenter

Comments

@MashiroC
Copy link

MashiroC commented Sep 9, 2021

我正在尝试使用 gnet 优化我的rpc框架,为了减少操作系统malloc的次数,我对于发送数据的[]byte进行了池化,但是我没有一个适合的时间来将从池中取出的[]byte放回。可以在这个仓库获取该用例:https://github.com/MashiroC/begonia-gnet-example

其中 common/conn.go

func BuildMessage(opcode byte, data []byte) (res []byte, err error) {
	// 计算 payload length 与 extend payload length
	var size []byte
	if len(data) >= extendLengthMax {
		err = fmt.Errorf("conn write error: payload len [%d] oversize", len(data))
		return
	} else if len(data) >= baseLenMax {
		tmp := twoBytesPool.Get().([]byte)
		binary.BigEndian.PutUint16(tmp, uint16(len(data)))
		size = []byte{255}
		size = append(size, tmp...)
		twoBytesPool.Put(tmp)
	} else {
		size = append(size, byte(len(data)))
	}

	// 组装opcode length data
	//res = make([]byte,0,65536)
	res = BytesPool.Get().([]byte)
	res = append(res, opcode)
	res = append(res, size...)
	res = append(res, data...)

	return
}

函数BuildMessage的作用是构建一个数据帧,在240行从池中取出了一个[]byte,避免了malloc,然后在后续我查看了gnet代码,AsyncWrite会将数据写进一个队列,由该队列来写数据。然而我找不到任何合适的将[]byte放回池内的时机。
如果提前放回,那么在一定并发的情况下,该函数内会取到一个已经压入队列但是还没有写入到网络的[]byte,然后会对将数据写入,覆盖之前的数据,会出现非常严重的bug。
如果将240行改为239行,进行一次malloc的话,是完全没有问题的。但是这样会造成极大的性能损耗,是难以接受的。

由此,我希望提供在写成功之后进行回调的功能,这样便可以将数据在该时机放回池内。如果认为提供该功能不合适,我也可以自己fork该仓库进行修改

@MashiroC MashiroC added enhancement New feature or request proposal Proposal for this repo labels Sep 9, 2021
@xscode-auto-reply
Copy link

Thanks for opening a new issue. The team has been notified and will review it as soon as possible.
For urgent issues and priority support, visit https://xscode.com/panjf2000/gnet

@panjf2000
Copy link
Owner

runtime.SetFinalizer() 可以解决。

@panjf2000 panjf2000 added the waiting for response waiting for the response from commenter label Sep 9, 2021
panjf2000 added a commit that referenced this issue Sep 27, 2021
0-haha pushed a commit to 0-haha/gnet that referenced this issue Jan 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request proposal Proposal for this repo waiting for response waiting for the response from commenter
Projects
None yet
Development

No branches or pull requests

2 participants