Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
guillaume-chervet committed Jun 19, 2024
1 parent f8cc3a7 commit 41c5112
Showing 1 changed file with 36 additions and 12 deletions.
48 changes: 36 additions & 12 deletions src/SlimFaas/SlimProxyMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,20 @@ public enum FunctionType
NotAFunction
}

public record FunctionStatus(int NumberReady, int NumberRequested);
public record FunctionStatus(int NumberReady, int NumberRequested, PodType PodType, FunctionVisibility Visibility);

[JsonSerializable(typeof(FunctionStatus))]
[JsonSourceGenerationOptions(WriteIndented = false, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
public partial class FunctionStatusSerializerContext : JsonSerializerContext
{
}

[JsonSerializable(typeof(List<FunctionStatus>))]
[JsonSourceGenerationOptions(WriteIndented = false, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
public partial class ListFunctionStatusSerializerContext : JsonSerializerContext
{
}

public class SlimProxyMiddleware(RequestDelegate next, ISlimFaasQueue slimFaasQueue, IWakeUpFunction wakeUpFunction,
ILogger<SlimProxyMiddleware> logger,
int timeoutWaitWakeSyncFunctionMilliSecond =
Expand Down Expand Up @@ -57,10 +63,23 @@ public async Task InvokeAsync(HttpContext context,
return;
}



HttpRequest contextRequest = context.Request;
(string functionPath, string functionName, FunctionType functionType) = GetFunctionInfo(logger, contextRequest);
HttpResponse contextResponse = context.Response;

if(contextRequest.Path.StartsWithSegments("/status-functions"))
{
IList<FunctionStatus> functionStatuses = replicasService.Deployments.Functions.Select(MapToFunctionStatus).ToList();
await contextResponse.WriteAsJsonAsync(functionStatuses,
ListFunctionStatusSerializerContext.Default.ListFunctionStatus);
context.Response.StatusCode = 200;
return;
}

(string functionPath, string functionName, FunctionType functionType) = GetFunctionInfo(logger, contextRequest);


switch (functionType)
{
case FunctionType.NotAFunction:
Expand Down Expand Up @@ -135,18 +154,12 @@ private static bool IsInternalIp(string? ipAddress, IList<string> podIps)
private static void BuildStatusResponse(IReplicasService replicasService,
string functionName, HttpResponse contextResponse)
{
DeploymentInformation? function = SearchFunction(replicasService, functionName);
if (function != null)
DeploymentInformation? functionDeploymentInformation = SearchFunction(replicasService, functionName);
if (functionDeploymentInformation != null)
{
DeploymentInformation? functionDeploymentInformation =
replicasService.Deployments.Functions.FirstOrDefault(f => f.Deployment == functionName);
int numberReady = functionDeploymentInformation == null
? 0
: functionDeploymentInformation.Pods.Count(p => p.Ready.HasValue && p.Ready.Value);
int numberRequested =
functionDeploymentInformation?.Replicas ?? 0;
FunctionStatus functionStatus = MapToFunctionStatus(functionDeploymentInformation);
contextResponse.StatusCode = 200;
contextResponse.WriteAsJsonAsync(new FunctionStatus(numberReady, numberRequested),
contextResponse.WriteAsJsonAsync(functionStatus,
FunctionStatusSerializerContext.Default.FunctionStatus);
}
else
Expand All @@ -155,6 +168,17 @@ private static void BuildStatusResponse(IReplicasService replicasService,
}
}

private static FunctionStatus MapToFunctionStatus(DeploymentInformation functionDeploymentInformation)
{
int numberReady = functionDeploymentInformation.Pods.Count(p => p.Ready.HasValue && p.Ready.Value);
int numberRequested =
functionDeploymentInformation.Replicas;
var functionStatus = new FunctionStatus(numberReady, numberRequested,
functionDeploymentInformation.PodType,
functionDeploymentInformation.Visibility);
return functionStatus;
}

private static void BuildWakeResponse(IReplicasService replicasService, IWakeUpFunction wakeUpFunction,
string functionName, HttpResponse contextResponse)
{
Expand Down

0 comments on commit 41c5112

Please sign in to comment.