Giter Site home page Giter Site logo

fibrous's Introduction

Banner

Fibrous

NuGet

High performance concurrency library for the .Net platform. Fibrous is a fork of Retlang [http://code.google.com/p/retlang/].

Fibrous is an actor-like framework but more of a flexible set of concurrency components. The main abstractions are Fibers, Ports and Channels. Fibers are execution contexts, Ports are messaging end points, and channels are combinations of ports that allow decoupling of fiber components.

Some of the library benefits:

  • Tiny library that makes multi-threading simple and easy to reason about
  • Thread safe publishing
  • Single or multiple subscribers
  • Request reply
  • UI fibers for worry free UI marshalling
  • Batching support
  • Scheduling support (Cron, DateTime and TimeSpan based)

Fibrous is great for multi-threading when you don't need extreme low latency or distributed actors but want an easy to reason about and extremely flexible messaging based model. Fibrous is also fast. It's in production use in multiple trading systems, both server side and front end.

If you need distributed concurrency, look into Akka.net or Proto.Actor and if you need extreme performance and super low latency, look into Disruptor.net.

Fibers

Fibers are synchronous execution contexts that maintain order of actions. Like Actors, Fibers can manage state without worries of cross threading issues. While a Fiber is synchronous, your system can consist of multiple Fibers communicating through messaging to provide parallelism to your system.

In version 3, Async Fibers were introduced. These work off of Func<Task> rather than Action delegates allowing a fiber based component to use async/await safely.

Fibers subscribe to channels to receive messages which queue actions based on the assigned handler. Fibers have a scheduling API that allows actions to be scheduled in the future as well as repeatedly. You can also directly queue actions onto a Fiber for it to execute.

Fibers are a repository of IDisposable objects and will dispose of all children upon the Fibers disposal. This is used to clean up subscriptions and scheduling for any fiber. This is also useful for dealing with children used in the Fiber's context that implement IDisposable.

There are specialised Fibers for Windows Forms and WPF, which automatically handle invoking actions on the UI/Dispatcher thread. There is a StubFiber, which is used for testing and special cases, and immediately executes actions on the calling thread.

//Representations of the IFiber and IAsyncFiber interface.
//There are many extensions to enable more complex behavior~~~~
public interface IFiber : IDisposable
{
    void Enqueue(Action action);
    IDisposable Schedule(Action action, TimeSpan dueTime);
    IDisposable Schedule(Action action, TimeSpan startTime, TimeSpan interval);
    void Add(IDisposable toAdd);
    void Remove(IDisposable toRemove);
}

public interface IAsyncFiber : IDisposable
{
    void Enqueue(Func<Task> action);
    IDisposable Schedule(Func<Task> action, TimeSpan dueTime);
    IDisposable Schedule(Func<Task> action, TimeSpan startTime, TimeSpan interval);
    void Add(IDisposable toAdd);
    void Remove(IDisposable toRemove);
}
//Work is done on the thread pool, but in a sequential fashion
IFiber fiber = new Fiber();

//You can enqueue methods
fiber.Enqueue(SomeParameterlessMethod);

//or lambdas
fiber.Enqueue(() => DoSomeWork(someParam));

//You can schedule when things happen
fiber.Schedule(ScheduledMethod, when);

//and also have them repeat
fiber.Schedule(ScheduledMethod, startWhen, repeatInterval);

Ports and Channels

Ports are the end points for publishing and subscribing to messages.

The port types (ISubscriberPort, IPublisherPort, IRequestPort<TRequest, TReply>, ISnapshotSubscriberPort<T, TSnapshot>, IEventPort and IEventTrigger) allow a variety of behavior around message passing and events.

Channels are the conduit for message passing between Fibers, and allow decoupling of the parts of your system. There are a variety of channel types built into Fibrous: one way channels that notify all subscribers, request/reply, queue channels that give messages to one of multiple subscribers, as well as a state channel that acts like a last value cache.

There is a static EventBus which allows a simpler mechanism for passing messages when only one normal channel per type is needed and an EventHub that allows auto-wiring of handlers.

There are a variety of subscription methods, including filtered, batched, keyed batched and the last message after a time interval.

Examples:

IFiber fiber = new Fiber();

//Create a channel and subscribe to messages
var channel = new Channel<string>();

channel.Subscribe(fiber, s => Console.WriteLine(s.ToUpper()));

//You can also create a channel and subscribe in one line
var channel = fiber.NewChannel<string>(s => Console.WriteLine(s.ToUpper()));

//Publish a message to the channel
channel.Publish("the message");

//EventBus... Global static channel per type (string is only used as an example)
EventBus<string>.Subscribe(fiber, s => Console.WriteLine(s.ToUpper()));

EventBus<string>.Publish("the message");


//Agents make some things even simpler
var agent = new Agent<string>(s => Console.WriteLine(s.ToUpper()));
agent.Publish("the message");

fibrous's People

Contributors

chrisa23 avatar jtone123 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

fibrous's Issues

Question

For a given Channel, is it legal to create one Subscription on one Fiber, and another Subscription on a different Fiber. I tried that for two message sinks that should receive the same messages, but handle them separately and write them to different destinations. It appears that the first Subscription works fine, but the second is ignored.

Very likely I messed something up, but just want to verify that the scenario above is legal in Fibrous.

Thanks,

Possible to use this with Unity3d?

Hey there,
I know this is probably a long shot but I am using Unity3d 2018.1 which uses .net framework 4.6 and I was told that now since it uses a higher version of .net that I should be able to utilize more of the TPL. I have a game server framework that was originally made to work in a Windows console application that was setup using ThreadFiber, PoolFiber, a things of that nature but I am trying to make it work within Unity instead of within the Windows console application which requires (from what I userstand) things to be moved to the main thread when executed or something of that nature.

Do you happen to have any experience or knowledge on how I might be able to use this system with Unity3d so I can convert what I currently have?

Thanks!

`Connect` extension method doesn't use its `fiber` parameter

Hi! I'm just started to explore your library and found this strange code:

        /// <summary>
        /// Quick way to connect a Subscriber port to a Publisher port.  Useful connecting channels and Agents
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="port"></param>
        /// <param name="fiber"></param>
        /// <param name="receive"></param>
        /// <returns></returns>
        public static IDisposable Connect<T>(this ISubscriberPort<T> port,
            IFiber fiber,
            IPublisherPort<T> receive)
        {
            return port.Subscribe(StubFiber.StartNew(), x => receive.Publish(x));
        }

I'm not sure how it should look like. I suppose that it is a copy-paste error and instead of StubFiber.StartNew() here should be the fiber argument. If not then perhaps you can just remove this unused argument.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.