arntech / guzzle-pool-client
ARNTech guzzle pool.
0.0.3
2020-06-30 11:50 UTC
Requires
- php: >=7.1
- ext-json: *
- arntech/utils: >=0.0.2
- guzzlehttp/guzzle: >=6 <7
This package is auto-updated.
Last update: 2024-09-29 06:14:21 UTC
README
这是什么?
这是一个实现了多个(希望未来会超过一个)池类型的 Guzzle 客户端库。目前,重点是使用非锁定池,以实现远程调用的更好异步调度。
这意味着什么?
Guzzle 默认具有阻塞池。在调用 $promise->wait() 之后,它们不接受新的请求。这个问题在于,如果您需要根据旧调用的响应进行新调用。对于这个问题,传统上应该等待当前请求队列稳定下来。
安装
首选的安装方法是使用 [Composer]。运行以下命令安装包并将其添加到项目的 composer.json
中的要求。
composer require arntech/card
使用方法
创建一个新的 Pool
$pool = new DynamicPool(100);// where 100 is the pool size
创建一个客户端
$pool = new DynamicPool(100);// where 100 is the pool size $client = new DynamicPoolClient(['pool'=>$pool]); //or $client = new DynamicPoolClient(['pool_size'=>100]);// where 100 is the pool size //or use uniqe calls for each request $client = new DynamicUniquePoolClient(['pool_size'=>100]); $client->add($promise, 'key');
DynamicPoolClient 扩展了 GuzzleHttp\Client。构造函数接受所有 GuzzleHttp\Client 参数,加上 pool 或 pool_size。DynamicUniquePoolClient 是一个客户端,它需要并使用 DynamicUniquePool,此池确保任何连续请求仅执行一次。这是通过设置一个键(例如,url 和请求参数之间的哈希)来实现的。
示例
1. DynamicPoolClient
$this->client = new DynamicPoolClient(['pool_size'=>10]); $req1=$this->client->getAsync('http://first.url') ->then(function ($response) { //do something $req2=$this->client->getAsync('http://second.url') ->then(function ($response) { //do something with response }); $this->client->add($req2); }); $this->client->add($req1); $this->client->wait();
2. DynamicUniquePoolClient
private function runGet($url) { $this->client->add($this->client->getAsync($url), $url); } $this->client = new DynamicUniquePoolClient(['pool_size'=>10]); $this->runGet('http://first.url'); $this->runGet('http://second.url'); $this->runGet('http://first.url'); //this will be canceled $this->client->wait();