-
Notifications
You must be signed in to change notification settings - Fork 18
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
Add support for Unix sockets #4
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,7 @@ module Redis | |
class Connection | ||
include Commands | ||
|
||
@socket : TCPSocket | OpenSSL::SSL::Socket::Client | ||
@socket : TCPSocket | OpenSSL::SSL::Socket::Client | UNIXSocket | ||
|
||
CRLF = "\r\n" | ||
|
||
|
@@ -21,16 +21,31 @@ module Redis | |
# SSL connections require specifying the `rediss://` scheme. | ||
# Password authentication uses the URI password. | ||
# DB selection uses the URI path. | ||
# | ||
# To use Unix sockets: `socket:/path/to/redis.sock` | ||
def initialize(@uri = URI.parse("redis:///")) | ||
host = uri.host || "localhost" | ||
port = uri.port || 6379 | ||
socket = TCPSocket.new(host, port) | ||
socket.sync = false | ||
|
||
# Check whether we should use SSL | ||
if uri.scheme == "rediss" | ||
socket = OpenSSL::SSL::Socket::Client.new(socket) | ||
if uri.scheme == "socket" | ||
socket = UNIXSocket.new(uri.path) | ||
socket.sync = false | ||
db = "0" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see why you did it this way, but it seems like it would be impossible to use a DB slot other than 0 while using a Unix socket. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Have to admit I only learned later what DB slots are. But agreed that it does not make sense to exclude the functionality depending on the connection type without any need. I'll change it when I have some time. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Following along the same lines as the other reply, if other Redis clients have opinions on how to select a different DB with a Unix socket URL, I'm happy to follow suit here. |
||
else | ||
host = uri.host || "localhost" | ||
port = uri.port || 6379 | ||
socket = TCPSocket.new(host, port) | ||
socket.sync = false | ||
|
||
# Check whether we should use SSL | ||
if uri.scheme == "rediss" | ||
socket = OpenSSL::SSL::Socket::Client.new(socket) | ||
socket.sync = false | ||
end | ||
|
||
# DB select | ||
db = if {"", "/"}.includes?(uri.path) | ||
"0" | ||
else | ||
uri.path[1..-1] | ||
end | ||
end | ||
|
||
@socket = socket | ||
|
@@ -42,12 +57,6 @@ module Redis | |
run({"auth", password}) | ||
end | ||
|
||
# DB select | ||
db = if {"", "/"}.includes?(uri.path) | ||
"0" | ||
else | ||
uri.path[1..-1] | ||
end | ||
run({"select", db}) unless db == "0" | ||
end | ||
end | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thinking about this a bit more, I think the
socket
URL scheme may be a bit too generic a name — they're all sockets. 😄 Do you think it might make more sense to useunix
as the URL scheme?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see the point. Unfortunately, there is not a standard among the Redis clients to build on. Redis itself (e.g. in redis.conf) refers to it as "unixsocket". Maybe that is the clearest?
It is also more consistent with the other existing Redis client for Crystal: https://github.com/stefanwille/crystal-redis/blob/009e35fa40bbd518798afcc32c397402c6c6acb2/src/redis/connection.cr#L8
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Apologies for the wildly delayed reply here. I seem to have pulled this move:
If the Redis server recommends
unixsocket
, I imagine that's what most Redis clients also use, and consistency can be pretty important. Admittedly, since I almost always use TCP sockets for IPC even when Unix sockets are available, I don't have many personal opinions on how to use Unix sockets so my only real recommendation is least-surprise. 🙂