Skip to content

Commit 8f346f8

Browse files
authored
Add __init__ function at package level to reduce boilerplate for using the library / doing codegen (#100)
1 parent 016bc66 commit 8f346f8

6 files changed

Lines changed: 40 additions & 67 deletions

File tree

docs/src/index.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,6 @@ gRPCClient.jl integrates with ProtoBuf.jl to automatically generate Julia client
3939
using ProtoBuf
4040
using gRPCClient
4141

42-
# Register our service codegen with ProtoBuf.jl
43-
grpc_register_service_codegen()
44-
4542
# Creates Julia bindings for the messages and RPC defined in test.proto
4643
protojl("test/proto/test.proto", ".", "test/gen")
4744
```

src/Curl.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -721,7 +721,7 @@ mutable struct gRPCCURL
721721
finalizer((x) -> close(x), grpc)
722722

723723
# This is used for the global const gRPCCURL handle
724-
# The user is expected to call grpc_init() in order to use it
724+
# grpc_init() is called automatically via __init__() when the package is loaded
725725
!running && return grpc
726726

727727
open(grpc)

src/Streaming.jl

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -115,27 +115,24 @@ Start a client streaming gRPC request (multiple requests, single response).
115115
```julia
116116
using gRPCClient
117117
118-
# Step 1: Initialize gRPC
119-
grpc_init()
120-
121-
# Step 2: Include generated Protocol Buffer bindings
118+
# Step 1: Include generated Protocol Buffer bindings
122119
include("test/gen/test/test_pb.jl")
123120
124-
# Step 3: Create a client
121+
# Step 2: Create a client
125122
client = TestService_TestClientStreamRPC_Client("localhost", 8001)
126123
127-
# Step 4: Create a request channel and send requests
124+
# Step 3: Create a request channel and send requests
128125
request_c = Channel{TestRequest}(16)
129126
put!(request_c, TestRequest(1, zeros(UInt64, 1)))
130127
131-
# Step 5: Initiate the streaming request
128+
# Step 4: Initiate the streaming request
132129
req = grpc_async_request(client, request_c)
133130
134-
# Step 6: Close the channel to signal no more requests will be sent
131+
# Step 5: Close the channel to signal no more requests will be sent
135132
# (the server won't respond until the stream ends)
136133
close(request_c)
137134
138-
# Step 7: Wait for the single response
135+
# Step 6: Wait for the single response
139136
test_response = grpc_async_await(client, req)
140137
```
141138
"""
@@ -175,27 +172,24 @@ Start a server streaming gRPC request (single request, multiple responses).
175172
```julia
176173
using gRPCClient
177174
178-
# Step 1: Initialize gRPC
179-
grpc_init()
180-
181-
# Step 2: Include generated Protocol Buffer bindings
175+
# Step 1: Include generated Protocol Buffer bindings
182176
include("test/gen/test/test_pb.jl")
183177
184-
# Step 3: Create a client
178+
# Step 2: Create a client
185179
client = TestService_TestServerStreamRPC_Client("localhost", 8001)
186180
187-
# Step 4: Create a response channel to receive multiple responses
181+
# Step 3: Create a response channel to receive multiple responses
188182
response_c = Channel{TestResponse}(16)
189183
190-
# Step 5: Send a single request (the server will respond with multiple messages)
184+
# Step 4: Send a single request (the server will respond with multiple messages)
191185
req = grpc_async_request(client, TestRequest(1, zeros(UInt64, 1)), response_c)
192186
193-
# Step 6: Process streaming responses (channel closes when server finishes)
187+
# Step 5: Process streaming responses (channel closes when server finishes)
194188
for test_response in response_c
195189
@info test_response
196190
end
197191
198-
# Step 7: Check for exceptions
192+
# Step 6: Check for exceptions
199193
grpc_async_await(req)
200194
```
201195
"""
@@ -242,33 +236,30 @@ Start a bidirectional streaming gRPC request (multiple requests, multiple respon
242236
```julia
243237
using gRPCClient
244238
245-
# Step 1: Initialize gRPC
246-
grpc_init()
247-
248-
# Step 2: Include generated Protocol Buffer bindings
239+
# Step 1: Include generated Protocol Buffer bindings
249240
include("test/gen/test/test_pb.jl")
250241
251-
# Step 3: Create a client
242+
# Step 2: Create a client
252243
client = TestService_TestBidirectionalStreamRPC_Client("localhost", 8001)
253244
254-
# Step 4: Create request and response channels (streaming in both directions simultaneously)
245+
# Step 3: Create request and response channels (streaming in both directions simultaneously)
255246
request_c = Channel{TestRequest}(16)
256247
response_c = Channel{TestResponse}(16)
257248
258-
# Step 5: Initiate the bidirectional streaming request
249+
# Step 4: Initiate the bidirectional streaming request
259250
req = grpc_async_request(client, request_c, response_c)
260251
261-
# Step 6: Send requests and receive responses concurrently
252+
# Step 5: Send requests and receive responses concurrently
262253
put!(request_c, TestRequest(1, zeros(UInt64, 1)))
263254
for test_response in response_c
264255
@info test_response
265256
break # Exit after first response for this example
266257
end
267258
268-
# Step 7: Close the request channel to signal no more requests will be sent
259+
# Step 6: Close the request channel to signal no more requests will be sent
269260
close(request_c)
270261
271-
# Step 8: Check for exceptions
262+
# Step 7: Check for exceptions
272263
grpc_async_await(req)
273264
```
274265
"""

src/Unary.jl

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,19 @@ This is ideal when you need to send many requests in parallel and waiting on eac
1010
```julia
1111
using gRPCClient
1212
13-
# Step 1: Initialize gRPC (must be called once before making any gRPC requests)
14-
grpc_init()
15-
16-
# Step 2: Include generated Protocol Buffer bindings
13+
# Step 1: Include generated Protocol Buffer bindings
1714
include("test/gen/test/test_pb.jl")
1815
19-
# Step 3: Create a client for your RPC method (hostname, port)
16+
# Step 2: Create a client for your RPC method (hostname, port)
2017
client = TestService_TestRPC_Client("localhost", 8001)
2118
22-
# Step 4: Send all requests without waiting for responses
19+
# Step 3: Send all requests without waiting for responses
2320
requests = Vector{gRPCRequest}()
2421
for i in 1:10
2522
push!(requests, grpc_async_request(client, TestRequest(1, zeros(UInt64, 1))))
2623
end
2724
28-
# Step 5: Wait for and process responses
25+
# Step 4: Wait for and process responses
2926
for request in requests
3027
response = grpc_async_await(client, request)
3128
@info response
@@ -79,25 +76,22 @@ This has the advantage over the request / await patern in that you can handle re
7976
```julia
8077
using gRPCClient
8178
82-
# Step 1: Initialize gRPC
83-
grpc_init()
84-
85-
# Step 2: Include generated Protocol Buffer bindings
79+
# Step 1: Include generated Protocol Buffer bindings
8680
include("test/gen/test/test_pb.jl")
8781
88-
# Step 3: Create a client
82+
# Step 2: Create a client
8983
client = TestService_TestRPC_Client("localhost", 8001)
9084
91-
# Step 4: Create a channel to receive responses (processes responses as they arrive, in any order)
85+
# Step 3: Create a channel to receive responses (processes responses as they arrive, in any order)
9286
N = 10
9387
channel = Channel{gRPCAsyncChannelResponse{TestResponse}}(N)
9488
95-
# Step 5: Send all requests (the index tracks which response corresponds to which request)
89+
# Step 4: Send all requests (the index tracks which response corresponds to which request)
9690
for (index, request) in enumerate([TestRequest(i, zeros(UInt64, i)) for i in 1:N])
9791
grpc_async_request(client, request, channel, index)
9892
end
9993
100-
# Step 6: Process responses as they arrive
94+
# Step 5: Process responses as they arrive
10195
for i in 1:N
10296
cr = take!(channel)
10397
!isnothing(cr.ex) && throw(cr.ex)
@@ -169,16 +163,13 @@ Use this when you want the simplest possible interface for a single request.
169163
```julia
170164
using gRPCClient
171165
172-
# Step 1: Initialize gRPC
173-
grpc_init()
174-
175-
# Step 2: Include generated Protocol Buffer bindings
166+
# Step 1: Include generated Protocol Buffer bindings
176167
include("test/gen/test/test_pb.jl")
177168
178-
# Step 3: Create a client
169+
# Step 2: Create a client
179170
client = TestService_TestRPC_Client("localhost", 8001)
180171
181-
# Step 4: Make a synchronous request (blocks until response is ready)
172+
# Step 3: Make a synchronous request (blocks until response is ready)
182173
response = grpc_sync_request(client, TestRequest(1, zeros(UInt64, 1)))
183174
@info response
184175
```

src/gRPC.jl

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,17 @@ grpc_global_handle() = _grpc
1010
"""
1111
grpc_init([grpc_curl::gRPCCURL])
1212
13-
Initializes the `gRPCCURL` object. This should be called once before making gRPC calls. There is no harm in calling this more than once (ie by different packages/dependencies). Typical usage looks like this:
14-
15-
```julia
16-
grpc_init()
17-
18-
client = TestService_TestRPC_Client("172.238.177.88", 8001)
19-
20-
# Make some gRPC calls
21-
22-
# Shut down the global gRPC handle
23-
grpc_shutdown()
24-
```
13+
Initializes the `gRPCCURL` object. The global handle is initialized automatically when the package is loaded. There is no harm in calling this more than once (ie by different packages/dependencies).
2514
2615
Unless specifying a `gRPCCURL` the global one provided by `grpc_global_handle()` is used. Each `gRPCCURL` state has its own connection pool and request semaphore, so sometimes you may want to manage your own like shown below:
2716
28-
```julia
17+
```julia
2918
grpc_myapp = gRPCCURL()
3019
grpc_init(grpc_myapp)
3120
3221
client = TestService_TestRPC_Client("172.238.177.88", 8001; grpc=grpc_myapp)
3322
34-
# Make some gRPC calls
23+
# Make some gRPC calls
3524
3625
# Only shuts down your gRPC handle
3726
grpc_shutdown(grpc_myapp)

src/gRPCClient.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,9 @@ export gRPCAsyncChannelResponse
111111
export gRPCException
112112
export gRPCServiceCallException
113113

114+
function __init__()
115+
grpc_init()
116+
grpc_register_service_codegen()
117+
end
118+
114119
end

0 commit comments

Comments
 (0)