Skip to content

Commit

Permalink
feat(slimfaas): add to sendclient to know with function is failing (#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
guillaume-chervet authored Aug 20, 2024
1 parent ccf6d48 commit 34c3273
Showing 1 changed file with 39 additions and 22 deletions.
61 changes: 39 additions & 22 deletions src/SlimFaas/SendClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,33 +21,49 @@ public class SendClient(HttpClient httpClient, ILogger<SendClient> logger) : ISe
public async Task<HttpResponseMessage> SendHttpRequestAsync(CustomRequest customRequest,
HttpContext? context = null, string? baseUrl = null)
{
string functionUrl = baseUrl ?? _baseFunctionUrl;
string customRequestFunctionName = customRequest.FunctionName;
string customRequestPath = customRequest.Path;
string customRequestQuery = customRequest.Query;
string targetUrl =
ComputeTargetUrl(functionUrl, customRequestFunctionName, customRequestPath, customRequestQuery, _namespaceSlimFaas);
logger.LogDebug("Sending async request to {TargetUrl}", targetUrl);
HttpRequestMessage targetRequestMessage = CreateTargetMessage(customRequest, new Uri(targetUrl));
if (context != null)
try
{
string functionUrl = baseUrl ?? _baseFunctionUrl;
string customRequestFunctionName = customRequest.FunctionName;
string customRequestPath = customRequest.Path;
string customRequestQuery = customRequest.Query;
string targetUrl =
ComputeTargetUrl(functionUrl, customRequestFunctionName, customRequestPath, customRequestQuery, _namespaceSlimFaas);
logger.LogDebug("Sending async request to {TargetUrl}", targetUrl);
HttpRequestMessage targetRequestMessage = CreateTargetMessage(customRequest, new Uri(targetUrl));
if (context != null)
{
return await httpClient.SendAsync(targetRequestMessage,
HttpCompletionOption.ResponseHeadersRead, context.RequestAborted);
}

return await httpClient.SendAsync(targetRequestMessage,
HttpCompletionOption.ResponseHeadersRead, context.RequestAborted);
HttpCompletionOption.ResponseHeadersRead);
}
catch (Exception e)
{
logger.LogError(e, "Error in SendHttpRequestAsync to {FunctionName} to {FunctionPath} ", customRequest.FunctionName, customRequest.FunctionName);
throw;
}

return await httpClient.SendAsync(targetRequestMessage,
HttpCompletionOption.ResponseHeadersRead);
}

public async Task<HttpResponseMessage> SendHttpRequestSync(HttpContext context, string functionName,
string functionPath, string functionQuery, string? baseUrl = null)
{
string targetUrl = ComputeTargetUrl(baseUrl ?? _baseFunctionUrl, functionName, functionPath, functionQuery, _namespaceSlimFaas);
logger.LogDebug("Sending sync request to {TargetUrl}", targetUrl);
HttpRequestMessage targetRequestMessage = CreateTargetMessage(context, new Uri(targetUrl));
HttpResponseMessage responseMessage = await httpClient.SendAsync(targetRequestMessage,
HttpCompletionOption.ResponseHeadersRead, context.RequestAborted);
return responseMessage;
try
{
string targetUrl = ComputeTargetUrl(baseUrl ?? _baseFunctionUrl, functionName, functionPath, functionQuery, _namespaceSlimFaas);
logger.LogDebug("Sending sync request to {TargetUrl}", targetUrl);
HttpRequestMessage targetRequestMessage = CreateTargetMessage(context, new Uri(targetUrl));
HttpResponseMessage responseMessage = await httpClient.SendAsync(targetRequestMessage,
HttpCompletionOption.ResponseHeadersRead, context.RequestAborted);
return responseMessage;
}
catch (Exception e)
{
logger.LogError(e, "Error in SendHttpRequestSync to {FunctionName} to {FunctionPath} ", functionName, functionPath);
throw;
}
}

private void CopyFromOriginalRequestContentAndHeaders(CustomRequest context, HttpRequestMessage requestMessage)
Expand All @@ -72,7 +88,7 @@ private void CopyFromOriginalRequestContentAndHeaders(CustomRequest context, Htt

private HttpRequestMessage CreateTargetMessage(CustomRequest context, Uri targetUri)
{
HttpRequestMessage requestMessage = new HttpRequestMessage();
HttpRequestMessage requestMessage = new();
CopyFromOriginalRequestContentAndHeaders(context, requestMessage);

requestMessage.RequestUri = targetUri;
Expand Down Expand Up @@ -133,7 +149,7 @@ private static string ComputeTargetUrl(string functionUrl, string customRequestF

private HttpRequestMessage CreateTargetMessage(HttpContext context, Uri targetUri)
{
HttpRequestMessage requestMessage = new HttpRequestMessage();
HttpRequestMessage requestMessage = new();
CopyFromOriginalRequestContentAndHeaders(context, requestMessage);

requestMessage.RequestUri = targetUri;
Expand All @@ -152,13 +168,14 @@ private HttpRequestMessage CreateTargetMessage(HttpContext context, Uri targetUr
private void CopyFromOriginalRequestContentAndHeaders(HttpContext context, HttpRequestMessage requestMessage)
{
string requestMethod = context.Request.Method;
context.Request.EnableBuffering(bufferThreshold: 1024 * 100, bufferLimit: 200 * 1024 * 1024);

if (!HttpMethods.IsGet(requestMethod) &&
!HttpMethods.IsHead(requestMethod) &&
!HttpMethods.IsDelete(requestMethod) &&
!HttpMethods.IsTrace(requestMethod))
{
StreamContent streamContent = new StreamContent(context.Request.Body);
StreamContent streamContent = new(context.Request.Body);
requestMessage.Content = streamContent;
}

Expand Down

0 comments on commit 34c3273

Please sign in to comment.