熔断器调用

@param mixed $callback 回调函数
@param array $params 参数
@param mixed $fallback 失败回调

@return mixed 返回结果
552722
public function doCall($callback, $params = [], $fallback = null)
    {
        // 加锁
        $lock = $this->circuitBreaker->getHalfOpenLock();
        $lock->lock();
        list($class, $method) = $callback;

        try {
            if ($class === null) {
                throw new \Exception($this->getServiceName() . '服务, 建立连接失败(null)');
            }

            if (
                ($class instanceof Client && $class->isConnected() == false) ||
                ($class instanceof ConnectionInterface && $class->check() == false)
            ) {
                throw new \Exception($this->circuitBreaker->serviceName . '服务,当前连接已断开');
            }

            $data = $class->$method(...$params);
            $this->circuitBreaker->incSuccessCount();
            App::trace($this->getServiceName() . '服务，当前[半开状态]，尝试执行成');
        } catch (\Exception $e) {
            $this->circuitBreaker->incFailCount();
            $data = $this->circuitBreaker->fallback($fallback);
            App::error($this->getServiceName() . '服务，当前[半开状态]，尝试执行失败, error=' . $e->getMessage());
        }

        $failCount = $this->circuitBreaker->getFailCounter();
        $successCount = $this->circuitBreaker->getSuccessCounter();
        $switchToFailCount = $this->circuitBreaker->getSwitchToFailCount();
        $switchToSuccessCount = $this->circuitBreaker->getSwitchToSuccessCount();

        if ($failCount >= $switchToFailCount && $this->circuitBreaker->isHalfOpen()) {
            $this->circuitBreaker->switchToOpenState();
            App::trace($this->getServiceName() . '服务，当前[半开状态]，失败次数达到上限，开始切换到开启状态');
        }

        if ($successCount >= $switchToSuccessCount) {
            $this->circuitBreaker->switchToCloseState();
            App::trace($this->getServiceName() . '服务，当前[半开状态]，成功次数达到上限，服务以及恢复，开始切换到关闭状态');
        }

        // 释放锁
        $lock->unlock();

        App::trace($this->getServiceName() . '服务，当前[半开状态], failCount=' . $failCount . ' successCount=' . $successCount);

        return $data;
    }
503934
protected function getCallback($callback, array $parameters)
    {
        if ($callback instanceof Closure) {
            return $this->createCallableCallback($callback, $parameters);
        } elseif (is_string($callback)) {
            return $this->createStringCallback($callback, $parameters);
        } else {
            return;
        }
    }
1159432
public function writeCallback($callback)
    {
        if (is_callable($callback)) {
            $debugs = debug_backtrace(version_compare(PHP_VERSION, '5.3.6', '>=') ? DEBUG_BACKTRACE_PROVIDE_OBJECT : true);
            // this is current function debug backtrace
            $current = array_shift($debugs);
            // this is the current function caller debug backtrace
            $caller = array_shift($debugs);
            call_user_func($callback, $this, isset($caller['object']) ? $caller['object'] : $this);
        }

        return $this;
    }
48056
public function group(array $params, $callback)
    {
        $prefix = trim($this->getParam($params, 'prefix'), '/');
        $before = $this->getParam($params, 'before');
        $after  = $this->getParam($params, 'after');

        if ($prefix) {
            $this->prefixes[] = $prefix;
            $this->prefix     = '/' . trim(implode('/', $this->prefixes), '/');
        }

        if ($before) {
            $this->befores[] = $before;
            $this->before    = explode('|', implode('|', $this->befores));
        }

        if ($after) {
            $this->afters[] = $after;
            $this->after    = explode('|', implode('|', $this->afters));
        }

        call_user_func_array($callback, [$this]);

        if ($prefix) {
            array_pop($this->prefixes);
            $this->prefix = $this->prefixes ? '/' . trim(implode('/', $this->prefixes), '/') : null;
        }

        if ($before) {
            array_pop($this->befores);
            $this->before = explode('|', implode('|', $this->befores));
        }

        if ($after) {
            array_pop($this->afters);
            $this->after = explode('|', implode('|', $this->afters));
        }

        return $this;
    }
297350
private function _get_template( $fallback = '' ) {
		$templates = $this->_templates();

		foreach ( $templates as $template ) {
			if ( file_exists( $template ) ) {
				return $template;
			}
		}

		return $fallback;
	}
