php-nsq
php-nsq copied to clipboard
是否需要每个请求结束之后都close NSQ链接?
由于PHP的特性,每个请求都会connectNsqd,请问此时是扩展接管TCP连接池还是每次都会重新创建一个TCP链接,所以每次请求结束,都需要在__destrct函数中close NSQ链接避免NSQ连接数爆掉?
扩展在publish的时候属于那种情况?希望能帮忙解答,谢谢!
还没有建立连接池,目前会重建创建tcp链接,你可以使用单例模式来减少你说的这种情况,
// 使用单例模式 + 长连接
class NsqManager {
private static $instance = null;
private $nsq = null;
private function __construct() {
$this->nsq = new Nsq();
$this->nsq->connectNsqd([...]);
}
public static function getInstance() {
if (self::$instance === null) {
self::$instance = new self();
}
return self::$instance;
}
public function publish($topic, $message) {
return $this->nsq->publish($topic, $message);
}
public function __destruct() {
if ($this->nsq) {
$this->nsq->closeNsqdConnection();
}
}
}
// 使用时
NsqManager::getInstance()->publish($topic, $message);