2015-06-26 23 views
5

Ho scritto una logica molto riprovevole per le chiamate client DocumentDB limitate.DocumentDB TransientFaultHandling Best Practice

L'esempio seguente è un esempio comune di questo con 10 tentativi.

La mia domanda è doppia: Questa è la migliore pratica, e c'è un modo meno dettagliato per gestirlo? Vedo che esiste un pacchetto nuget di Microsoft.Azure.Documents.Client.TransientFaultHandling pacchetto Microsoft n. che dovrebbe ottenere gli stessi risultati con meno codice, ma non riesco a trovare alcun esempio su StackOverflow o Google e sembra che non ci sia alcun chiaro documentazione disponibile da Microsoft.

int maxRetryAttempts = 10; 

while (maxRetryAttempts > 0) 
{ 
    try 
    { 
     // Attempt to call DocumentDB Method 
     // ---[DocumentDB Method Here]--- 
    } 
    catch (DocumentClientException de) 
    { 
     if (de.StatusCode.HasValue) 
     { 
      var statusCode = (int)de.StatusCode; 

      if (statusCode == 429 || statusCode == 503) 
      { 
       //Sleep for retry amount 
       Thread.Sleep(de.RetryAfter); 

       //Decrement max retry attempts 
       maxRetryAttempts--; 
      } 

     } 
    } 
    catch (AggregateException ae) 
    {      
     foreach (Exception ex in ae.InnerExceptions) 
     { 
      if (ex is DocumentClientException) 
      { 
       var documentClientException = ex as DocumentClientException; 
       var statusCode = (int)documentClientException.StatusCode; 
       if (statusCode == 429 || statusCode == 503) 
       { 
        //Sleep for retry amount 
        Thread.Sleep(documentClientException.RetryAfter); 

        //Decrement max retry attempts 
        maxRetryAttempts--; 
       } 
       else 
       { 
        throw; 
       } 
      } 
     } 
    } 
} 

if (maxRetryAttempts < 0) 
{ 
    //Max retry attempts reached 
} 
+0

In il tuo blocco 'catch', quando si estrae l'oggetto 'AggregateException', scarta silenziosamente tutte le eccezioni non-'DocumentClientException'. È intenzionale? Mi sarei aspettato bolle di eccezioni sconosciute. –

risposta

7

È possibile trovare il codice di esempio utilizzando il pacchetto TransientFaultHandling Nuget nel repository GitHub per lo strumento di migrazione dei dati DocumentDB:

https://github.com/Azure/azure-documentdb-datamigrationtool/blob/master/DocumentDb/Microsoft.DataTransfer.DocumentDb.FunctionalTests/DocumentDbHelper.cs

che sembra qualcosa di simile:

private static IReliableReadWriteDocumentClient CreateClient(IDocumentDbConnectionSettings connectionSettings) 
{ 
    return new DocumentClient(new Uri(connectionSettings.AccountEndpoint), connectionSettings.AccountKey) 
     .AsReliable(new FixedInterval(10, TimeSpan.FromSeconds(1))); 
} 
+2

Grazie Andrew. Questo era esattamente ciò di cui avevo bisogno. C'è un modo per recuperare la quantità di tentativi che si sono verificati dopo aver effettuato una chiamata riuscita? Vedo che recupero l'importo dell'IF utilizzato, oltre ad altre informazioni, ma sapere quanti tentativi sono stati necessari mi aiuterà a sintonizzare le mie chiamate. – CodeAbundance

+0

Bello. I clienti affidabili e "inaffidabili" ora hanno codebase non condivisibile nonostante siano uguali. Finché non usi 'dynamic', lol. – abatishchev