首页 话题 小组 问答 好文 用户 我的社区 域名交易 唠叨

[分享]PHP实现动态代理

发布于 2025-02-24 16:37:21
0
47
对接第三方时需要重试,使用反射简单写了一个重试代理,上代码:
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;
    }
}
评论
一个月内的热帖推荐
久久在线
Lv.1普通用户

551

帖子

20

小组

2021

积分

站长交流