Skip to content

Commit

Permalink
chore: update target framework and code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
brmagadutra committed Feb 7, 2024
1 parent fa4df74 commit 2e47a98
Show file tree
Hide file tree
Showing 70 changed files with 248 additions and 150 deletions.
14 changes: 7 additions & 7 deletions LoadShedding.sln
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "samples", "samples", "{BB0D
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "unit", "unit", "{E149B487-146C-4AA4-BFC9-4A42FBEA3E05}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Samples.WebApi", "samples\Samples.WebApi\Samples.WebApi.csproj", "{8853CFF3-987F-469A-9D3D-24C875B7DFB4}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "integration", "integration", "{6143CC7F-F882-4A90-9CAB-E11597429CC4}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "benchmark", "benchmark", "{D91E6E3A-4165-4934-9462-E62B04250932}"
Expand Down Expand Up @@ -43,16 +41,14 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
docker\Sample.Dockerfile = docker\Sample.Dockerfile
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Farfetch.LoadShedding.Samples.WebApi", "samples\Farfetch.LoadShedding.Samples.WebApi\Farfetch.LoadShedding.Samples.WebApi.csproj", "{B1D34BC0-0ACF-4649-8FC6-D83A69EDB2DC}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{8853CFF3-987F-469A-9D3D-24C875B7DFB4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8853CFF3-987F-469A-9D3D-24C875B7DFB4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8853CFF3-987F-469A-9D3D-24C875B7DFB4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8853CFF3-987F-469A-9D3D-24C875B7DFB4}.Release|Any CPU.Build.0 = Release|Any CPU
{59E73202-B2B1-4098-94B3-348C853C6583}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{59E73202-B2B1-4098-94B3-348C853C6583}.Debug|Any CPU.Build.0 = Debug|Any CPU
{59E73202-B2B1-4098-94B3-348C853C6583}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down Expand Up @@ -85,13 +81,16 @@ Global
{0BD82D67-CE42-4EB8-AF58-703AB143DF05}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0BD82D67-CE42-4EB8-AF58-703AB143DF05}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0BD82D67-CE42-4EB8-AF58-703AB143DF05}.Release|Any CPU.Build.0 = Release|Any CPU
{B1D34BC0-0ACF-4649-8FC6-D83A69EDB2DC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B1D34BC0-0ACF-4649-8FC6-D83A69EDB2DC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B1D34BC0-0ACF-4649-8FC6-D83A69EDB2DC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B1D34BC0-0ACF-4649-8FC6-D83A69EDB2DC}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{E149B487-146C-4AA4-BFC9-4A42FBEA3E05} = {693C5879-B401-4398-BAA1-7FAF8B610BDD}
{8853CFF3-987F-469A-9D3D-24C875B7DFB4} = {BB0DD68C-4E4B-46DE-86B0-BA365448F9A2}
{6143CC7F-F882-4A90-9CAB-E11597429CC4} = {693C5879-B401-4398-BAA1-7FAF8B610BDD}
{D91E6E3A-4165-4934-9462-E62B04250932} = {693C5879-B401-4398-BAA1-7FAF8B610BDD}
{A9CDBF8F-C28F-48CC-A01F-6BE76A3DD5A2} = {693C5879-B401-4398-BAA1-7FAF8B610BDD}
Expand All @@ -103,6 +102,7 @@ Global
{A7D2B85A-AF4C-4CD2-92AA-99F2933A570D} = {A9CDBF8F-C28F-48CC-A01F-6BE76A3DD5A2}
{3CC2B32C-6931-4E2E-96B6-C7E0480A9C08} = {E149B487-146C-4AA4-BFC9-4A42FBEA3E05}
{0BD82D67-CE42-4EB8-AF58-703AB143DF05} = {E149B487-146C-4AA4-BFC9-4A42FBEA3E05}
{B1D34BC0-0ACF-4649-8FC6-D83A69EDB2DC} = {BB0DD68C-4E4B-46DE-86B0-BA365448F9A2}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {4CAA1372-4CC5-492D-8CB3-3D426B8C4E6A}
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# LoadShedding · [![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/farfetch/loadshedding/blob/main/LICENSE) [![nuget version](https://img.shields.io/nuget/v/loadshedding.svg?style=flat)](https://www.nuget.org/packages/LoadShedding/) ![Build Master](https://github.com/Farfetch/loadshedding/workflows/Build/badge.svg?branch=master)
# LoadShedding · [![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/farfetch/loadshedding/blob/main/LICENSE) [![nuget version](https://img.shields.io/nuget/v/loadshedding.svg?style=flat)](https://www.nuget.org/packages/Farfetch.LoadShedding/) ![Build Master](https://github.com/Farfetch/loadshedding/workflows/Build/badge.svg?branch=main)

## Introduction

Expand Down
10 changes: 5 additions & 5 deletions docker/Sample.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,26 @@ RUN mkdir /app
WORKDIR /app

