jacqueskang/IpcServiceFramework

[Feature Request] TcpClient timeout settings

Closed this issue · 8 comments

Could you make a change to add the send/receieve timeout options for TcpClient? I.e. something like

var client = new TcpClient()
{
SendTimeout = _sendTimeout,
ReceiveTimeout = _recvTimeout
};

I set SendTimeout and ReceiveTimeout on TcpClient but then I realized it's not affecting asynchronous reading.
See https://docs.microsoft.com/en-us/dotnet/api/system.net.sockets.networkstream.readtimeout?view=netcore-2.1

This property affects only synchronous reads performed by calling the Read method. This property does not affect asynchronous reads performed by calling the BeginRead method.

It seems that you can still have timeout behavior using the cancellation token, something like:

var source = new CancellationTokenSource();
source.CancelAfter(1000);
await client.InvokeAsync(x => x.DoSomething(), source.Token);

Is it acceptable?

It seems that you can still have timeout behavior using the cancellation token, something like:

var source = new CancellationTokenSource();
source.CancelAfter(1000);
await client.InvokeAsync(x => x.DoSomething(), source.Token);

Is it acceptable?

What you've proposed works 99% of the time and I'm currently doing this in the client however in some cases it seems to ignore the CancellationToken and hangs. When it hangs, sometimes it then times out after 10 minutes, but other times it never times out and you need to restart the client.

My application has approx 200 clients and I've tried dedicating an individual TCP server and port to each client, and queueing client messages so that server responses don't get out of sync but there is an underlying issue with the client sometimes ignoring the CancellationToken.

I'll further investigate why cancellation not be taken into account in that 1% case.

Thanks much appreciated. Another good idea might be to expose the CancellationToken on the server side like what you are doing on the client side.

@burnzau On server side CancellationToken is already in place to support graceful shutdown. Normally closing TCP connection from client side should be enough.

Resolved in v2.1.1

Cheers mate, just gotta say I really like this library