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

请问http长连接如何复用? #198

Closed
wooviet opened this issue Dec 31, 2020 · 5 comments
Closed

请问http长连接如何复用? #198

wooviet opened this issue Dec 31, 2020 · 5 comments

Comments

@wooviet
Copy link

wooviet commented Dec 31, 2020

除了头Connection字段,还有什么点需要处理呢?抓包发现基本上每个请求都建立了连接。

@Barenboim
Copy link
Contributor

没有道理啊,连接复用非常智能的,每个请求都建立连接是不是对方主动关闭了连接?
task->get_task_seq()可以看到当前任务是连接上的第几次请求。你可以打一下。

@wooviet
Copy link
Author

wooviet commented Dec 31, 2020

我又测试了一下,以下是一个简单的总结:
前提:访问同一个url,不设置connection字段
结论:
1.一个循环中建立task并start,循环100次,基本上不复用
2.前一个task的回调中创建新的task并start,如此循环5次,复用
3.前一个task的回调中创建新的task,将新的task放到前一个task series中,如此循环5次,复用

前提:访问不同url(服务器地址相同),不设置connection字段
结论:
1.不复用 2.复用 3.复用

@wooviet
Copy link
Author

wooviet commented Dec 31, 2020

我理解的复用是同一个地址的请求,都应该复用,无论我是在哪个线程发起的请求,但根据测试结果来看,在回调线程中发起下一个请求才是复用的。

@Barenboim
Copy link
Contributor

噢噢,连接复用的原理是有空闲连接就复用,如果没有空闲连接而且并发连接数没有超过最大值(默认200),则发起新连接;达到最大值,则得到EAGAIN错误。你的第一个case是100个请求同时发起,那么很大概率上是并发请求,所以没有办法复用连接。

@wooviet
Copy link
Author

wooviet commented Dec 31, 2020

测了一下,确实是这样,感谢! @Barenboim

@wooviet wooviet closed this as completed Dec 31, 2020
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

No branches or pull requests

2 participants