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

客户端发出主动关闭请求时无法触发onClose #78

Closed
cs-charles opened this issue Apr 5, 2021 · 3 comments
Closed

客户端发出主动关闭请求时无法触发onClose #78

cs-charles opened this issue Apr 5, 2021 · 3 comments
Labels
bug Something isn't working

Comments

@cs-charles
Copy link
Contributor

在案例控制服务端的最大连接数代码中,通过OnConnect事件和onClose事件来控制链接计数器。经本人测试后,如果是客户端发送header.OpCode为ws.OpClose后,服务端会调用c.ShutdownWrite()方法来关闭连接,同时设置c.connected.Set(false)。而onClose方法在响应关闭连接的事件回调后会触发如下逻辑,由于c.connected在前面已经被设置成了false,所以并不会执行onClose()方法。导致服务端的连接计数器不准确。

func (c *Connection) handleClose(fd int) {
	if c.connected.Get() {
		c.connected.Set(false)
		c.loop.DeleteFdInLoop(fd)

		c.callBack.OnClose(c)
		if err := unix.Close(fd); err != nil {
			log.Error("[close fd]", err)
		}

		ringbuffer.PutInPool(c.inBuffer)
		ringbuffer.PutInPool(c.outBuffer)
	}
}
@Allenxuxu
Copy link
Owner

这边确实有问题,ShutdownWrite 方法内部理论上不需要 c.connected.Set(false),这样也就应该没有这个问题了。

@Allenxuxu
Copy link
Owner

如果你有空,可以在 PR 加个单测复现下这个问题,应该删除这行就可以 Fix 这个问题了

c.connected.Set(false)

@Allenxuxu Allenxuxu added the bug Something isn't working label Apr 5, 2021
Allenxuxu added a commit that referenced this issue Apr 9, 2021
@Allenxuxu
Copy link
Owner

@cs-charles 这个bug 已经修复了,感谢指出

rfyiamcool pushed a commit to rfyiamcool/gev that referenced this issue Apr 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants