Introduction
Avalanche service is a class library that helps in mitigating consumers and producers. Service is an object that converts a request into a response by transporting a query through a stack of handlers.
// Create handler
Action<IQuery<string, Type>> handler = q => q.Response.SetValue( Type.GetType(q.Request) );
// Create service
IService service = Services.Create(handler);
// Create request
string request = "System.String";
// Issue request
Type? response = service.Get<string, Type>(request);
Information
Class libraries:
- Avalanche.Service.Abstractions - Interfaces and abstractions.
- Avalanche.Service - Service implementations.
- Avalanche.Service.DependencyInjection - Microsoft.Extensions.DependencyInjection support.
- Avalanche.Service.Extra - Extra handlers. Comes with some external dependencies.
Interoperability:
- .NET 5.0.
Features
- Zero heap allocation / very low heap allocation
- Very high performance
- Value-typed requests
- Strong generics on consumer side
- Strong gneerics on producer side
- Sync & async on consumer side
- Sync & async on producer side
- Scope hierarchies
- Recursive requests
- Cyclic requests
- Request specific cancel tokens
- Service life-time specific cancel tokens
- Complete dispose/ownership management of services and cached values
- Error management within and outside process
- Dependency-injection supported
- Logging supported
- Cache lines can be disconnected
Benchmarks
When getting a result that is already in cache, the result is provided at 277ns and with zero heap allocation.
When constructing a new result, there is total overhead of 700ns for about 10 handlers, and very log heap allocation.
When using async call model for creating a new result for about 10 handlers, there is 4,106ns overhead.
When using async call model for getting a cached result, the result is provided at 490ns.
Method | Mean | Error | StdDev | Gen 0 | Gen 1 | Allocated |
---|---|---|---|---|---|---|
GetWithCache | 277.5 ns | 4.03 ns | 3.57 ns | - | - | - |
GetWithoutCache | 700.1 ns | 12.46 ns | 11.65 ns | 0.0381 | - | 320 B |
GetAsyncWithCache | 490.1 ns | 4.26 ns | 3.99 ns | 0.0324 | - | 272 B |
GetAsyncWithoutCache | 4,106.3 ns | 67.99 ns | 63.59 ns | 0.2060 | - | 1720 B |