Windows Azure

Windows Azure Load Balancing Timeout

Fév 1, 2012

Emmanuel Piteau

Une instance Windows Azure s »exécute derrière un système de load balancing matériel.

Ce matériel utilise un timeout de 60 secondes qui n »est pas modifiable de quelque manière que ce soit.
Cela a pour effet de couper la connexion après 60 secondes d »inactivité. Si votre application utilise des sockets et nécessite une connexion ouverte plus longtemps pour un transfert de données ou autre, vous devez contourner ce problème en utilisant ServicePointManager.SetTcpKeepAlive(). Les
paquets TCP Keep-Alive maintiennent la connexion entre votre client et l »équilibreur de charge lors d »une requête HTTP de longue durée.

Voici un exemple d »utilisation :

private void SetKeepAlive(Socket sock)
{
    int KeepAliveTime = 1000 * 45; // 45 seconds of inactivity allowed
    int KeepAliveInterval = 1000; // 1 second interval on keep-alive checks (default)
            
    my_socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
    my_socket.IOControl(IOControlCode.KeepAliveValues, GetKeepAliveByte(KeepAliveTime, KeepAliveInterval), null);
}


// "consts" to help understand calculations
const int bytesperlong = 4; // 32 / 8
const int bitsperbyte = 8;

private byte[] GetKeepAliveByte(ulong time, ulong interval)
{
    // resulting structure
    byte[] SIO_KEEPALIVE_VALS = new byte[3 * bytesperlong];
    try
    {
        // array to hold input values
        ulong[] input = new ulong[3];

        // put input arguments in input array
        if (time == 0 || interval == 0) // enable disable keep-alive
            input[0] = (0UL); // off
        else
            input[0] = (1UL); // on

        input[1] = (time); // time millis
        input[2] = (interval); // interval millis

        // pack input into byte struct
        for (int i = 0; i < input.Length; i++)
        {
            SIO_KEEPALIVE_VALS[i * bytesperlong + 3] = (byte)(input[i] >> ((bytesperlong - 1) * bitsperbyte) & 0xff);
            SIO_KEEPALIVE_VALS[i * bytesperlong + 2] = (byte)(input[i] >> ((bytesperlong - 2) * bitsperbyte) & 0xff);
            SIO_KEEPALIVE_VALS[i * bytesperlong + 1] = (byte)(input[i] >> ((bytesperlong - 3) * bitsperbyte) & 0xff);
            SIO_KEEPALIVE_VALS[i * bytesperlong + 0] = (byte)(input[i] >> ((bytesperlong - 4) * bitsperbyte) & 0xff);
        }
    }
    catch (Exception)
    {
        return SIO_KEEPALIVE_VALS;
    }
    return SIO_KEEPALIVE_VALS;
}

0 commentaires

Soumettre un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *

Découvrez nos autres articles

Aller au contenu principal