Skip to content

Commit

Permalink
Merge pull request #34 from thibaultCha/fix/luasec-timeout
Browse files Browse the repository at this point in the history
fix(ssl) single settimeout call per socket
  • Loading branch information
thibaultcha committed Feb 3, 2016
2 parents 6412863 + f7186eb commit 9a7910b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
2 changes: 1 addition & 1 deletion spec/02-integration/08_ssl_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ desc("SSL", function()
}
}
assert.truthy(err)
assert.equal("SocketError", err.type)
assert.equal("SSLError", err.type)
assert.falsy(session)
end)

Expand Down
15 changes: 8 additions & 7 deletions src/cassandra.lua
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,9 @@ end
function Host:send(request)
request:set_version(self.protocol_version)

self:set_timeout(self.options.socket_options.read_timeout)

local frame_reader, err = send_and_receive(self, request)
if err then
if err == "timeout" then
if err == "timeout" or err == "wantread" then -- cosocket/luasocket or LuaSec timeout
return nil, Errors.TimeoutError(self.address)
else
return nil, Errors.SocketError(self.address, err)
Expand Down Expand Up @@ -271,10 +269,12 @@ function Host:connect()
return false, Errors.SocketError(self.address, err), true
end

self:set_timeout(self.options.socket_options.read_timeout)

if self.options.ssl_options.enabled then
ok, err = do_ssl_handshake(self)
if not ok then
return false, Errors.SocketError(self.address, err)
return false, Errors.SSLError(self.address, err)
end
end

Expand Down Expand Up @@ -314,7 +314,7 @@ function Host:connect()

local ok, err = send_auth(self, self.options.auth)
if err then
return nil, Errors.AuthenticationError(err)
return false, Errors.AuthenticationError(err)
elseif ok then
ready = true
end
Expand Down Expand Up @@ -382,7 +382,7 @@ function Host:set_keep_alive()
return true
end

if self.socket_type == "ngx" then
if self:can_keep_alive() then
-- tcpsock:setkeepalive() does not accept nil values, so this is a quick workaround
-- see https://github.com/openresty/lua-nginx-module/pull/625
local ok, err
Expand Down Expand Up @@ -624,7 +624,7 @@ function RequestHandler:send(request)
return self:handle_error(request, err)
end

-- Success! Make sure to re-up node in case it was marked as DOWN
-- Success! Make sure to re-up the node in case it was marked as DOWN
local ok, cache_err = self.coordinator:set_up()
if not ok then
return nil, cache_err
Expand Down Expand Up @@ -685,6 +685,7 @@ function RequestHandler:handle_error(request, err)
end

function RequestHandler:retry(request)
self.coordinator:close()
self.n_retries = self.n_retries + 1
log.info("Retrying request on next coordinator")
return self:send_on_next_coordinator(request)
Expand Down
5 changes: 4 additions & 1 deletion src/cassandra/errors.lua
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,17 @@ local ERROR_TYPES = {
return message.." for socket with peer "..address
end
},
SSLError = {
info = "Represents an error happening during the SSL handshake."
},
TimeoutError = {
info = "Represents a client-side error that is raised when the client didn't hear back from the server within {client_options.socket_options.read_timeout}.",
message = function(address)
return "timeout for peer "..address
end
},
AuthenticationError = {
info = "Represents an authentication error from the driver or from a Cassandra node."
info = "Represents an error happening during the authentication flow."
},
SharedDictError = {
info = "Represents an error with the lua_shared_dict in use.",
Expand Down

0 comments on commit 9a7910b

Please sign in to comment.