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

new low_speed_limit and low_speed_time options #23

Closed
wants to merge 5 commits into from
Closed
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
23 changes: 20 additions & 3 deletions src/curl.jl
Original file line number Diff line number Diff line change
Expand Up @@ -160,15 +160,29 @@ function recv_data(easy::Curl.Easy, output::Channel{T}, max_recv_message_length:
close(output)
end

function set_low_speed_limits(easy::Curl.Easy, low_speed_limit, low_speed_time)
low_speed_limit >= 0 ||
throw(ArgumentError("`low_speed_limit` must be non-negative, got $(low_speed_limit)."))
low_speed_time >= 0 ||
throw(ArgumentError("`low_speed_time` must be non-negative, got $(low_speed_time)."))

_max = typemax(Clong) ÷ 1000
low_speed_limit = low_speed_limit <= _max ? round(Clong, low_speed_limit) : _max
low_speed_time = low_speed_time <= _max ? round(Clong, low_speed_time) : _max

Curl.setopt(easy, CURLOPT_LOW_SPEED_LIMIT, low_speed_limit)
Curl.setopt(easy, CURLOPT_LOW_SPEED_TIME, low_speed_time)
return nothing
end

function set_connect_timeout(easy::Curl.Easy, timeout::Real)
timeout >= 0 ||
throw(ArgumentError("timeout must be positive, got $timeout"))
if timeout ≤ typemax(Clong) ÷ 1000
timeout_ms = round(Clong, timeout * 1000)
Curl.setopt(easy, CURLOPT_CONNECTTIMEOUT_MS, timeout_ms)
else
timeout = timeout ≤ typemax(Clong) ? round(Clong, timeout) : Clong(0)
Curl.setopt(easy, CURLOPT_CONNECTTIMEOUT, timeout)
Curl.setopt(easy, CURLOPT_CONNECTTIMEOUT, Clong(0))
Copy link
Member

@tanmaykm tanmaykm Jul 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would disregard the specified timeout value. Was this intentional?

Ah, didn't notice the explanation here. Yes, this looks correct.

end
end

Expand All @@ -181,12 +195,15 @@ function grpc_request(downloader::Downloader, url::String, input::Channel{T1}, o
connect_timeout::Real = 0,
max_recv_message_length::Int = DEFAULT_MAX_RECV_MESSAGE_LENGTH,
max_send_message_length::Int = DEFAULT_MAX_SEND_MESSAGE_LENGTH,
verbose::Bool = false)::gRPCStatus where {T1 <: ProtoType, T2 <: ProtoType}
verbose::Bool = false,
low_speed_limit::Int = 0,
low_speed_time::Int = 0)::gRPCStatus where {T1 <: ProtoType, T2 <: ProtoType}
Curl.with_handle(easy_handle(maxage, keepalive, negotiation, revocation, request_timeout)) do easy
# setup the request
Curl.set_url(easy, url)
Curl.set_timeout(easy, request_timeout)
set_connect_timeout(easy, connect_timeout)
set_low_speed_limits(easy, low_speed_limit, low_speed_time)
Curl.set_verbose(easy, verbose)
Curl.add_upload_callbacks(easy)
Downloads.set_ca_roots(downloader, easy)
Expand Down
21 changes: 17 additions & 4 deletions src/grpc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ end
[ max_recv_message_length = 0, ]
[ max_send_message_length = 0, ]
[ verbose::Bool = false, ]
[ low_speed_limit = 0, ]
[ low_speed_time = 0, ]
)

Contains settings to control the behavior of gRPC requests.
Expand All @@ -98,6 +100,10 @@ Contains settings to control the behavior of gRPC requests.
- `max_send_message_length`: maximum message length to send (default is
`max_message_length`, same as setting this to 0)
- `verbose`: whether to print out verbose communication logs (default false)
- `low_speed_limit`: speed in Bytes per second below which a connection is
considered slow (default is 0 and disables the setting)
- `low_speed_time`: duration in seconds for which a slow connection is tolerated
(default is 0 and disables the setting)
"""
struct gRPCController <: ProtoRpcController
maxage::Clong
Expand All @@ -109,7 +115,8 @@ struct gRPCController <: ProtoRpcController
max_recv_message_length::Int
max_send_message_length::Int
verbose::Bool

low_speed_limit::Int
low_speed_time::Int
function gRPCController(;
maxage::Integer = 0,
keepalive::Integer = 60,
Expand All @@ -120,15 +127,19 @@ struct gRPCController <: ProtoRpcController
max_message_length::Integer = DEFAULT_MAX_MESSAGE_LENGTH,
max_recv_message_length::Integer = 0,
max_send_message_length::Integer = 0,
verbose::Bool = false
verbose::Bool = false,
low_speed_limit::Integer = 0,
low_speed_time::Integer = 0,
)
if maxage < 0 || keepalive < 0 || request_timeout < 0 || connect_timeout < 0 ||
max_message_length < 0 || max_recv_message_length < 0 || max_send_message_length < 0
max_message_length < 0 || max_recv_message_length < 0 || max_send_message_length < 0 ||
low_speed_limit < 0 || low_speed_time < 0
throw(ArgumentError("Invalid gRPCController parameter"))
end
(max_recv_message_length == 0) && (max_recv_message_length = max_message_length)
(max_send_message_length == 0) && (max_send_message_length = max_message_length)
new(maxage, keepalive, negotiation, revocation, request_timeout, connect_timeout, max_recv_message_length, max_send_message_length, verbose)
return new(maxage, keepalive, negotiation, revocation, request_timeout, connect_timeout,
max_recv_message_length, max_send_message_length, verbose,low_speed_limit,low_speed_time)
end
end

Expand Down Expand Up @@ -203,6 +214,8 @@ function call_method(channel::gRPCChannel, service::ServiceDescriptor, method::M
max_recv_message_length = controller.max_recv_message_length,
max_send_message_length = controller.max_send_message_length,
verbose = controller.verbose,
low_speed_limit = controller.low_speed_limit,
low_speed_time = controller.low_speed_time,
)
outchannel, status_future
end