We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Shared Connection pool

Hi, I have a question about the method setShared used in services. I'm using the library elastic-php which defines the concept of connection pool. Following the guide of elastic php I defined in services an object client:

$client = ClientBuilder::create()
        ->setConnectionPool('\Elasticsearch\ConnectionPool\StaticNoPingConnectionPool', [])
        ->build();

and I set it as shared (setShared). In my controller I get it with di and I use it to connect to my backed. I should have some more info about setshared because I don't want any problem of concurrency. What does setshared exactly mean? Each client request is served by an instance of my controller and all these processes (or thread?) use the client created with ClientBuilder. It is safe?

Thanks

setShared means that each time you access service in di the same object will be returned.



2.0k

So I suppose that to handle concurrent request the elastic-php library must have some mechanism (example semaphores) to give next connection to client. If not it can't be used because two concurrent clients could get the same connection. In this case I should set object $client in services not shared .....

Idk, i just told you what setShared is doing, im not using this library but it used somewhere on forum i think. You can check forum source in phalcon repo - https://github.com/phalcon/forum



2.0k
edited Oct '16

Ok I checked the phalcon forum and in my opinion there is an error. In file bootstrap.php:

protected function initElastic()
{
    $this->di->setShared('elastic', function () {
        /**
         * @var DiInterface $this
         * @var Config $config
         */
        $config = $this->getShared('config')->get('elasticsearch', new Config);
        $hosts  = $config->get('hosts', new Config)->toArray();
        if (empty($hosts)) {
            // Fallback
            $hosts = ['127.0.0.1:9200'];
        }
        return new ElasticClient(['hosts' => $hosts]);
    });
}

So the object is shared but the elastic-php is not thread safe (see nextConnection method in StaticNoPingConnectionPool). Two concurrent clients could get the same connection....

edited Oct '16

This has NOTHING to do about thread safe. If you setShared then doing:

$di->get('elastic');
$di->get('elastic');

Will return the same object, if it would be no shared then it would return diffrent object.

Set shared is just to set singleton - and nothing more.



2.0k
edited Oct '16

it is clear. The class using setshared is initialized only one time. What is not clear to me is if it is safe using it with the library elastic-php

edited Oct '16

Idk, im not using it myself. I guess yes if you don't want to have not needed connections.

Just as i already wrote, set shared has totally NOTHING TO DO about thread safe. It's not creating new thread and anything like this. It's just for singleton.