Guesses the correct node associated to a given slot using a precalculated
slots map, falling back to the same logic used by Redis to initialize a
cluster (best-effort).

@param int $slot Slot index.

@return string Connection ID.
@throws RedisException
189832
protected function guessNode($slot)
    {
        if (isset($this->slotsMap[$slot])) {
            return $this->slotsMap[$slot];
        }

        $count = \count($this->hosts);
        $index = \min((int) ($slot / (int) (16384 / $count)), $count - 1);
        $nodes = \array_values($this->hosts);

        return $nodes[$index];
    }
718101
private function findSlotById(string $id, array $slots)
    {
        $slot = null;
        foreach($slots as $s){
            $slot_id = $s['id'] ?? null;
            if ($slot_id == $id){
                $slot = $s;
                break;
            }
        }
        return $slot;
    }
856218
public function getConnection($command, $args)
    {
        $slot = $this->getSlot($command, $args);
        if (null === $slot) {
            throw new \BadMethodCallException(
                "Cannot use '{$command}' with redis-cluster."
            );
        }

        if (isset($this->slots[$slot])) {
            return $this->slots[$slot];
        }

        return $this->getConnectionBySlot($slot);
    }
939427
public function get_heading_before_slot($slot) {
        if ($this->slots[$slot]->firstinsection) {
            return $this->slots[$slot]->section->heading;
        } else {
            return null;
        }
    }
620461
public function slot($id) : Slot
    {
        if (!isset($this->slots[$id])){
            return $this->slots[$id] = new Slot($id);
        }
        return $this->slots[$id];
    }
1019714
public static function isValid($slot)
    {
        switch (strtolower($slot)) {
        case self::STAGING:
        case self::PRODUCTION:
        return true;

        default:
        return false;
        }
    }
1030934
public function get_original_slot($slot) {
        $originalslot = $this->quba->get_question_attempt_metadata($slot, 'originalslot');
        if ($originalslot) {
            return $originalslot;
        } else {
            return $slot;
        }
    }
816567
public function redo_question_button($slot, $disabled) {
        $attributes = array('type' => 'submit',  'name' => 'redoslot' . $slot,
                'value' => get_string('redoquestion', 'quiz'), 'class' => 'mod_quiz-redo_question_button');
        if ($disabled) {
            $attributes['disabled'] = 'disabled';
        }
        return html_writer::div(html_writer::empty_tag('input', $attributes));
    }
147714
public function getConnection()
    {
        if (null === $this->connection) {
            $this->connection = new \Redis();

            if (!$this->connection->pconnect(
                $this->config['default']['host'],
                $this->config['default']['port']
            )) {
                throw new \RedisException('Unable to connect');
            }
        }

        return $this->connection;
    }
157761
protected function all_variant_stats_for_one_slot($slot) {
        $toreturn = array();
        foreach ($this->for_slot($slot)->get_variants() as $variant) {
            $toreturn[] = $this->for_slot($slot, $variant);
        }
        return $toreturn;
    }
736788
public function rename($source, $destination) {
        try {
            return $this->getRedis()->rename($source, $destination);
        } catch (RedisException $ex) {
            throw new ConnectException();
        }
    }
686685
public static function use($connection)
    {
        if (! is_null($config = config("database.redis.clusters.{$connection}.0"))) {
            config(["database.redis.{$connection}" => $config]);
        } elseif (is_null($config) && is_null($config = config("database.redis.{$connection}"))) {
            throw new Exception("Redis connection [{$connection}] has not been configured.");
        }

        config(['database.redis.horizon' => array_merge($config, [
            'options' => ['prefix' => config('horizon.prefix') ?: 'horizon:'],
        ])]);
    }
1194917
protected function getConnection() {
        if (count($this->pool) > 0) {
            /**
             * @var $connection RedisConnection
             */
            foreach ($this->pool as $k => $connection) {
                unset($this->pool[$k]);
                break;
            }
            return $connection;
        } else {
            return new RedisConnection($this);
        }
    }
993903
public function initialise_for_slot($slot, $question, $variant = null) {
        $newqstat = new calculated($question, $slot, $variant);
        if ($variant === null) {
            $this->questionstats[$slot] = $newqstat;
        } else {
            $this->questionstats[$slot]->variantstats[$variant] = $newqstat;
        }
    }
478786
public function read(int $bytes): string
	{
		$bytesLeft = $bytes;

		$data = '';

		do
		{
			$chunk = fread($this->connection, min($bytesLeft, 4096));

			if($chunk === false || $chunk === '')
			{
				throw new RedisException($this->appendReadErrorReason('Failed to read data from the server.'));
			}

			$data .= $chunk;

			$bytesLeft = $bytes - strlen($data);
		}
		while($bytesLeft > 0);

		return $data;
	}
1070859
public function is_first_slot_on_page($slotnumber) {
        if ($slotnumber == 1) {
            return true;
        }
        return $this->slotsinorder[$slotnumber]->page != $this->slotsinorder[$slotnumber - 1]->page;
    }
1180461
public function getRedisConnection(array $options = [])
    {
        $options = array_replace(['host' => 'localhost', 'port' => 6379], $options);
        if (null !== $cache = $this->getConnection('redis', $options)) {
            return $cache;
        }

        if (!extension_loaded('redis')) {
            throw new RuntimeException('The Redis cache requires the Redis extension.');
        }

        $redis = new \Redis();

        if (!@$redis->connect($options['host'], $options['port'])) {
            throw new RuntimeException(sprintf("Redis instance with host '%s' and port '%s' is not reachable.", $options['host'], $options['port']));
        }
        if (!defined('Redis::SERIALIZER_IGBINARY') || !$redis->setOption(\Redis::OPT_SERIALIZER, \Redis::SERIALIZER_IGBINARY)) {
            $redis->setOption(\Redis::OPT_SERIALIZER, \Redis::SERIALIZER_PHP);
        }

        return $this->setConnection('redis', $options, $redis);
    }