-
-
Notifications
You must be signed in to change notification settings - Fork 990
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support the stream path in the connection params and URI (for persistent connections with database initCmd) #139
Conversation
Mostly as a way to allow multiple persistent connections to single [ipaddress, port] pair.
Might it be appropriate to build the path out of the alias if one isn't supplied? I don't know, but it seemed like a bigger change to me (might break intentional sharing of the underlying connection via two different aliases), whereas this is a backward compatible feature addition. |
I wouldn't use
I think a different parameter should be used in this case, but I can't think of anything decent right now. |
Some thoughts:
|
After thinking about it I think I'll accept your request as is, but using a slightly different approach. I prefer not to add a default value for $uri = "tcp://{$parameters->host}:{$parameters->port}";
if (isset($parameters->persistent) && $parameters->persistent) {
$flags |= STREAM_CLIENT_PERSISTENT;
$uri .= strpos($path = $parameters->path, '/') === 0 ? $path : "/$path";
} A couple of notes:
Would it be OK? |
Change is available on both Thanks @dominics. |
Fantastic, thanks! |
…onnections. stream_socket_client() has the undocumented ability to open different persistent streams by providing a path in the $address string. Previously we supported this behaviour with a combination of "persistent" and "path" (see #139) but this can be confusing, especially now that we support the redis:// scheme which uses the path part of an URI string to specify a database number. After this change, instead of using an URI string such as: $parameters = 'tcp://127.0.0.1/first?persistent=1&database=5'; You should use the following ones: $parameters = 'tcp://127.0.0.1?persistent=first&database=5'; $parameters = 'redis://127.0.0.1/5?persistent=first'; Avoiding "path" makes even more sense when using array connection parameters: $parameters = [ 'host' => '127.0.0.1', 'database' => 5, 'persistent' => 'first', ] This feature is not supported when using UNIX domain sockets because the path trick of stream_socket_client() does not play well with the actual path of the socket file. The client will throw an InvalidArgumentException exception to notify the user. NOTE: unfortunately we have to disable the tests for persistent connections when running under HHVM due to a bug in their implementation of get_resource_type() preventing us to recognize a persistent stream from userland code.
Here's a quick example test for the interaction between the
database
connection parameter, and persistent connections:As you can see, initCmds are only run once, upon a connection being established. Then, because the two clients are connecting to the same IP address and port combination, with persistent connections turned on, they share a single persistent connection. The result is that the third
ping()
is executed in the wrong database (although this is particularly bad when the command isflushdb
instead ofping
:-)The PHP manual user notes (sigh) for
stream_socket_client
mention that it actually supports specifying a path, and that this will make it open separate persistent connections:This seems to work. With this PR applied, and specifying different paths in the above example (say,
tcp://127.0.0.1:6379/first?persistent=1&database=0
andtcp://127.0.0.1:6379/second?persistent=1&database=1
), separate persistent connections are opened, and we can happily have one for each database. Then theSELECT
in the initCmds will happily stick around, and won't be overwritten.