Skip to content
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

Allow alternative coroutine implementations #41

Open
wants to merge 4 commits into
base: version-2.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion rockspec/redis-lua-scm-1.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package = "redis-lua"
version = "scm-1"

source = {
url = "git://github.com/nrk/redis-lua.git"
url = "git://github.com/CosyVerif/redis-lua.git"
}

description = {
Expand Down
29 changes: 16 additions & 13 deletions src/redis.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ local defaults = {
port = 6379,
tcp_nodelay = true,
path = nil,
coroutine = coroutine,
}

local function merge_defaults(parameters)
Expand Down Expand Up @@ -282,17 +283,18 @@ local function load_methods(proto, commands)
return client
end

local function create_client(proto, client_socket, commands)
local function create_client(proto, parameters, commands)
local client = load_methods(proto, commands)
client.error = redis.error
client.network = {
socket = client_socket,
socket = parameters.socket,
read = network.read,
write = network.write,
}
client.requests = {
multibulk = request.multibulk,
}
client.coroutine = parameters.coroutine
return client
end

Expand Down Expand Up @@ -571,7 +573,7 @@ do
end
end

return coroutine.wrap(function()
return client.coroutine.wrap(function()
while true do
local message
local response = response.read(client)
Expand Down Expand Up @@ -601,7 +603,7 @@ do
if aborting and subscriptions == 0 then
break
end
coroutine.yield(message, abort)
client.coroutine.yield(message, abort)
end
end)
end
Expand All @@ -627,7 +629,7 @@ do

local function initialize_transaction(client, options, block, queued_parsers)
local table_insert = table.insert
local coro = coroutine.create(block)
local coro = client.coroutine.create(block)

if options.watch then
local watch_keys = {}
Expand All @@ -644,13 +646,13 @@ do
client.error('cannot use EXEC inside a transaction block')
end
transaction_client.multi = function(...)
coroutine.yield()
client.coroutine.yield()
end
transaction_client.commands_queued = function()
return #queued_parsers
end

assert(coroutine.resume(coro, transaction_client))
assert(client.coroutine.resume(coro, transaction_client))

transaction_client.multi = nil
transaction_client.discard = function(...)
Expand Down Expand Up @@ -690,8 +692,8 @@ do
local coro = initialize_transaction(client, options, coroutine_block, queued_parsers)

local success, retval
if coroutine.status(coro) == 'suspended' then
success, retval = coroutine.resume(coro)
if client.coroutine.status(coro) == 'suspended' then
success, retval = client.coroutine.resume(coro)
else
-- do not fail if the coroutine has not been resumed (missing t:multi() with CAS)
success, retval = true, 'empty transaction'
Expand Down Expand Up @@ -768,7 +770,7 @@ do
monitoring = false
end

return coroutine.wrap(function()
return client.coroutine.wrap(function()
client:monitor()

while monitoring do
Expand All @@ -790,7 +792,7 @@ do
client.error('Unable to match MONITOR payload: '..response)
end

coroutine.yield(message, abort)
client.coroutine.yield(message, abort)
end
end)
end
Expand Down Expand Up @@ -884,8 +886,9 @@ function redis.connect(...)
redis.error('invalid type for the commands table')
end

local socket = create_connection(merge_defaults(parameters))
local client = create_client(client_prototype, socket, commands)
parameters = merge_defaults(parameters)
parameters.socket = create_connection(parameters)
local client = create_client(client_prototype, parameters, commands)

return client
end
Expand Down
10 changes: 10 additions & 0 deletions test/test_client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,16 @@ context("Client initialization", function()
assert_true(client:ping())
end)

test("Can use an alternative coroutine implementation", function()
local connection = require('socket').tcp()
connection:connect(settings.host, settings.port)
local coroutine = "alternative"

local client = redis.connect({ socket = connection, coroutine = coroutine })
assert_type(client, 'table')
assert_true(client.coroutine == coroutine)
end)

test("Can specify a timeout for connecting", function()
local time, timeout = os.time(), 2;

Expand Down