Skip to content

Commit

Permalink
Adding a TryFiveTimes method for AzureBlobCache, currently only using…
Browse files Browse the repository at this point in the history
… within the RewritePath method but could/should be used everywhere that a connection is made

Former-commit-id: c1e464b
Former-commit-id: 28f34f708bce6e9e4653addeface206a14ed02d0
  • Loading branch information
Jeavon committed Nov 17, 2015
1 parent 8375638 commit ee53e10
Showing 1 changed file with 45 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -371,29 +371,37 @@ public override void RewritePath(HttpContext context)
// Write the blob storage directly to the stream
request.Method = "GET";

using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
TryFiveTimes(() =>
{
Stream cachedStream = response.GetResponseStream();

if (cachedStream != null)
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
HttpResponse contextResponse = context.Response;
cachedStream.CopyTo(contextResponse.OutputStream);
ImageProcessingModule.SetHeaders(context, this.mimeType, null, this.MaxDays, response.StatusCode);
Stream cachedStream = response.GetResponseStream();

if (cachedStream != null)
{
HttpResponse contextResponse = context.Response;
cachedStream.CopyTo(contextResponse.OutputStream);
ImageProcessingModule.SetHeaders(context, this.mimeType, null, this.MaxDays, response.StatusCode);
}
}
}
});

}
else
{
// Redirect the request to the blob URL
request.Method = "HEAD";

using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
TryFiveTimes(() =>
{
HttpStatusCode responseCode = response.StatusCode;
ImageProcessingModule.AddCorsRequestHeaders(context);
context.Response.Redirect(responseCode == HttpStatusCode.NotFound ? this.CachedPath : this.cachedRewritePath, false);
}
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
HttpStatusCode responseCode = response.StatusCode;
ImageProcessingModule.AddCorsRequestHeaders(context);
context.Response.Redirect(responseCode == HttpStatusCode.NotFound ? this.CachedPath : this.cachedRewritePath, false);
}
});

}
}

Expand All @@ -411,5 +419,29 @@ private static CloudBlobContainer CreateContainer(CloudBlobClient cloudBlobClien
container.SetPermissions(new BlobContainerPermissions { PublicAccess = accessType });
return container;
}

/// <summary>
/// Trys to execute a delegate action five times
/// </summary>
/// <param name="delegateAction">The delegate to be executed</param>
private static void TryFiveTimes(Action delegateAction)
{
for (var retry = 0; ; retry++)
{
try
{
delegateAction();
return;
}
catch (Exception)
{
if (retry >= 5)
{
throw;
}
}
}
}

}
}

0 comments on commit ee53e10

Please sign in to comment.