private async Task RunSingleRpcAsync(TestService.TestServiceClient client, CancellationToken cancellationToken, RpcType rpcType) { long rpcId = statsWatcher.RpcIdGenerator.Increment(); try { Console.WriteLine($"Starting RPC {rpcId} of type {rpcType}"); // metadata to send with the RPC var headers = new Metadata(); if (metadata.ContainsKey(rpcType)) { headers = metadata[rpcType]; if (headers.Count > 0) { var printableHeaders = "[" + string.Join(", ", headers) + "]"; Console.WriteLine($"Will send metadata {printableHeaders}"); } } if (rpcType == RpcType.UnaryCall) { var call = client.UnaryCallAsync(new SimpleRequest(), new CallOptions(headers: headers, cancellationToken: cancellationToken, deadline: DateTime.UtcNow.AddSeconds(options.RpcTimeoutSec))); var response = await call; var hostname = (await call.ResponseHeadersAsync).GetValue("hostname") ?? response.Hostname; statsWatcher.OnRpcComplete(rpcId, rpcType, hostname); if (options.PrintResponse) { Console.WriteLine($"Got response {response}"); } } else if (rpcType == RpcType.EmptyCall) { var call = client.EmptyCallAsync(new Empty(), new CallOptions(headers: headers, cancellationToken: cancellationToken, deadline: DateTime.UtcNow.AddSeconds(options.RpcTimeoutSec))); var response = await call; var hostname = (await call.ResponseHeadersAsync).GetValue("hostname"); statsWatcher.OnRpcComplete(rpcId, rpcType, hostname); if (options.PrintResponse) { Console.WriteLine($"Got response {response}"); } } else { throw new InvalidOperationException($"Unsupported RPC type ${rpcType}"); } Console.WriteLine($"RPC {rpcId} succeeded"); } catch (RpcException ex) { statsWatcher.OnRpcComplete(rpcId, rpcType, null); Console.WriteLine($"RPC {rpcId} failed: {ex}"); } }
private async Task RunSingleRpcAsync(TestService.TestServiceClient client, CancellationToken cancellationToken) { long rpcId = statsWatcher.RpcIdGenerator.Increment(); try { Console.WriteLine($"Starting RPC {rpcId}."); var response = await client.UnaryCallAsync(new SimpleRequest(), new CallOptions(cancellationToken : cancellationToken, deadline : DateTime.UtcNow.AddSeconds(options.RpcTimeoutSec))); statsWatcher.OnRpcComplete(rpcId, response.Hostname); if (options.PrintResponse) { Console.WriteLine($"Got response {response}"); } Console.WriteLine($"RPC {rpcId} succeeded "); } catch (RpcException ex) { statsWatcher.OnRpcComplete(rpcId, null); Console.WriteLine($"RPC {rpcId} failed: {ex}"); } }