122020
private function run($callback)
    {
        $injects = [];
        $reflectionFunction = new \ReflectionFunction($callback);
        foreach ($reflectionFunction->getParameters() as $parameter) {
            if ($class = $parameter->getClass()) {
                $injects[] = app($class->name);
            }
        }

        return call_user_func_array($callback, $injects);
    }
297494
private function getArguments(callable $callback, array $callbackArguments)
    {
        $parameters = $this->getParameters($callback);
        $arguments = [];
        foreach ($parameters as $param) {
            if (array_key_exists($param->getName(), $callbackArguments)) {
                $arguments[] = $callbackArguments[$param->getName()];
            } elseif ($param->isDefaultValueAvailable()) {
                $arguments[] = $param->getDefaultValue();
            } else {
                $arguments[$param->getName()] = null;
            }
        }

        return $arguments;
    }
596315
protected function executeCallback($callback, $args)
    {
        if (is_string($callback)) {
            return $this->callStringCallback($callback, $args);
        } elseif ($callback instanceof Closure) {
            return call_user_func_array($callback, $args);
        }

        return '';
    }
18727
public function setFallbacks($fallback)
    {
        $this->localeFallbacks = (array)$fallback;
        if ($this->coreFormatter instanceof Multilingual) {
            $this->coreFormatter->setFallbacks($this->localeFallbacks);
        }
        if ($this->textProvider instanceof Multilingual) {
            $this->textProvider->setFallbacks($this->localeFallbacks);
        }
        return $this;
    }
1248746
public function open($callback = null)
    {
        $result = $this->redis->connect($this->config['host'] ?? '127.0.0.1', $this->config['port'] ?? 6379, $this->config['serialize'] ?? true);
        if(isset($this->config['password']))
        {
            $result = $result && $this->redis->auth($this->config['password']);
        }
        if(isset($this->config['db']))
        {
            $r = $this->redis->select($this->config['db']);
            $result = $result && $r;
        }
        return $result;
    }
67991
public function filter(\Closure $callback, $reindex = true) {
		$result = array_filter($this->annotations, $callback);
		
		if ($reindex) {
			$result = array_values($result);
		}
		
		return $result;
	}
718495
public static function factory( $callback, $params = array() ) {
		$route = new static();
		$route->callback = $callback;
		$route->params = $params;
		return $route;
	}
568343
public function call($callback, array $parameters = [])
    {
        switch (count($parameters)) {
            case 0:
                return call_user_func($callback);
            case 1:
                return call_user_func($callback, $parameters[0]);
            case 2:
                return call_user_func($callback, $parameters[0], $parameters[1]);
            case 3:
                return call_user_func($callback, $parameters[0], $parameters[1], $parameters[2]);
            case 4:
                return call_user_func($callback, $parameters[0], $parameters[1], $parameters[2], $parameters[3]);
        }

        return call_user_func_array($callback, $parameters);
    }
1327114
public function getBackLink($fallback = '')
    {
        $url = '';

        if ($this->owner->Request) {
            if ($this->owner->Request->requestVar('BackURL')) {
                $url = $this->owner->Request->requestVar('BackURL');
            } else {
                if ($this->owner->Request->isAjax() && $this->owner->Request->getHeader('X-Backurl')) {
                    $url = $this->owner->Request->getHeader('X-Backurl');
                } else {
                    if ($this->owner->Request->getHeader('Referer')) {
                        $url = $this->owner->Request->getHeader('Referer');
                    }
                }
            }
        }

        if (!$url) {
            $url = $fallback ? $fallback : singleton('director')->baseURL();
        }

        return $url;
    }
1295386
public function resolveCallback(Closure $callback, $params = [])
    {
        $reflectionParameter = new ReflectionFunction($callback);

        $dependencies = $reflectionParameter->getParameters();

        return $this->getDependencies($dependencies, $params);
    }
1186559
public function headers($key = null, $fallback = null)
    {
        return $key
            ? $this->request->headers->get($key, $fallback)
            : $this->request->headers;
    }
731015
static function executeCallback($callback, array $params = array()) {
    // If callback has @
    if (is_string($callback) && strpos($callback, '@') !== false) {
      // Split
      $arrCallback = explode('@', $callback);
      // Set object
      $object = class_exists($arrCallback[0]) ? (new $arrCallback[0]()) : null;
      // Set method
      $method = isset($arrCallback[0]) ? $arrCallback[0] : null;
      // Set callback
      $callback = array($object, $method);
    }
    // If not callable
    if ( ! is_callable($callback)) {
      // Throw error
      throw new InvalidCallback('Callback must be callable');
    }
    // Execute and return
    return call_user_func_array($callback, $params);
  }