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

support nonblocking behavior. #31

Closed
littleboss opened this issue Dec 28, 2013 · 2 comments
Closed

support nonblocking behavior. #31

littleboss opened this issue Dec 28, 2013 · 2 comments

Comments

@littleboss
Copy link

i check the lua redis client in https://github.com/agentzh/lua-resty-redis, it support 100% nonblocking behavior. because it uses cosocket api,

dose this client api support non blocking

thanks

@nrk
Copy link
Owner

nrk commented Jan 13, 2014

Hi @littleboss,

This is a blocking client implementation and currently there are no plans to add a non-blocking interface to it (to be honest I've never experimented with cosocket) as it's not a priority. Things may change in this respect with help coming from other developers though.

@AMD-NICK
Copy link

You can make a non-blocking (asynchronous) implementation using copas. It will look like this ( example with benchmark):

local redis  = require("redis")
local copas  = require("copas")
local socket = require("socket")

local get_connection = function(host, port, pass)
	local sock = socket.tcp()
	sock = copas.wrap(sock)
	sock:connect(host, port or 6379)

	local client = redis.connect({
		socket = sock,
	})
	client:auth(pass)
	return client
end

local now = socket.gettime
local start = now()
local reps = 1000
for i = 1, reps do

	copas.addthread(function()
		local host, port, pass = "redis", 6379, "redis-pass"
		local client = get_connection(host, port, pass)

		print(i, client:incr("bench") )
		reps = reps - 1
		if reps == 0 then
			print("finished in " .. (now() - start) .. " seconds") -- 0.18 seconds for 1000 requests
		end
	end)
end

copas.loop()

⚠️ Yes, we making a new connection for every request, but you always can implement connection pool with copas.queue and copas.semaphore like I done it here: click, so you will not have to create new connections for every request

Here is the benchmark for the default implementation:

local redis = require("redis")

local host, port, pass = "redis", 6379, "redis_pass"

local client = redis.connect(host, port or 6379)
client:auth(pass)

local now = require("socket").gettime
local start = now()
for _ = 1, 1000 do
	print( client:incr("bench1") )
end
print("finished in " .. (now() - start) .. " seconds") -- 11.95 seconds

As you can see, 1000 queries in the synchronous implementation are executed in 11 seconds, while "async" queries are executed in 0.18 seconds

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants