Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

420
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda