https://github.com/david-desmaisons/composableasync
Create, compose and inject asynchronous behaviors in .Net Framework and .Net Core.
https://github.com/david-desmaisons/composableasync
actor-model aop async-programming asynchronous c-sharp concurrency task thread
Last synced: about 1 month ago
JSON representation
Create, compose and inject asynchronous behaviors in .Net Framework and .Net Core.
- Host: GitHub
- URL: https://github.com/david-desmaisons/composableasync
- Owner: David-Desmaisons
- License: mit
- Created: 2015-10-03T16:29:00.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2020-11-02T21:11:46.000Z (almost 5 years ago)
- Last Synced: 2025-08-19T15:10:42.971Z (about 2 months ago)
- Topics: actor-model, aop, async-programming, asynchronous, c-sharp, concurrency, task, thread
- Language: C#
- Homepage: http://david-desmaisons.github.io/ComposableAsync/
- Size: 2.04 MB
- Stars: 32
- Watchers: 6
- Forks: 10
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Composable Async
================[](https://ci.appveyor.com/project/David-Desmaisons/ComposableAsync)
[](https://www.nuget.org/packages/ComposableAsync.Core/)
[](https://github.com/David-Desmaisons/ComposableAsync/blob/master/LICENSE)Create, compose and inject asynchronous behaviors in .Net Framework and .Net Core.
# Goal
* Create asynchronous behavior such as [fiber](https://www.wikiwand.com/en/Fiber_(computer_science)), rate limiter, [circuit breaker](https://www.wikiwand.com/en/Circuit_breaker_design_pattern).
* Compose these behaviors and use them as building blocks with [aspect oriented programming](https://www.wikiwand.com/en/Aspect-oriented_programming).
* Provide a lightweight way inject these behaviors to transform POCOs in [actors](https://en.wikipedia.org/wiki/Actor_model).
# Create asynchronous behavior
Asynchronous behaviors are implemented using [IDispatcher abstraction](http://david-desmaisons.github.io/ComposableAsync/core-api/ComposableAsync.IDispatcher.html).
Composable Async provides various dispatchers implementation:
## Retry
```C#
// Create dispatcher that catch all ArgumentException and retry for ever with a delay of 200 ms
var retryDispatcher = RetryPolicy.For()
.WithWaitBetweenRetry(TimeSpan.FromSeconds(0.2))
.ForEver();
```See more at [ComposableAsync.Resilient](http://david-desmaisons.github.io/ComposableAsync/resilient-api/index.html#retrypolicy)
## Circuit-Breaker
```C#
// Create dispatcher that catch all ArgumentException and retry for ever with a delay of 200 ms
var retryDispatcher = CircuitBreakerPolicy.For()
.WithRetryAndTimeout(10, TimeSpan.FromMilliseconds(500));
```See more at [ComposableAsync.Resilient](http://david-desmaisons.github.io/ComposableAsync/resilient-api/index.html#circuitbreakerpolicy)
## Fiber
```C#
// Create dispatcher that dispatch all action on the same thread
var fiberDispatcher = Fiber.CreateMonoThreadedFiber();
```See more at [ComposableAsync.Concurrent](http://david-desmaisons.github.io/ComposableAsync/concurrent-api/index.html)
## RateLimiter
```C#
// Create dispatcher that dispatch all action on the same thread
var timeConstraint = TimeLimiter.GetFromMaxCountByInterval(5, TimeSpan.FromSeconds(1));
```See more at [RateLimiter](https://github.com/David-Desmaisons/RateLimiter)
# Compose dispatchers
Use then extension methods to create a dispatcher that will execute sequentially dispatchers
```C#
///
/// Returns a composed dispatcher applying the given dispatchers sequentially
///
///
///
///
public static IDispatcher Then(this IDispatcher dispatcher, IEnumerable others)
``````C#
var composed = fiberDispatcher.Then(timeConstraint);
```# Use dispatchers
## Await dispatcher
```C#
await fiberDispatcher;
// After the await, the code executes in the dispatcher context
// In this case the code will execute on the fiber thread
Console.WriteLine($"This is fiber thread {Thread.CurrentThread.ManagedThreadId}");
```## As httpDelegateHandler
Transform a dispatcher into [HttpMessageHandler](https://docs.microsoft.com/en-us/dotnet/api/system.net.http.httpmessagehandler?view=netframework-4.8) with AsDelegatingHandler extension method:
```C#
/// Using time limiter nuget
var handler = TimeLimiter
.GetFromMaxCountByInterval(60, TimeSpan.FromMinutes(1))
.AsDelegatingHandler();
var client = new HttpClient(handler);
```## As wrapper for proxy Factory
Using `ComposableAsync.Factory`, with this option all methods call to the proxyfied object are wrapped using the provided dispatcher.
```C#
var retryDispatcher = RetryPolicy.For().ForEver();var originalObject = new BusinessObject();
var proxyFactory = new ProxyFactory(retryDispatcher);
var proxyObject = proxyFactory.Build(originalObject);// The call to the originalObject will be wrapped into a retry policy for SystemException
var res = await proxyObject.Execute(cancellationToken);
```# Actors
`ComposableAsync.Concurrent` also provides an [actor](https://en.wikipedia.org/wiki/Actor_model) factory based on fiber and proxy factory.
```C#
// Instantiate actor factory
var builder = new ActorFactoryBuilder();
var factory = builder.GetActorFactory(shared: false);
// When shared is true, all actor leaves in the same thread,
// when shared is false, each actor leaves in its own thread.// Instantiate an actor from a POCO
var fooActor = fact.Build(new ConcreteFoo());
```See more at [ComposableAsync.Concurrent](http://david-desmaisons.github.io/ComposableAsync/concurrent-api/index.html)
# Nuget
For core functionality:
```
Install-Package ComposableAsync.Core
```For factories:
```
Install-Package ComposableAsync.Factory
```For actors:
```
Install-Package ComposableAsync.Concurrent
```For retry and circuit-breaker:
```
Install-Package ComposableAsync.Resilient
```[Go nuget packages](https://www.nuget.org/packages/ComposableAsync.Core/)