@@ -12,11 +12,12 @@ const NOCHANNEL = NoChannel()
1212
1313
1414Base. isopen (req:: NoChannel ) = false
15- Base. isempty (req:: NoChannel ) = true
15+ Base. isempty (req:: NoChannel ) = true
1616Base. put! (req:: NoChannel , :: IOBuffer ) = false
1717Base. take! (req:: NoChannel ) = nothing
1818Base. close (req:: NoChannel ) = false
19- Base. iterate (req:: NoChannel ) = Iterators. Stateful (Iterators. flatten (Iterators. repeated (nothing , 0 )))
19+ Base. iterate (req:: NoChannel ) =
20+ Iterators. Stateful (Iterators. flatten (Iterators. repeated (nothing , 0 )))
2021
2122
2223function write_callback (
@@ -197,8 +198,8 @@ mutable struct gRPCRequest
197198 response:: IOBuffer
198199
199200 # These are only used when the request or response is streaming
200- request_c:: Union{Channel{IOBuffer}, NoChannel}
201- response_c:: Union{Channel{IOBuffer}, NoChannel}
201+ request_c:: Union{Channel{IOBuffer},NoChannel}
202+ response_c:: Union{Channel{IOBuffer},NoChannel}
202203
203204 # The task making the request can block on this until the request is complete
204205 ready:: Event
@@ -231,13 +232,20 @@ mutable struct gRPCRequest
231232 url:: String ,
232233 request:: IOBuffer ,
233234 response:: IOBuffer ,
234- request_c:: Union{Channel{IOBuffer}, NoChannel} ,
235- response_c:: Union{Channel{IOBuffer}, NoChannel} ;
235+ request_c:: Union{Channel{IOBuffer},NoChannel} ,
236+ response_c:: Union{Channel{IOBuffer},NoChannel} ;
236237 deadline = 10 ,
237238 keepalive = 60 ,
238239 max_send_message_length = 4 * 1024 * 1024 ,
239240 max_recieve_message_length = 4 * 1024 * 1024 ,
240241 )
242+ ! isready (grpc) && throw (
243+ gRPCServiceCallException (
244+ GRPC_FAILED_PRECONDITION,
245+ " gRPCCURL backend is not running, did you forget to call grpc_init()?" ,
246+ ),
247+ )
248+
241249 # If the grpc handle is shutdown avoid acquiring the request semaphore and immediately throw an exception
242250 if ! grpc. running
243251 throw (
@@ -259,8 +267,12 @@ mutable struct gRPCRequest
259267 # curl_easy_setopt(easy_handle, CURLOPT_VERBOSE, UInt32(1))
260268
261269 curl_easy_setopt (easy_handle, CURLOPT_URL, url)
262- curl_easy_setopt (easy_handle, CURLOPT_TIMEOUT_MS, Clong (ceil (1000 * deadline)))
263- curl_easy_setopt (easy_handle, CURLOPT_CONNECTTIMEOUT_MS, Clong (ceil (1000 * deadline)))
270+ curl_easy_setopt (easy_handle, CURLOPT_TIMEOUT_MS, Clong (ceil (1000 * deadline)))
271+ curl_easy_setopt (
272+ easy_handle,
273+ CURLOPT_CONNECTTIMEOUT_MS,
274+ Clong (ceil (1000 * deadline)),
275+ )
264276 curl_easy_setopt (easy_handle, CURLOPT_PIPEWAIT, Clong (1 ))
265277 curl_easy_setopt (easy_handle, CURLOPT_POST, Clong (1 ))
266278 curl_easy_setopt (easy_handle, CURLOPT_CUSTOMREQUEST, " POST" )
@@ -702,30 +714,27 @@ mutable struct gRPCCURL
702714 running:: Bool
703715 requests:: Vector{gRPCRequest}
704716 # Allows for controlling the maximum number of concurrent gRPC requests/streams
705- events :: Channel{Event}
717+ sem :: Channel{Event}
706718
707- function gRPCCURL (max_streams:: Int = GRPC_MAX_STREAMS)
719+ function gRPCCURL (; max_streams:: Int = GRPC_MAX_STREAMS, running = true )
708720 grpc = new (
709721 Ptr {Cvoid} (0 ),
710722 ReentrantLock (),
711723 nothing ,
712724 Dict {curl_socket_t,CURLWatcher} (),
713725 ReentrantLock (),
714- true ,
726+ running ,
715727 Vector {gRPCRequest} (),
716728 Channel {Event} (max_streams),
717729 )
718730
719- # We use a channel as a Semaphore which also acts as a way to reuse Events to reduce allocations
720- for _ = 1 : max_streams
721- put! (grpc. events, Event ())
722- end
723-
724- preserve_handle (grpc)
731+ finalizer ((x) -> close (x), grpc)
725732
726- grpc_multi_init (grpc)
733+ # This is used for the global const gRPCCURL handle
734+ # The user is expected to call grpc_init() in order to use it
735+ ! running && return grpc
727736
728- finalizer ((x) -> close (x), grpc)
737+ open ( grpc)
729738
730739 return grpc
731740 end
@@ -774,9 +783,9 @@ function Base.open(grpc::gRPCCURL)
774783 grpc. watchers = Dict {curl_socket_t,CURLWatcher} ()
775784 end
776785
777- grpc. events = Channel {Event} (grpc. events . sz_max)
778- for _ = 1 : grpc. events . sz_max
779- put! (grpc. events , Event ())
786+ grpc. sem = Channel {Event} (grpc. sem . sz_max)
787+ for _ = 1 : grpc. sem . sz_max
788+ put! (grpc. sem , Event ())
780789 end
781790
782791 grpc. requests = Vector {gRPCRequest} ()
@@ -789,11 +798,13 @@ function Base.open(grpc::gRPCCURL)
789798 end
790799end
791800
792- max_reqs_dec (grpc:: gRPCCURL ) = take! (grpc. events)
801+ isready (grpc:: gRPCCURL ) = grpc. running
802+
803+ max_reqs_dec (grpc:: gRPCCURL ) = take! (grpc. sem)
793804function max_reqs_inc (grpc:: gRPCCURL , req:: gRPCRequest )
794805 # Reset before we recycle
795806 reset (req. curl_done_reading)
796- put! (grpc. events , req. curl_done_reading)
807+ put! (grpc. sem , req. curl_done_reading)
797808end
798809
799810function cleanup_request (grpc:: gRPCCURL , req:: gRPCRequest )
0 commit comments