Skip to content

Commit

Permalink
Fixed busy subscription loop
Browse files Browse the repository at this point in the history
  • Loading branch information
belvedere-trading-user committed Jan 5, 2016
1 parent 3411e9c commit 98e8e60
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 12 deletions.
5 changes: 0 additions & 5 deletions src/client.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,6 @@ end
function subscription_loop(conn::SubscriptionConnection, err_callback::Function)
while is_connected(conn)
try
# Probably could do something better here, but we can't block
# forever or else subsequent subscribe commands on the same
# socket will block until a message is received
sleep(.1)
nb_available(conn.socket) > 0 || continue
l = getline(conn.socket)
reply = parseline(l, conn.socket)
message = SubscriptionMessage(reply)
Expand Down
4 changes: 2 additions & 2 deletions src/commands.jl
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ end
@redisfunction "publish" Integer channel message

function _subscribe(conn::SubscriptionConnection, channels::Array)
execute_command(conn, unshift!(channels, "subscribe"))
execute_command_without_reply(conn, unshift!(channels, "subscribe"))
end

function subscribe(conn::SubscriptionConnection, channel::AbstractString, callback::Function)
Expand All @@ -274,7 +274,7 @@ function unsubscribe(conn::SubscriptionConnection, channels...)
end

function _psubscribe(conn::SubscriptionConnection, patterns::Array)
execute_command(conn, unshift!(patterns, "psubscribe"))
execute_command_without_reply(conn, unshift!(patterns, "psubscribe"))
end

function psubscribe(conn::SubscriptionConnection, pattern::AbstractString, callback::Function)
Expand Down
10 changes: 5 additions & 5 deletions src/parser.jl
Original file line number Diff line number Diff line change
Expand Up @@ -76,18 +76,18 @@ function pack_command(command)
packed_command
end



function execute_command(conn::RedisConnectionBase, command)
function execute_command_without_reply(conn::RedisConnectionBase, command)
is_connected(conn) || throw(ConnectionException("Socket is disconnected"))
send_command(conn, pack_command(command))
end

function execute_command(conn::RedisConnectionBase, command)
execute_command_without_reply(conn, command)
l = getline(conn.socket)
reply = parseline(l, conn.socket)
return reply
end



baremodule SubscriptionMessageType
const Message = 0
const Pmessage = 1
Expand Down
1 change: 1 addition & 0 deletions test/redis_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ subs = open_subscription(conn, g)
x = Any[]
f(y) = push!(x, y)
subscribe(subs, "channel", f)
subscribe(subs, "duplicate", f)
@test publish(conn, "channel", "hello, world!") == 1
sleep(2)
@test x == ["hello, world!"]
Expand Down

0 comments on commit 98e8e60

Please sign in to comment.