|
13 | 13 |
|
14 | 14 | use Hyperf\Contract\PackerInterface; |
15 | 15 | use Hyperf\Dispatcher\HttpDispatcher; |
16 | | -use Hyperf\Engine\Coroutine; |
17 | 16 | use Hyperf\ExceptionHandler\ExceptionHandlerDispatcher; |
18 | 17 | use Hyperf\HttpMessage\Server\Request as Psr7Request; |
19 | 18 | use Hyperf\HttpMessage\Server\Response as Psr7Response; |
|
28 | 27 | use Hyperf\Utils\Context; |
29 | 28 | use Hyperf\Utils\Filesystem\Filesystem; |
30 | 29 | use Hyperf\Utils\Packer\JsonPacker; |
31 | | -use Hyperf\Utils\Str; |
32 | 30 | use Psr\Container\ContainerInterface; |
33 | 31 | use Psr\Http\Message\ResponseInterface; |
34 | 32 | use Psr\Http\Message\ServerRequestInterface; |
35 | 33 |
|
36 | 34 | class Client extends Server |
37 | 35 | { |
38 | 36 | /** |
39 | | - * @var array |
| 37 | + * @var PackerInterface |
40 | 38 | */ |
41 | | - public $ignoreContextPrefix = [ |
42 | | - 'database.connection', |
43 | | - 'redis.connection', |
44 | | - ]; |
| 39 | + protected $packer; |
45 | 40 |
|
46 | 41 | /** |
47 | | - * @var PackerInterface |
| 42 | + * @var float |
48 | 43 | */ |
49 | | - protected $packer; |
| 44 | + protected $waitTimeout = 10.0; |
50 | 45 |
|
51 | 46 | public function __construct(ContainerInterface $container, PackerInterface $packer = null, $server = 'http') |
52 | 47 | { |
@@ -134,34 +129,34 @@ public function file($uri, $data = [], $headers = []) |
134 | 129 |
|
135 | 130 | public function request(string $method, string $path, array $options = []) |
136 | 131 | { |
137 | | - /* |
138 | | - * @var Psr7Request |
139 | | - */ |
140 | | - [$psr7Request, $psr7Response] = $this->init($method, $path, $options); |
141 | | - |
142 | | - $psr7Request = $this->coreMiddleware->dispatch($psr7Request); |
143 | | - /** @var Dispatched $dispatched */ |
144 | | - $dispatched = $psr7Request->getAttribute(Dispatched::class); |
145 | | - $middlewares = $this->middlewares; |
146 | | - if ($dispatched->isFound()) { |
147 | | - $registeredMiddlewares = MiddlewareManager::get($this->serverName, $dispatched->handler->route, $psr7Request->getMethod()); |
148 | | - $middlewares = array_merge($middlewares, $registeredMiddlewares); |
149 | | - } |
| 132 | + return wait(function () use ($method, $path, $options) { |
| 133 | + /* |
| 134 | + * @var Psr7Request |
| 135 | + */ |
| 136 | + [$psr7Request, $psr7Response] = $this->init($method, $path, $options); |
| 137 | + |
| 138 | + $psr7Request = $this->coreMiddleware->dispatch($psr7Request); |
| 139 | + /** @var Dispatched $dispatched */ |
| 140 | + $dispatched = $psr7Request->getAttribute(Dispatched::class); |
| 141 | + $middlewares = $this->middlewares; |
| 142 | + if ($dispatched->isFound()) { |
| 143 | + $registeredMiddlewares = MiddlewareManager::get($this->serverName, $dispatched->handler->route, $psr7Request->getMethod()); |
| 144 | + $middlewares = array_merge($middlewares, $registeredMiddlewares); |
| 145 | + } |
150 | 146 |
|
151 | | - try { |
152 | | - $psr7Response = $this->dispatcher->dispatch($psr7Request, $middlewares, $this->coreMiddleware); |
153 | | - } catch (\Throwable $throwable) { |
154 | | - // Delegate the exception to exception handler. |
155 | | - $psr7Response = $this->exceptionHandlerDispatcher->dispatch($throwable, $this->exceptionHandlers); |
156 | | - } |
| 147 | + try { |
| 148 | + $psr7Response = $this->dispatcher->dispatch($psr7Request, $middlewares, $this->coreMiddleware); |
| 149 | + } catch (\Throwable $throwable) { |
| 150 | + // Delegate the exception to exception handler. |
| 151 | + $psr7Response = $this->exceptionHandlerDispatcher->dispatch($throwable, $this->exceptionHandlers); |
| 152 | + } |
157 | 153 |
|
158 | | - return $psr7Response; |
| 154 | + return $psr7Response; |
| 155 | + }, $this->waitTimeout); |
159 | 156 | } |
160 | 157 |
|
161 | 158 | protected function init(string $method, string $path, array $options = []): array |
162 | 159 | { |
163 | | - $this->flushContext(); |
164 | | - |
165 | 160 | $query = $options['query'] ?? []; |
166 | 161 | $params = $options['form_params'] ?? []; |
167 | 162 | $json = $options['json'] ?? []; |
@@ -192,18 +187,6 @@ protected function init(string $method, string $path, array $options = []): arra |
192 | 187 | return [$psr7Request, $psr7Response]; |
193 | 188 | } |
194 | 189 |
|
195 | | - protected function flushContext() |
196 | | - { |
197 | | - $context = Coroutine::getContextFor() ?? []; |
198 | | - |
199 | | - foreach ($context as $key => $value) { |
200 | | - if (Str::startsWith($key, $this->ignoreContextPrefix)) { |
201 | | - continue; |
202 | | - } |
203 | | - $context[$key] = null; |
204 | | - } |
205 | | - } |
206 | | - |
207 | 190 | protected function normalizeFiles(array $multipart): array |
208 | 191 | { |
209 | 192 | $files = []; |
|
0 commit comments