Base solution for your next web application

Activities of "grinay"

<cite>alper: </cite> Become a partner:

If you are experienced in ASP.NET Zero and open to project development on a per-customer basis, see <a class="postlink" href="https://aspnetzero.com/Partnership">https://aspnetzero.com/Partnership</a>

Thank.

Try to put [UnitOfWork(IsDisabled = true)] attribute before you method. It will disable any other unit of work.

It didn't help. Anyway I solved the problem. The main problem was using Task.Wait() method. The code is checking availability of servers by TcpClient library. This library doesn't have any way to setup timeout for connect. Therefore I solved it with TcpClient.Connect(args).Wait(timeout) method. But problem that wait method is blocking thread. Such a way when I tried to scan 1000+ servers, everything became slow, because of 1000+ threads were in blocking state. I reworked my scan method and made it really asynchronous like this.

add extension method for Task

private static async Task WithTimeout(this Task task, TimeSpan timeout)
        {
            Task winner = await Task.WhenAny(task, Task.Delay(timeout));
            if (winner != task)
            {
                throw new TimeoutException();
            }

            await task;
        }

Then use tcp client with this method

await tcpClient.ConnectAsync(serverIpAddr, server.Port ?? 1).WithTimeout(TimeSpan.FromSeconds(connectTimeout));

Now code became really fast. it scans 1000 server, without any delay, and don't slow down the whole system . Hope this help someone else.

Answer

OK. Thank.

Showing 1 to 4 of 4 entries