How to implement a custom TCP health probe for Azure Cloud Services (classic) worker roles?

106 Views Asked by At

I have a Cloud Services (classic extended support) app that exposes a couple of UDP and a couple of TCP endpoints in a worker role. I want to add a custom health probe that probes the application state (not just the accessibility of the service itself which is what the default health probe checks if I'm not mistaken) and disable the instance in the load balancer under certain conditions.

The documentation states:

A TCP probe fails when: 1. The TCP listener on the instance doesn't respond at all during the timeout period. A probe is marked down based on the number of timed-out probe requests, which were configured to go unanswered before marking down the probe. 2. The probe receives a TCP reset from the instance.

However, I'm not aware of how to produce this effect. How can I send a TCP RST message to the probe using a C# API? Or is there another way I should implement the custom probe?

This is the code I have so far:

        var healthCheckEndpoint = RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["health"];
        var healthCheckIpEndpoint = healthCheckEndpoint.IPEndpoint;
        var server = new TcpListener(healthCheckIpEndpoint.Address, healthCheckIpEndpoint.Port);
        server.Start();

        while (true)
        {
            using (var client = server.AcceptTcpClient())
            {
                client.LingerState = new LingerOption(false, 0);
                client.Close();
            }
        }

I have also tried to call client.Client.Close() to force the underlying Socket to immediately close. However, the health check still passes. Also, when I use nc -v -w 65 localhost 10101 the connection seems to successfully close, or at least I can't see any logs that indicate that a reset was sent.

0

There are 0 best solutions below