Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

418
Visualizações
How to set DotNetty(Netty) Bootstrap.ConnectAsync timeout?

I'm using DotNetty in Unity, and has the following client bootstrap, when the server is down, it will take the "clientBootstrap.ConnectAsync()" around 17 seconds to give up trying to establish a connection, during which, the client is frozen. Actually, it doesn't need to try for 17 seconds to know there can be no connection. How can I configurate so the "clientBootstrap.ConnectAsync()" trys only for maybe 5 seconds? I added ".Option(ChannelOption.ConnectTimeout, TimeSpan.FromMilliseconds(5000))", but still, it takes 17 seconds to stop trying. The option only triggered an extra timeout exception.

    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));

Thank you very much.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You should be able to set a timeout according to this:

Not familiar with dotnetty but applied to your snippet I would try something like:

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
    }
}

Useful to read task delay documentation:

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda