对接第三方时需要重试,使用反射简单写了一个重试代理,上代码:
class Retry {
/**
* @var object 代理对象
*/
private $__proxy;
/**
* @var int 重试一次
*/
private $__retry_times = 1;
/**
* @var int 延迟一秒
*/
private $__delay_time = 1000000;
/**
* @param $name
* @param $args
* @return mixed
* @throws Exception
*/
public function __call($name, $args){
if(!method_exists($this->get_proxy(), $name)){
throw new Exception('cannot found method.');
}
$times = 0;
$reflection = new ReflectionClass($this->get_proxy());
$method = $reflection->getMethod($name);
if (!$method->isPublic() || $method->isAbstract()) {
throw new Exception('method is not public or is abstract.');
}
while ($times < $this->get_retry_times()){
try{
$res = $method->invokeArgs($this->get_proxy(), $args);
return $res;
}catch (Exception $e){
$times++;
if($times >= $this->__retry_times){
throw $e;
}
usleep($this->get_delay_microseconds());
}
}
}
public function set_proxy($real_subject){
$this->__proxy = $real_subject;
}
public function get_proxy(){
return $this->__proxy;
}
public function set_retry_times($retry_times){
$this->__retry_times = $retry_times;
}
public function get_retry_times(){
return $this->__retry_times;
}
public function set_delay_microseconds($delay_time){
$this->__delay_time = $delay_time;
}
public function get_delay_microseconds(){
return $this->__delay_time;
}
}