Skip to content

Commit

Permalink
optionally set the TCP_NODELAY flag
Browse files Browse the repository at this point in the history
Fixes issue #38.
  • Loading branch information
erikedin committed Mar 11, 2018
1 parent c010b85 commit b37160a
Showing 1 changed file with 30 additions and 5 deletions.
35 changes: 30 additions & 5 deletions src/client.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ show(io::IO, c::WSClient) =
Note: As of right now the handshake is not validated, because the response headers aren't set here.
"
function connection_result_(client::WSClient, result::HandshakeResult, handler::WebSocketHandler)
function connection_result_(client::WSClient,
result::HandshakeResult,
handler::WebSocketHandler,
fix_small_message_latency::Bool)
# Requirement
# @4_1_P5 Waiting for a handshake response
#
Expand All @@ -68,6 +71,10 @@ function connection_result_(client::WSClient, result::HandshakeResult, handler::
return false
end

if fix_small_message_latency
ccall(:uv_tcp_nodelay, Cint, (Ptr{Void}, Cint), result.stream, 1)
end

connection = get(client.connection)

# For `writer` the target object is the IO stream for the WebSocket connection.
Expand Down Expand Up @@ -109,7 +116,10 @@ function connection_result_(client::WSClient, result::HandshakeResult, handler::
end

"The HTTP Upgrade failed, for whatever reason."
function connection_result_(client::WSClient, result::HandshakeFailure, handler::WebSocketHandler)
function connection_result_(client::WSClient,
result::HandshakeFailure,
handler::WebSocketHandler,
fix_small_message_latency::Bool)
# Requirement
# @4_1_EstablishConnection_4 Could not open the connection
# @4_1_EstablishConnection_5-2 TLS Connection fails
Expand All @@ -119,8 +129,23 @@ function connection_result_(client::WSClient, result::HandshakeFailure, handler:
false
end

"Connect the client to a WebSocket server at `uri`, and use `handler` for the callbacks."
function wsconnect(client::WSClient, uri::URI, handler::WebSocketHandler)
"""
Connect the client to a WebSocket server at `uri`, and use `handler` for the callbacks.
# Arguments
- `fix_small_message_latency::Bool = false`: Set the TCP_NODELAY flag to improve small message latency.
# Fix small message latency
The TCP protocol can buffer small messages (1448 bytes and smaller). The reason is that this reduces
the overhead when sending large amounts of small packets. However, it also means that latency can be
much higher for small messages. This buffering can be disabled by setting a flag TCP_NODELAY.
If your application will send and receive primarily small messages (1448 bytes or smaller), and it
is sensitive to latency, then set `fix_small_message_latency` to true. This sets the TCP_NODELAY
flag, and latency may be improved.
"""
function wsconnect(client::WSClient, uri::URI, handler::WebSocketHandler;
fix_small_message_latency=false)
handler_proxy = WebSocketsHandlerProxy(handler)

# Requirement
Expand All @@ -142,7 +167,7 @@ function wsconnect(client::WSClient, uri::URI, handler::WebSocketHandler)
# protocol.
handshake_result = client.do_handshake(get(client.connection).rng, new_uri)

connection_result_(client, handshake_result, handler_proxy)
connection_result_(client, handshake_result, handler_proxy, fix_small_message_latency)
end

# Requirement
Expand Down

0 comments on commit b37160a

Please sign in to comment.