Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
1b40ffd
created new project
sammychinedu2ky Jul 15, 2022
d9e11e7
added channels to serve as queue
sammychinedu2ky Jul 15, 2022
7974f86
added a background service
sammychinedu2ky Jul 15, 2022
d6921a6
created project
sammychinedu2ky Jul 15, 2022
994cd82
added channels to serve as a queue
sammychinedu2ky Jul 15, 2022
f9f0df6
created background file
sammychinedu2ky Jul 15, 2022
ad7f789
added background service
sammychinedu2ky Jul 15, 2022
f51a27b
added curl comment
sammychinedu2ky Jul 15, 2022
be77f8f
added curl comment
sammychinedu2ky Jul 15, 2022
9b8c837
removed some files
sammychinedu2ky Jul 15, 2022
dfd3537
blank lines added
sammychinedu2ky Jul 15, 2022
293670b
add suggestion to batch
sammychinedu2ky Jul 15, 2022
f01c8da
logger added
sammychinedu2ky Jul 15, 2022
16bc512
logging information
sammychinedu2ky Jul 15, 2022
c53de06
log error
sammychinedu2ky Jul 15, 2022
833ac81
Edit: CamelCasing
sammychinedu2ky Jul 15, 2022
5b7fba6
CamelCasing
sammychinedu2ky Jul 15, 2022
6abd29f
Added some modifiers
sammychinedu2ky Jul 15, 2022
f0bdd46
edit: cancellation token
sammychinedu2ky Jul 15, 2022
d880cfe
edit: CamelCasing on backgroundservice
sammychinedu2ky Jul 15, 2022
0113f8a
added cancellation token
sammychinedu2ky Jul 15, 2022
2de82f0
edit: changed casing
sammychinedu2ky Jul 16, 2022
044b94f
changed casing of background
sammychinedu2ky Jul 16, 2022
6235a5b
return status code 202
sammychinedu2ky Jul 16, 2022
abb678e
removed unnecessary middleware
sammychinedu2ky Jul 16, 2022
81067fb
changed from stream to byte
sammychinedu2ky Jul 16, 2022
fa3f454
changed channel type to memory<byte>
sammychinedu2ky Jul 16, 2022
a70571e
removed unnecessary endpoint
sammychinedu2ky Jul 16, 2022
528f021
remove line
sammychinedu2ky Jul 16, 2022
19ef786
changed from Memory<byte> to ReadOnlyMemory<byte>
sammychinedu2ky Jul 16, 2022
9b9ccce
Merge branch 'minimal-api-stream-sample-sam' of https://github.com/sa…
sammychinedu2ky Jul 16, 2022
2e9f1e5
added a line
sammychinedu2ky Jul 16, 2022
072d6f6
use buffer to avoid double allocation
sammychinedu2ky Jul 16, 2022
cb32457
added a comment
sammychinedu2ky Jul 16, 2022
38a5ee7
added a comment
sammychinedu2ky Jul 16, 2022
dba2da5
reduced bounded size
sammychinedu2ky Jul 16, 2022
5fdd2bd
add: spacing
sammychinedu2ky Jul 16, 2022
8d707f3
Merge branch 'minimal-api-stream-sample-sam' of https://github.com/sa…
sammychinedu2ky Jul 16, 2022
7311686
increase bounded size
sammychinedu2ky Jul 16, 2022
41f3c07
add bounded constraint
sammychinedu2ky Jul 16, 2022
e247286
confirming request-size
sammychinedu2ky Jul 16, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System.Text.Json;
using System.Threading.Channels;

namespace BackgroundQueueService;

class BackgroundQueue : BackgroundService
{
private readonly Channel<ReadOnlyMemory<byte>> _queue;
private readonly ILogger<BackgroundQueue> _logger;

public BackgroundQueue(Channel<ReadOnlyMemory<byte>> queue, ILogger<BackgroundQueue> logger)
{
_queue = queue;
_logger = logger;
}

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
await foreach (var dataStream in _queue.Reader.ReadAllAsync(stoppingToken))
{
try
{
var person = JsonSerializer.Deserialize<Person>(dataStream.Span)!;
_logger.LogInformation($"{person.Name} is {person.Age} years and from {person.Country}");
// you could do something else with the data
}
catch (Exception ex)
{
_logger.LogError(ex.Message);
}
}
}
}

class Person
{
public string Name { get; set; } = String.Empty;
public int Age { get; set; }
public string Country { get; set; } = String.Empty;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using System.Threading.Channels;
using BackgroundQueueService;

var builder = WebApplication.CreateBuilder(args);
// The max memory we're willing to use for the upload endpoint on this instance
var maxMemory = 500 * 1024 * 1024;

// The max size of a single message, we're staying below the default LOH size of 85K
var maxMessageSize = 80 * 1024;

// The max size of the queue based on those restrictions
var maxQueueSize = maxMemory / maxMessageSize;

// Create a channel to send data to the background queue.
builder.Services.AddSingleton<Channel<ReadOnlyMemory<byte>>>((_) => Channel.CreateBounded<ReadOnlyMemory<byte>>(maxQueueSize));

// Create a background queue service.
builder.Services.AddHostedService<BackgroundQueue>();
var app = builder.Build();

// curl --request POST 'http://localhost:5256/register' --header 'Content-Type: application/json' --data-raw '{ "Name":"Samson", "Age": 23, "Country":"Nigeria" }'
app.MapPost("/register", async (HttpRequest req, Stream body, Channel<ReadOnlyMemory<byte>> queue) =>
{
if (req.ContentLength is not null && req.ContentLength > maxMessageSize)
{
// Message size exceeded
return Results.BadRequest();
}

// We're not above the message size and we have a content length, or
// we're a chunked request and we're going to read up to the maxMessageSize + 1.
// We add one to the message size so that we can detect when a chunked request body
// is bigger than our configured max.
var readSize = (int?)req.ContentLength ?? (maxMessageSize + 1);

var buffer = new byte[readSize];

// Read at least that many bytes from the body
var read = await body.ReadAtLeastAsync(buffer, readSize, throwOnEndOfStream: false);

// We read more than the max, so this is a bad request
if (read > maxMessageSize)
{
// Message size exceeded
return Results.BadRequest();
}

// Attempt to send the buffer to the background queue.
if (queue.Writer.TryWrite(buffer.AsMemory(0..read)))
{
return Results.Accepted();
}

// We couldn't accept the message since we're overloaded
return Results.StatusCode(StatusCodes.Status429TooManyRequests);
});


app.Run();
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}

This file was deleted.