public function getIdleConnection() { // we want to get the first available one // this will keep the connections at the front the busiest // and then we can add an idle timer to the connections foreach ($this->connections as $connection) { // need to figure out different states (in trans etc.) if ($connection->getState() === Connection::STATE_READY) { return $connection; } } // no idle connections were found - spin up new one $connection = new Connection($this->parameters, $this->loop); if (!$this->autoDisconnect) { $this->connections[] = $connection; $connection->on('close', function () use($connection) { $this->connections = array_filter($this->connections, function ($c) use($connection) { return $connection !== $c; }); }); } return $connection; }