Git Product home page Git Product logo

actors's Introduction

actors

Build Status

Lightweight actor pattern for .NET.

Actors are units of serial execution within a broader concurrent topology. They enable the safe construction of massively parallel applications without the traditional synchronization primitives (mutexes, semaphores, and so forth). These aren't needed because rather than communicating by sharing state, actors communicate by message passing.

Hello World

An Actor implementation has a dedicated Inbox to which messages can be posted in a fire-and-forget fashion via its Send() method. They are subsequently delivered to the actor's Perform() method in the order of their submission. An actor consumes a message by calling Receive() on the inbox.

Below is a classic example, where the "Hello World" string is streamed character-by-character to a simple Printer actor. It demonstrates the basic Actor API and the order-preserving semantics of an actor's Inbox.

class Printer : Actor<char>
{
    protected override Task Perform(Inbox inbox)
    {
        var ch = inbox.Receive();
        Console.Write(ch);
        return Task.CompletedTask;
    }
}

// calling the actor
var printer = new Printer();
foreach (var ch in "Hello World\n")
{
    printer.Send(ch);
}
await printer.Drain();

Actor classes can be optionally typed with the message that the actor expects to receive, as in the above example. If the actor only expects one kind of message, extending the generic Actor<M> is recommended for added type safety. More complex actors that handle a variety of message types should extend the non-generic Actor variant and cast messages internally.

More Examples

See the Examples project for a variety of typical actor use cases. Run all examples with

just run

Pass the name of an example to run just that. For example, to run the Batch example:

just run Batch

Performance

Most of the overhead in an actor system is in the scheduling of dormant actors when they receive their first message and the subsequent unscheduling when they process their last. We'll call this the 'slow path'.

When an actor is constantly posted work at a rate close to its throughput, it will remain scheduled and the overhead is one monitor entry per Send() and one per Receive(). We'll call this the 'fast path'.

Benchmarks were run using the following setup:

Parameter Value
Harness BenchmarkDotNet v0.13.12
OS Ubuntu 22.04.3 LTS (Jammy Jellyfish)
Framework .NET SDK 8.0.101
Processor AMD EPYC 7763, 1 CPU, 4 logical and 2 physical cores, virtualised

Results:

Path Time-cost Calculation
Fast 106 ns/op FireAndForgetBenchmark, time / number of messages
Slow 1.3 µs/op EchoBenchmark, time / number of messages / 2

actors's People

Contributors

ekoutanov avatar

Stargazers

Maxx avatar Julio Litwin avatar  avatar Anil avatar Harrison Cole avatar

Watchers

Carl Johan Crafoord avatar Johan Lyheden avatar Max MacLeod avatar Johan Leinfors avatar nighter avatar  avatar Blessing Orazulume avatar Anil avatar Fab avatar Lu Han avatar  avatar

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.