Skip to content

Commit 3c5a1ab

Browse files
committedJun 1, 2011
Add support for UNIX domain sockets when availabe in LuaSocket.
1 parent 154a05f commit 3c5a1ab

File tree

3 files changed

+80
-30
lines changed

3 files changed

+80
-30
lines changed
 

‎CHANGELOG

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ v2.0.2 (2011-??-??)
22
* Added an abstraction for PUB/SUB that makes possible to consume messages
33
pushed to channels using a coroutine-based iterator.
44

5+
* Added support for connecting to Redis using UNIX domain sockets when they
6+
are available in LuaSocket.
7+
58
v2.0.1 (2011-01-24)
69
* Vastly improved abstraction for Redis transactions (MULTI/EXEC) supporting
710
check-and-set (CAS), automatic retries upon transaction failures and a few

‎README.markdown

+8
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ redis-lua is a pure Lua client library for the Redis advanced key-value database
1010
- Command pipelining
1111
- Redis transactions (MULTI/EXEC) with CAS
1212
- User-definable commands
13+
- UNIX domain sockets (when available in LuaSocket)
1314

1415
## Examples of usage ##
1516

@@ -26,6 +27,13 @@ local redis = Redis.connect('127.0.0.1', 6379)
2627
local response = redis:ping() -- true
2728
```
2829

30+
It is also possible to connect to a local redis instance using __UNIX domain sockets__
31+
if LuaSocket has been compiled with them enabled (unfortunately it is not the default):
32+
33+
``` lua
34+
local redis = Redis.connect('unix:///tmp/redis.sock')
35+
```
36+
2937
### Set keys and get their values ###
3038

3139
``` lua

‎src/redis.lua

+69-30
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
module('Redis', package.seeall)
22

3-
local socket = require('socket')
4-
local uri = require('socket.url')
3+
local commands, network, request, response = {}, {}, {}, {}
54

6-
local commands = {}
7-
local network, request, response = {}, {}, {}
5+
local defaults = {
6+
host = '127.0.0.1',
7+
port = 6379,
8+
tcp_nodelay = true,
9+
path = nil
10+
}
811

9-
local defaults = { host = '127.0.0.1', port = 6379, tcp_nodelay = true }
1012
local protocol = {
1113
newline = '\r\n',
1214
ok = 'OK',
@@ -15,6 +17,18 @@ local protocol = {
1517
null = 'nil'
1618
}
1719

20+
local function merge_defaults(parameters)
21+
if parameters == nil then
22+
parameters = {}
23+
end
24+
for k, v in pairs(defaults) do
25+
if parameters[k] == nil then
26+
parameters[k] = defaults[k]
27+
end
28+
end
29+
return parameters
30+
end
31+
1832
local function parse_boolean(v)
1933
if v == '1' or v == 'true' or v == 'TRUE' then
2034
return true
@@ -614,47 +628,72 @@ end
614628

615629
-- ############################################################################
616630

631+
function connect_tcp(socket, parameters)
632+
local host, port = parameters.host, tonumber(parameters.port)
633+
local ok, err = socket:connect(host, port)
634+
if not ok then
635+
error('could not connect to '..host..':'..port..' ['..err..']')
636+
end
637+
socket:setoption('tcp-nodelay', parameters.tcp_nodelay)
638+
return socket
639+
end
640+
641+
function connect_unix(socket, parameters)
642+
local ok, err = socket:connect(parameters.path)
643+
if not ok then
644+
error('could not connect to '..parameters.path..' ['..err..']')
645+
end
646+
return socket
647+
end
648+
649+
function create_connection(parameters)
650+
local perform_connection, driver
651+
local socket = require('socket')
652+
653+
if parameters.scheme == 'unix' then
654+
perform_connection, driver = connect_unix, require('socket.unix')
655+
assert(driver, 'your build of LuaSocket does not support UNIX domain sockets')
656+
else
657+
if parameters.scheme then
658+
local scheme = parameters.scheme
659+
assert(scheme == 'redis' or scheme == 'tcp', 'invalid scheme: '..scheme)
660+
end
661+
perform_connection, driver = connect_tcp, socket.tcp
662+
end
663+
664+
return perform_connection(driver(), parameters)
665+
end
666+
617667
function connect(...)
618-
local args = {...}
619-
local host, port = defaults.host, defaults.port
620-
local tcp_nodelay = defaults.tcp_nodelay
668+
local args, parameters = {...}, nil
621669

622670
if #args == 1 then
623671
if type(args[1]) == 'table' then
624-
host = args[1].host or defaults.host
625-
port = args[1].port or defaults.port
626-
if args[1].tcp_nodelay ~= nil then
627-
tcp_nodelay = args[1].tcp_nodelay == true
628-
end
672+
parameters = args[1]
629673
else
630-
local server = uri.parse(select(1, ...))
631-
if server.scheme then
632-
assert(server.scheme == 'redis', '"'..server.scheme..'" is an invalid scheme')
633-
host, port = server.host, server.port or defaults.port
634-
if server.query then
635-
for k,v in server.query:gmatch('([-_%w]+)=([-_%w]+)') do
674+
local uri = require('socket.url')
675+
parameters = uri.parse(select(1, ...))
676+
if parameters.scheme then
677+
if parameters.query then
678+
for k, v in parameters.query:gmatch('([-_%w]+)=([-_%w]+)') do
636679
if k == 'tcp_nodelay' or k == 'tcp-nodelay' then
637-
tcp_nodelay = parse_boolean(v)
638-
if tcp_nodelay == nil then
639-
tcp_nodelay = defaults.tcp_nodelay
640-
end
680+
parameters.tcp_nodelay = parse_boolean(v)
641681
end
642682
end
643683
end
644684
else
645-
host, port = server.path, defaults.port
685+
parameters.host = parameters.path
646686
end
647687
end
648688
elseif #args > 1 then
649-
host, port = unpack(args)
689+
local host, port = unpack(args)
690+
parameters = { host = host, port = port }
650691
end
651692

652-
assert(host, 'please specify the address of running redis instance')
653-
local client_socket = socket.connect(host, tonumber(port))
654-
assert(client_socket, 'could not connect to ' .. host .. ':' .. port)
655-
client_socket:setoption('tcp-nodelay', tcp_nodelay)
693+
local socket = create_connection(merge_defaults(parameters))
694+
local client = create_client(client_prototype, socket, commands)
656695

657-
return create_client(client_prototype, client_socket, commands)
696+
return client
658697
end
659698

660699
-- ############################################################################

0 commit comments

Comments
 (0)
Please sign in to comment.