如何设置 DotNetty(Netty) Bootstrap.ConnectAsync 超时?
How to set DotNetty(Netty) Bootstrap.ConnectAsync timeout?
我在 Unity 中使用 DotNetty,并有以下客户端 bootstrap,当服务器关闭时,它会在“clientBootstrap.ConnectAsync()”大约 17 秒后放弃尝试建立连接,在此期间,客户端被冻结。其实用不着试17秒就知道连接不上了。
我如何配置才能使“clientBootstrap.ConnectAsync()”仅尝试 5 秒?
我添加了“.Option(ChannelOption.ConnectTimeout, TimeSpan.FromMilliseconds(5000))”,但仍然需要 17 秒才能停止尝试。该选项仅触发了额外的超时异常。
try
{
var clientBootstrap = new Bootstrap();
clientBootstrap
.Group(group)
.Channel<TcpSocketChannel>()
.Option(ChannelOption.TcpNodelay, true)
.Option(ChannelOption.ConnectTimeout, TimeSpan.FromMilliseconds(1000))
.Handler(new ActionChannelInitializer<ISocketChannel>(channel =>
{
var pipeline = channel.Pipeline;
pipeline.AddLast(new IdleStateHandler(0, 0, 6));
......
}));
clientChannel = await clientBootstrap.ConnectAsync(new IPEndPoint(IPAddress.Parse(IP), Port));
非常感谢。
你应该可以根据this设置超时:
不熟悉 dotnetty 但应用于您的代码段我会尝试类似的方法:
try
{
var clientBootstrap = new Bootstrap();
clientBootstrap
.Group(group)
.Channel<TcpSocketChannel>()
.Option(ChannelOption.TcpNodelay, true)
.Option(ChannelOption.ConnectTimeout, TimeSpan.FromMilliseconds(1000))
.Handler(new ActionChannelInitializer<ISocketChannel>(channel =>
{
var pipeline = channel.Pipeline;
pipeline.AddLast(new IdleStateHandler(0, 0, 6));
......
}));
int timeout = 5000;
var clientChannel = clientBootstrap.ConnectAsync(new IPEndPoint(IPAddress.Parse(IP), Port));
if (await Task.WhenAny(clientChannel, Task.Delay(timeout)) == clientChannel) {
// task completed within timeout
} else {
// timeout logic
}
}
有用读任务延迟documentation:
我在 Unity 中使用 DotNetty,并有以下客户端 bootstrap,当服务器关闭时,它会在“clientBootstrap.ConnectAsync()”大约 17 秒后放弃尝试建立连接,在此期间,客户端被冻结。其实用不着试17秒就知道连接不上了。 我如何配置才能使“clientBootstrap.ConnectAsync()”仅尝试 5 秒? 我添加了“.Option(ChannelOption.ConnectTimeout, TimeSpan.FromMilliseconds(5000))”,但仍然需要 17 秒才能停止尝试。该选项仅触发了额外的超时异常。
try
{
var clientBootstrap = new Bootstrap();
clientBootstrap
.Group(group)
.Channel<TcpSocketChannel>()
.Option(ChannelOption.TcpNodelay, true)
.Option(ChannelOption.ConnectTimeout, TimeSpan.FromMilliseconds(1000))
.Handler(new ActionChannelInitializer<ISocketChannel>(channel =>
{
var pipeline = channel.Pipeline;
pipeline.AddLast(new IdleStateHandler(0, 0, 6));
......
}));
clientChannel = await clientBootstrap.ConnectAsync(new IPEndPoint(IPAddress.Parse(IP), Port));
非常感谢。
你应该可以根据this设置超时:
不熟悉 dotnetty 但应用于您的代码段我会尝试类似的方法:
try
{
var clientBootstrap = new Bootstrap();
clientBootstrap
.Group(group)
.Channel<TcpSocketChannel>()
.Option(ChannelOption.TcpNodelay, true)
.Option(ChannelOption.ConnectTimeout, TimeSpan.FromMilliseconds(1000))
.Handler(new ActionChannelInitializer<ISocketChannel>(channel =>
{
var pipeline = channel.Pipeline;
pipeline.AddLast(new IdleStateHandler(0, 0, 6));
......
}));
int timeout = 5000;
var clientChannel = clientBootstrap.ConnectAsync(new IPEndPoint(IPAddress.Parse(IP), Port));
if (await Task.WhenAny(clientChannel, Task.Delay(timeout)) == clientChannel) {
// task completed within timeout
} else {
// timeout logic
}
}
有用读任务延迟documentation: