1212struct gRPCStatus
1313 success:: Bool
1414 message:: String
15+ exception:: Union{Nothing,Exception}
16+ end
17+
18+ gRPCStatus (success:: Bool , message:: AbstractString ) = gRPCStatus (success, string (message), nothing )
19+ function gRPCStatus (status_future)
20+ try
21+ fetch (status_future)
22+ catch ex
23+ task_exception = isa (ex, TaskFailedException) ? ex. task. exception : ex
24+ while isa (task_exception, TaskFailedException)
25+ task_exception = task_exception. task. exception
26+ end
27+ gRPCStatus (false , string (task_exception), task_exception)
28+ end
1529end
1630
1731"""
18- struct gRPCException
32+ struct gRPCServiceCallException
1933 message::String
2034 end
2135
22- Every gRPC request returns the result and a future representing the status
23- of the gRPC request. Use the `gRPCCheck` method on the status future to check
24- the request status and throw a `gRPCException` if it is not successful.
25-
26- A `gRPCException` has the following members:
36+ A `gRPCServiceCallException` is thrown if a gRPC request is not successful.
37+ It has the following members:
2738
2839- `message`: any error message if request was not successful
2940"""
30- struct gRPCException <: Exception
41+ struct gRPCServiceCallException <: gRPCException
3142 message:: String
3243end
3344
45+ Base. show (io:: IO , m:: gRPCServiceCallException ) = print (io, " gRPCServiceCallException - $(m. message) " )
46+
3447"""
3548 gRPCCheck(status; throw_error::Bool=true)
3649
37- Check the response of a gRPC request and raise a `gRPCException` if it has
38- failed. If `throw_error` is set to false, returns `true` or `false` indicating
39- success instead.
50+ Every gRPC request returns the result and a future representing the status
51+ of the gRPC request. Check the response of a gRPC request and raise a
52+ `gRPCException` if it has failed. If `throw_error` is set to false, this
53+ returns `true` or `false` indicating success instead.
4054"""
41- gRPCCheck (status ; throw_error:: Bool = true ) = gRPCCheck (fetch (status ); throw_error= throw_error)
55+ gRPCCheck (status_future ; throw_error:: Bool = true ) = gRPCCheck (gRPCStatus (status_future ); throw_error= throw_error)
4256function gRPCCheck (status:: gRPCStatus ; throw_error:: Bool = true )
43- if throw_error
44- status. success || throw (gRPCException (status. message))
45- else
46- status. success
57+ if throw_error && ! status. success
58+ if status. exception === nothing
59+ throw (gRPCServiceCallException (status. message))
60+ else
61+ throw (status. exception)
62+ end
4763 end
64+ status. success
4865end
4966
5067"""
5572 [ revocation::Bool = true, ]
5673 [ request_timeout::Real = Inf, ]
5774 [ connect_timeout::Real = 0, ]
75+ [ max_message_length = DEFAULT_MAX_MESSAGE_LENGTH, ]
76+ [ max_recv_message_length = 0, ]
77+ [ max_send_message_length = 0, ]
5878 [ verbose::Bool = false, ]
5979 )
6080
@@ -70,6 +90,11 @@ Contains settings to control the behavior of gRPC requests.
7090- `request_timeout`: request timeout (seconds)
7191- `connect_timeout`: connect timeout (seconds) (default is 300 seconds, same
7292 as setting this to 0)
93+ - `max_message_length`: maximum message length (default is 4MB)
94+ - `max_recv_message_length`: maximum message length to receive (default is
95+ `max_message_length`, same as setting this to 0)
96+ - `max_send_message_length`: maximum message length to send (default is
97+ `max_message_length`, same as setting this to 0)
7398- `verbose`: whether to print out verbose communication logs (default false)
7499"""
75100struct gRPCController <: ProtoRpcController
@@ -79,6 +104,8 @@ struct gRPCController <: ProtoRpcController
79104 revocation:: Bool
80105 request_timeout:: Real
81106 connect_timeout:: Real
107+ max_recv_message_length:: Int
108+ max_send_message_length:: Int
82109 verbose:: Bool
83110
84111 function gRPCController (;
@@ -88,9 +115,18 @@ struct gRPCController <: ProtoRpcController
88115 revocation:: Bool = true ,
89116 request_timeout:: Real = Inf ,
90117 connect_timeout:: Real = 0 ,
118+ max_message_length:: Integer = DEFAULT_MAX_MESSAGE_LENGTH,
119+ max_recv_message_length:: Integer = 0 ,
120+ max_send_message_length:: Integer = 0 ,
91121 verbose:: Bool = false
92122 )
93- new (maxage, keepalive, negotiation, revocation, request_timeout, connect_timeout, verbose)
123+ if maxage < 0 || keepalive < 0 || request_timeout < 0 || connect_timeout < 0 ||
124+ max_message_length < 0 || max_recv_message_length < 0 || max_send_message_length < 0
125+ throw (ArgumentError (" Invalid gRPCController parameter" ))
126+ end
127+ (max_recv_message_length == 0 ) && (max_recv_message_length = max_message_length)
128+ (max_send_message_length == 0 ) && (max_send_message_length = max_message_length)
129+ new (maxage, keepalive, negotiation, revocation, request_timeout, connect_timeout, max_recv_message_length, max_send_message_length, verbose)
94130 end
95131end
96132
@@ -118,13 +154,15 @@ struct gRPCChannel <: ProtoRpcChannel
118154 end
119155end
120156
121- function to_delimited_message_bytes (msg)
157+ function to_delimited_message_bytes (msg, max_message_length :: Int )
122158 iob = IOBuffer ()
123- write (iob, UInt8 (0 )) # compression
124- write (iob, hton (UInt32 (0 ))) # message length (placeholder)
125- data_len = writeproto (iob, msg) # message bytes
126- seek (iob, 1 ) # seek out the message length placeholder
127- write (iob, hton (UInt32 (data_len))) # fill the message length
159+ limitiob = LimitIO (iob, max_message_length)
160+ write (limitiob, UInt8 (0 )) # compression
161+ write (limitiob, hton (UInt32 (0 ))) # message length (placeholder)
162+ data_len = writeproto (limitiob, msg) # message bytes
163+
164+ seek (iob, 1 ) # seek out the message length placeholder
165+ write (iob, hton (UInt32 (data_len))) # fill the message length
128166 take! (iob)
129167end
130168
@@ -155,6 +193,8 @@ function call_method(channel::gRPCChannel, service::ServiceDescriptor, method::M
155193 revocation = controller. revocation,
156194 request_timeout = controller. request_timeout,
157195 connect_timeout = controller. connect_timeout,
196+ max_recv_message_length = controller. max_recv_message_length,
197+ max_send_message_length = controller. max_send_message_length,
158198 verbose = controller. verbose,
159199 )
160200 outchannel, status_future
0 commit comments