Com-AugustCellars / CoAP-CSharp

CoAP Implementation in C#
Other
41 stars 19 forks source link

Server stops responding after 65536 requests in simple test app #95

Open Jepplar opened 11 months ago

Jepplar commented 11 months ago

I'm running into an issue where the CoAP server seemingly stops responding to the client after 2**16 requests in one of my applications. I have separated the issue into a simple test server+client as outlined below which replicates the issue.

Even though the client sends 100k requests, only 65k of them are received by the server. Adding a sleep when sending requests, waiting for ACK etc does not seem to make a difference.

Any ideas on how to resolve the issue?

Server code:

namespace CoapTestServer
{
    internal class Program
    {
        static void Main(string[] args)
        {
            CoapServer server = new CoapServer();

            server.Add(new TestResource());
            server.Start();

            Console.WriteLine("Running...");
            Console.ReadKey();
        }
    }

    class TestResource : Resource
    {
        static long connectionCount = 0;

        public TestResource() : base("test-resource")
        {
        }

        protected override void DoGet(CoapExchange exchange)
        {
            long newCount = Interlocked.Increment(ref connectionCount);
            Console.WriteLine($"Server received {newCount} requests");

            exchange.Respond("This is a response");
        }
    }
}

Client code:

Uri hostUri = new Uri("coap://localhost/test-resource");
for (int i = 0; i < 100 * 1000; i++)
{
    try
    {
        Request request = new(Method.GET, false)
        {
            URI = hostUri,
        };

        request.Send();
        request.WaitForResponse();

        Console.WriteLine($"Client sent {i} requests");
    }
    catch (Exception ex)
    {
        Console.WriteLine($"{ex.Message}");
    }
}

Console.WriteLine("Finished, press enter to exit");
Console.ReadLine();

Output: image

I am using NuGet package 1.10.0 of "Com.AugustCellars.CoAP" with .NET 7.0 and Visual Studio 2022 on Windows 10.

Jepplar commented 11 months ago

I have managed to find a workaround for the issue by lowering ExchangeLifetime to 5000 in the server config from its default value. It seems to be connected to deduplication although I'm not quite sure which side effects it might have.

I'm also not sure why it runs into an issue at 65k exchanges which seem a bit low. I am assuming that it runs out of some kind of handle or buffer space, which the above mentioned sweep resolves by removing old exchanges prematurely.

Is there perhaps a better solution?