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

upd keep_alive configuration and behavior desc #37

Closed
wants to merge 2 commits into from

Conversation

ihipop
Copy link
Contributor

@ihipop ihipop commented Apr 19, 2019

@twose
Copy link
Member

twose commented Apr 19, 2019

为什么非重定向请求的就可以长连接呢 哪里的代码逻辑有问题?

@ihipop
Copy link
Contributor Author

ihipop commented Apr 19, 2019

为什么非重定向请求的就可以长连接呢 哪里的代码逻辑有问题?

你问题问错了,是为什么 **重定向** 请求才可以长连接

重定向请求你是同一个Swoole http client自动接续完成的(判断了header里面的location,更新当前的$request的目的地然后继续exec(),同服务器和端口的情况下,Swoole http client会自动使用keepalive连接)

saber/src/Request.php

Lines 670 to 711 in b845add

if (($this->client->headers['location'] ?? false) && $this->_redirect_times < $this->redirect) {
$current_uri = (string)$this->uri;
//record headers before redirect
$this->_redirect_headers[$current_uri] = PHP_DEBUG ?
array_merge([], $this->client->headers) :
$this->client->headers;
$location = $this->client->headers['location'];
$this->uri = Uri::resolve($this->uri, $location);
if ($this->uri->getPort() === 443) {
$this->withSSL(true);
}
// TODO: remove some secret information
$this->withMethod('GET')
->withBody(null)
->withHeader('Host', $this->uri->getHost())
->withHeader('Referer', $current_uri)
->removeInterceptor('request');
/**
* Redirect-interceptors have permission to release or intercept redirects,
* just return a bool type value
*/
$allow_redirect = true;
$ret = $this->callInterceptor('before_redirect', $this);
if ($ret !== null) {
if (is_bool($ret)) {
$allow_redirect = $ret;
} else {
return $ret;
}
}
if ($allow_redirect) {
$this->_form_flag |= self::FROM_REDIRECT;
$this->exec();
$this->_redirect_times++;
if ($this->isInQueue()) {
return $this;
}
return $this->recv();

image

而我每次对同一个服务器即使是同一个URL请求你也是重新生成一个$request,然后 这个新生成的$request是没有绑定Swoole http client,然后你从这个代码里面生成了一个新客户端

$this->client = $client_pool->createEx($options, !$this->use_pool);

根源还是$request和实际发送请求的客户端绑定的设计导致的

其实有workaround可以修复的,但你说只接受文档更新嘛

@twose
Copy link
Member

twose commented Apr 19, 2019

写错了 你也可以pr修复补丁

@ihipop
Copy link
Contributor Author

ihipop commented Apr 22, 2019

OK 那我重新开个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.

2 participants