# 1 - Copy projects
COPY samples/Samples.WebApi/Samples.WebApi.csproj samples/Samples.WebApi/
COPY samples/Farfetch.LoadShedding.Samples.WebApi/Farfetch.LoadShedding.Samples.WebApi.csproj samples/Farfetch.LoadShedding.Samples.WebApi/
COPY ./*/*.csproj ./

#Restore original file paths
RUN for file in $(ls *.csproj); do mkdir -p ./${file%.*}/ && mv $file ./${file%.*}/ && echo $file; done

# 1.1 - Restore packages
RUN dotnet restore samples/Samples.WebApi/Samples.WebApi.csproj -r $RUNTIME_IDENTIFIER
RUN dotnet restore samples/Farfetch.LoadShedding.Samples.WebApi/Farfetch.LoadShedding.Samples.WebApi.csproj -r $RUNTIME_IDENTIFIER

# 2 - Copy all files
COPY . .

# 3 - Build
RUN dotnet build -c Release samples/Samples.WebApi/Samples.WebApi.csproj
RUN dotnet build -c Release samples/Farfetch.LoadShedding.Samples.WebApi/Farfetch.LoadShedding.Samples.WebApi.csproj

##
## Publish
##
FROM build AS publish
RUN dotnet publish samples/Samples.WebApi/Samples.WebApi.csproj --no-build -c Release -o /out
RUN dotnet publish samples/Farfetch.LoadShedding.Samples.WebApi/Farfetch.LoadShedding.Samples.WebApi.csproj --no-build -c Release -o /out

##
## Run
Expand All @@ -48,4 +48,4 @@ WORKDIR /out
ENV ASPNETCORE_URLS=http://+:5261

COPY --from=publish /out .
ENTRYPOINT ["dotnet", "Samples.WebApi.dll"]
ENTRYPOINT ["dotnet", "Farfetch.LoadShedding.Samples.WebApi.dll"]
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,27 @@
using Microsoft.AspNetCore.Mvc;
using MongoDB.Driver;

namespace Samples.WebApi.Controllers
namespace Farfetch.LoadShedding.Samples.WebApi.Controllers
{
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
private readonly IMongoCollection<WeatherForecast> _collection;

/// <summary>
/// Initializes a new instance of the <see cref="WeatherForecastController"/> class.
/// </summary>
/// <param name="collection"></param>
public WeatherForecastController(IMongoCollection<WeatherForecast> collection)
{
this._collection = collection;
_collection = collection;
}

/// <summary>
///
/// </summary>
/// <returns>A <see cref="Task{TResult}"/> representing the result of the asynchronous operation.</returns>
[HttpGet(Name = "GetWeatherForecast")]
[EndpointPriority(Farfetch.LoadShedding.Tasks.Priority.Critical)]
public async Task<IEnumerable<WeatherForecast>> GetAsync()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
using Farfetch.LoadShedding;
using Farfetch.LoadShedding.Samples.WebApi;
using MongoDB.Driver;
using Prometheus;
using Samples.WebApi;

var builder = WebApplication.CreateBuilder(args);

builder.Configuration.AddEnvironmentVariables();

// Add services to the container.

builder.Services.AddHttpContextAccessor();
builder.Services.AddControllers();

Expand All @@ -32,7 +31,6 @@
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();


builder.Services.AddSingleton(_ => MongoUrl.Create(builder.Configuration["MongoConnectionString"]));

builder.Services.AddSingleton<IMongoClient>(provider => new MongoClient(provider.GetRequiredService<MongoUrl>()));
Expand Down Expand Up @@ -73,15 +71,15 @@

var summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching",
};

var forecasts = Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Id = Guid.NewGuid(),
Date = DateTime.Now.AddDays(index),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = summaries[Random.Shared.Next(summaries.Length)]
Summary = summaries[Random.Shared.Next(summaries.Length)],
});

collection.InsertMany(forecasts);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Samples.WebApi
namespace Farfetch.LoadShedding.Samples.WebApi
{
public class WeatherForecast
{
Expand All @@ -12,4 +12,4 @@ public class WeatherForecast

public string? Summary { get; set; }
}
}
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ namespace Farfetch.LoadShedding.AspNetCore.Attributes
{
public class EndpointPriorityAttribute : Attribute
{
/// <summary>
/// Initializes a new instance of the <see cref="EndpointPriorityAttribute"/> class.
/// </summary>
/// <param name="priority"></param>
public EndpointPriorityAttribute(Priority priority)
{
Priority = priority;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using Farfetch.LoadShedding.AspNetCore.Options;
using System;
using System.Collections.Generic;
using Farfetch.LoadShedding.AspNetCore.Options;
using Farfetch.LoadShedding.Calculators;
using Farfetch.LoadShedding.Events;
using System;
using System.Collections.Generic;

namespace Farfetch.LoadShedding.AspNetCore.Configurators
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using Farfetch.LoadShedding.AspNetCore.Configurators;
using System;
using Farfetch.LoadShedding.AspNetCore.Configurators;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using System;

namespace Microsoft.AspNetCore.Builder
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using Farfetch.LoadShedding.AspNetCore.Resolvers;
using System;
using System.Threading.Tasks;
using Farfetch.LoadShedding.AspNetCore.Resolvers;
using Farfetch.LoadShedding.Configurations;
using Farfetch.LoadShedding.Tasks;
using Microsoft.AspNetCore.Http;
using System;
using System.Threading.Tasks;

namespace Farfetch.LoadShedding.AspNetCore.Options
{
Expand All @@ -26,7 +26,6 @@ internal AdaptativeLimiterOptions()
public AdaptativeLimiterOptions UseEndpointPriorityResolver()
=> this.UsePriorityResolver(new EndpointPriorityResolver());


///// <summary>
///// Sets the HttpHeaderPriorityResolver, it converts the header X-Priority to the request priority (critical, normal, noncritical).
///// </summary>
Expand All @@ -50,7 +49,6 @@ public AdaptativeLimiterOptions UseHeaderPriorityResolver(string headerName)
public AdaptativeLimiterOptions UsePriorityResolver(Func<HttpContext, Task<Priority>> priorityResolverFunc)
=> this.UsePriorityResolver(new CustomPriorityResolver(priorityResolverFunc));


/// <summary>
/// Sets a custom priority resolver instance.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Farfetch.LoadShedding.Tasks;
using Microsoft.AspNetCore.Http;
using System;
using System;
using System.Threading.Tasks;
using Farfetch.LoadShedding.Tasks;
using Microsoft.AspNetCore.Http;

namespace Farfetch.LoadShedding.AspNetCore.Resolvers
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Farfetch.LoadShedding.Tasks;
using System.Threading.Tasks;
using Farfetch.LoadShedding.Tasks;
using Microsoft.AspNetCore.Http;
using System.Threading.Tasks;

namespace Farfetch.LoadShedding.AspNetCore.Resolvers
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Farfetch.LoadShedding.Tasks;
using Microsoft.AspNetCore.Http;
using System;
using System;
using System.Threading.Tasks;
using Farfetch.LoadShedding.Tasks;
using Microsoft.AspNetCore.Http;

namespace Farfetch.LoadShedding.AspNetCore.Resolvers
{
Expand Down Expand Up @@ -43,7 +43,7 @@ public Task<Priority> ResolveAsync(HttpContext context)
private string NormalizeHeaderValue(string headerValue)
{
return headerValue
.Replace(Separator, String.Empty)
.Replace(Separator, string.Empty)
.ToLower();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Farfetch.LoadShedding.Tasks;
using System.Threading.Tasks;
using Farfetch.LoadShedding.Tasks;
using Microsoft.AspNetCore.Http;
using System.Threading.Tasks;

namespace Farfetch.LoadShedding.AspNetCore.Resolvers
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using Farfetch.LoadShedding.AspNetCore.Configurators;
using System;
using Farfetch.LoadShedding.AspNetCore.Configurators;
using Farfetch.LoadShedding.Prometheus;
using Farfetch.LoadShedding.Prometheus.Metrics;
using Farfetch.LoadShedding.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Prometheus;
using System;

namespace Farfetch.LoadShedding
{
Expand All @@ -18,7 +18,7 @@ public static class IAdaptativeLimiterOptionsExtensions
/// Extension method to include prometheus metrics.
/// </summary>
/// <param name="options">The LoadSheddingOptions instance.</param>
/// <param name="registry">The Prometheus registry collector.</param>
/// <param name="optionsDelegate"></param>
/// <returns>LoadSheddingOptions</returns>
public static LoadSheddingOptions AddMetrics(
this LoadSheddingOptions options,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Farfetch.LoadShedding.Prometheus.Metrics;
using Prometheus;
using Prometheus;

namespace Farfetch.LoadShedding.Prometheus
{
Expand Down
6 changes: 1 addition & 5 deletions src/Farfetch.LoadShedding.Prometheus/MetricOptions.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Farfetch.LoadShedding.Prometheus
namespace Farfetch.LoadShedding.Prometheus
{
public class MetricOptions
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ internal HttpRequestsConcurrencyItemsGauge(
{
}

/// <inheritdoc/>
protected override string DefaultName => "http_requests_concurrency_items_total";

public void Set(string method, string priority, double value)
Expand All @@ -23,14 +24,15 @@ public void Set(string method, string priority, double value)
.Set(value);
}

/// <inheritdoc/>
protected override Gauge Create(CollectorRegistry registry, MetricOptions options)
{
return PrometheusBase
.Metrics
.WithCustomRegistry(registry)
.CreateGauge(options.Name, Description, new PrometheusBase.GaugeConfiguration
{
LabelNames = new[] { MetricsConstants.MethodLabel, MetricsConstants.PriorityLabel }
LabelNames = new[] { MetricsConstants.MethodLabel, MetricsConstants.PriorityLabel },
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ internal HttpRequestsConcurrencyLimitGauge(
{
}

/// <inheritdoc/>
protected override string DefaultName => "http_requests_concurrency_limit_total";

public void Set(double value)
{
this.Metric?.Set(value);
}

/// <inheritdoc/>
protected override Gauge Create(CollectorRegistry registry, MetricOptions options)
{
return PrometheusBase
Expand Down
Loading

0 comments on commit 2e47a98

Please sign in to comment.