An open API service indexing awesome lists of open source software.

https://github.com/nikiforovall/channels-composition-and-otel

Job Offloading Pattern using an a pipeline built with System.Threading.Channels and OpenTelemetry
https://github.com/nikiforovall/channels-composition-and-otel

Last synced: about 1 month ago
JSON representation

Job Offloading Pattern using an a pipeline built with System.Threading.Channels and OpenTelemetry

Awesome Lists containing this project

README

        

# System.Threading.Channels Composition and OpenTelemetry. Job Offloading Pattern

The project demonstrates how to build an in-memory pipeline that allows to offload long-running tasks. As a result, we have an improved user experience because the result is returned as soon as a task is scheduled.

```csharp
app.MapPost(
"/start",
async (
[FromBody] Payload payload,
IBackgroundProcessor writer,
CancellationToken cancellationToken
) =>
{
await writer.QueueBackgroundWorkItemAsync(payload, cancellationToken);

return TypedResults.Ok(new { Success = true });
}
);
```

```csharp
public class ProcessorChannel : IBackgroundProcessor
{
public ProcessorChannel()
{
var options = processorOptions.Value;
_pipeline = Channel.CreateBounded(10);

Writer = _pipeline;
Reader = _pipeline
.Pipe(InitPipeline, capacity: 10)
.PipeAsync(
maxConcurrency: 5,
Step1,
capacity: 10
)
.PipeAsync(
maxConcurrency: 2,
Step2,
capacity: 10
)
.Pipe(FinishPipeline);
}
}
```

![producer-consumer](/assets/producer-consumer.png)

## Demo

```bash
dotnet run --project ./src/AppHost
```

```bash
./scripts/bombardier.sh 100
```

`ProcessorChannelSettings.UseUnifiedSpanForAllPipelines=false`

![pipeline-trace](/assets/pipeline-trace.png)

`ProcessorChannelSettings.UseUnifiedSpanForAllPipelines=true`

![batch-trace](/assets/batch-trace.png)

```bash
dotnet-counters monitor -n API --counters MyService.Pipelines
```

![metrics](/assets/metrics.png)

## Reference

*
*
*
*
*
*
*
*
*