grpc / grpc/grpc-dotnet

How can a server detected that client has stopped listening to a response stream.

Open
#2,296 0 comments 0 reactions 0 assignees View on GitHub
question
Dominant language
C#
Stars
4.5k
Forks
836
Avg merge
6d 3h
Merged PRs (30d)
7

Description

I am implementing a demo server and client with GRPC, where client send a single request and receive the multiple responses.

# Proto:
```
rpc Test (HelloRequest1) returns (stream HelloReply1);
```

# Server Implementation:
```
public override async Task Test(HelloRequest1 request, IServerStreamWriter responseStream, ServerCallContext context)
{
try
{
Console.WriteLine("request form client " + request.Name);
await Task.Delay(1000 * 60);
await responseStream.WriteAsync(new HelloReply1
{
Message = "Process Completed " + request.Name
});
Console.WriteLine("ended writing responses");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
```

# Client Implementation:
```
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
try
{
var handler = new SocketsHttpHandler
{
PooledConnectionIdleTimeout = Timeout.InfiniteTimeSpan,
KeepAlivePingDelay = TimeSpan.FromSeconds(20),
KeepAlivePingTimeout = TimeSpan.FromSeconds(30),
EnableMultipleHttp2Connections = true
};

Console.WriteLine($"Hello, World! {moduleIp}");
var channel = GrpcChannel.ForAddress($"http://{moduleIp}:7045");
/*var channel = GrpcChannel.ForAddress($"http://{moduleIp}:7045", new GrpcChannelOptions
{
HttpHandler = handler
});*/
var Client = new Send.SendClient(channel);
var response = Client.Test(new HelloRequest1 { Name = "Start" });
while (await response.ResponseStream.MoveNext(cancellationToken: InstallationCancelToken.Token))
{
var res = response.ResponseStream.Current;
Console.WriteLine($"{res.Message}");
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
```

After receiving the request from the client server may take some time to process it, hence to simulate that I have added this 1 min delay,
during this time if client is disconnected from the WIFI, it will stop listening over the stream, but server is not aware of it, after a minute of delay server is writing the message to stream which is not received at the client end.

In my case If the message is not delivered I want to save that message to a queue to process it latter and continue the running process on the server side, hence I can't use the keep alive because after the certain time server will throw a error and stop the running process.

Is there a way to detected if the client is listening to the stream before writing the message on to the stream.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.