Skip to content

Commit

Permalink
added ability to use random server from cluster
Browse files Browse the repository at this point in the history
  • Loading branch information
TinderBox authored Sep 11, 2017
1 parent 08eabd9 commit 2a6f0a5
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ class Client
*/
protected $cluster;

/**
* Tells client to send queries over whole cluster selecting server by random
*
* @var bool
*/
protected $useRandomServer = false;

/**
* Client constructor.
*
Expand Down Expand Up @@ -77,6 +84,26 @@ public function __construct($server, QueryMapperInterface $mapper = null, Transp
$this->setMapper($mapper);
}

/**
* Sets flag to use random server in cluster
*
* @param bool $flag
*/
public function useRandomServer(bool $flag)
{
$this->useRandomServer = $flag;
}

/**
* Returns flag which tells client to use random server in cluster
*
* @return bool
*/
protected function shouldUseRandomServer() : bool
{
return !is_null($this->getCluster()) && $this->useRandomServer;
}

/**
* Creates default http transport.
*
Expand Down Expand Up @@ -162,9 +189,29 @@ public function setServer(Server $server): self
*/
public function getServer(): Server
{
if ($this->shouldUseRandomServer()) {
return $this->getRandomServer();
}

return $this->server;
}

/**
* Returns random server from cluster
*
* @return \Tinderbox\Clickhouse\Server
*/
public function getRandomServer() : Server
{
$cluster = $this->getCluster();
$servers = $cluster->getServers();
$random = array_rand($servers, 1);

dump($servers[$random]);

return $servers[$random];
}

/**
* Sets cluster.
*
Expand Down

0 comments on commit 2a6f0a5

Please sign in to comment.