Skip to content

Commit 5f5cb80

Browse files
committed
Add cancellation to go client
1 parent 0d1d1b6 commit 5f5cb80

File tree

1 file changed

+20
-11
lines changed
  • src/BenchmarksApps/Grpc/GoClient

1 file changed

+20
-11
lines changed

src/BenchmarksApps/Grpc/GoClient/main.go

+20-11
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ var (
3030
streams = flag.Int("streams", 1, "Streams per connection")
3131
warmup = flag.Int("warmup", 5, "Warmup in seconds")
3232
duration = flag.Int("duration", 10, "Duration in seconds")
33+
ctx, cancel = context.WithCancel(context.Background())
3334
warmupWg sync.WaitGroup
3435
finishedWg sync.WaitGroup
3536
connections []*grpc.ClientConn
@@ -98,6 +99,8 @@ func main() {
9899
fmt.Print("Finished warming up\n")
99100
time.Sleep(time.Duration(*duration) * time.Second)
100101
stopped = true
102+
cancel()
103+
fmt.Print("Stopping benchmarks\n")
101104
}()
102105

103106
// Start caller threads for each connection + stream
@@ -107,6 +110,8 @@ func main() {
107110
// Wait for caller threads to finish
108111
finishedWg.Wait()
109112

113+
fmt.Print("Caller threads finished\n")
114+
110115
// Output results
111116
calculateRequestStatistics()
112117
calculateLatencyStatistics()
@@ -239,8 +244,8 @@ func makeCaller(cc *grpc.ClientConn, connectionID int, streamID int, scenario st
239244
}
240245

241246
start := time.Now()
242-
if _, err := client.UnaryCall(context.Background(), request); err != nil {
243-
handleFailure(connectionID)
247+
if _, err := client.UnaryCall(ctx, request); err != nil {
248+
handleFailure(connectionID, err)
244249
} else {
245250
end := time.Now()
246251
handleSuccess(connectionID, start, end)
@@ -259,18 +264,18 @@ func makeCaller(cc *grpc.ClientConn, connectionID int, streamID int, scenario st
259264
ResponseSize: int32(*responseSize),
260265
}
261266

262-
stream, err := client.StreamingFromServer(context.Background(), request)
267+
stream, err := client.StreamingFromServer(ctx, request)
263268
if err != nil {
264269
// Wait for warmup to be finished before reporting the call failed
265270
warmupWg.Wait()
266-
handleFailure(connectionID)
271+
handleFailure(connectionID, err)
267272
return
268273
}
269274

270275
for {
271276
start := time.Now()
272277
if _, err := stream.Recv(); err != nil {
273-
handleFailure(connectionID)
278+
handleFailure(connectionID, err)
274279
} else {
275280
end := time.Now()
276281
handleSuccess(connectionID, start, end)
@@ -284,11 +289,11 @@ func makeCaller(cc *grpc.ClientConn, connectionID int, streamID int, scenario st
284289
}
285290
if scenario == "pingpongstreaming" {
286291
return func() {
287-
stream, err := client.StreamingCall(context.Background())
292+
stream, err := client.StreamingCall(ctx)
288293
if err != nil {
289294
// Wait for warmup to be finished before reporting the call failed
290295
warmupWg.Wait()
291-
handleFailure(connectionID)
296+
handleFailure(connectionID, err)
292297
return
293298
}
294299

@@ -300,10 +305,10 @@ func makeCaller(cc *grpc.ClientConn, connectionID int, streamID int, scenario st
300305

301306
start := time.Now()
302307
if err := stream.Send(request); err != nil {
303-
handleFailure(connectionID)
308+
handleFailure(connectionID, err)
304309
} else {
305310
if _, err := stream.Recv(); err != nil {
306-
handleFailure(connectionID)
311+
handleFailure(connectionID, err)
307312
} else {
308313
end := time.Now()
309314
handleSuccess(connectionID, start, end)
@@ -320,8 +325,12 @@ func makeCaller(cc *grpc.ClientConn, connectionID int, streamID int, scenario st
320325
return nil
321326
}
322327

323-
func handleFailure(connectionID int) {
324-
if stopped || warmingUp {
328+
func handleFailure(connectionID int, err error) {
329+
if warmingUp {
330+
return
331+
}
332+
if stopped {
333+
fmt.Printf("Failure after stop: %v\n", err)
325334
return
326335
}
327336
connectionLocks[connectionID].Lock()

0 commit comments

Comments
 (0)