hollywood
repository·master·Indexed 25 days ago
https://github.com/anthdm/hollywoodA high-performance, low-latency actor engine for Golang designed for real-time applications such as game servers, trading engines, and advertising brokers. It implements the actor model using the Receiver interface, supporting asynchronous messaging, synchronous requests, actor hierarchies for supervision, and remote communication via the remote package. Features include configurable inboxes with backpressure, tag-based routing, middleware for interception, and an Eventstream for system monitoring.
What's inside hollywood
- The Hollywood engine is the central core of the actor model. It manages the entire lifecycle of actors, including spawning new actors, sending messages to them, and stopping them.
Understand the Trade-Engine Actor Pattern
masterThe Trade-Engine example demonstrates a hierarchical actor system designed for monitoring and executing trades. It uses three distinct actor roles to manage lifecycle and responsibilities:
- Trade Engine Actor: The central management hub. It is responsible for spawning and overseeing the lifecycle of
Price WatcherandTrade Executoractors. - Price Watcher: A ticker-specific actor that monitors prices. It uses
(*actor.Engine).SendRepeatto trigger periodic updates. It manages its own lifecycle by checking for active subscribers; if no subscribers exist, it stops its repetition via(actor.SendRepeater).Stop()and terminates itself using(*actor.Engine).Poison. - Trade Executor: An actor that manages individual trade logic. It subscribes to a
Price Watcherfor a specific ticker. Upon receiving aPriceUpdate, it evaluates trade parameters. If a trade is canceled, it sends anUnsubscribemessage to thePrice Watcherand terminates itself using(*actor.Engine).Poison.
- Trade Engine Actor: The central management hub. It is responsible for spawning and overseeing the lifecycle of
Shutting down actors with PoisonPill
masterTo gracefully shut down an actor, send it aPoisonPillmessage. This signal instructs the actor to terminate.Add Middleware to Receivers
masterYou can implement custom middleware for yourReceiverimplementations. Middleware is useful for cross-cutting concerns like storing metrics, or saving/loading state duringactor.Startedandactor.Stoppedlifecycle events.Using Middleware to intercept messages
masterMiddleware allows you to intercept messages before they reach the actor. This pattern is used to implement cross-cutting concerns such as:
- Logging
- Metrics
- Tracing
How the Actor Model works in Hollywood
masterIn Hollywood, the basic building block is an actor (also called a
Receiver). Actors are independent units of computation that communicate exclusively via message passing. Each actor maintains its own state and behavior.Actors can be organized into hierarchies where higher-level actors supervise lower-level ones. This allows for scalable, fault-tolerant systems where actors can operate independently even if others fail.
Routing messages with Tags
masterActors can be assigned an arbitrary number of Tags. These tags are used for message routing. You can broadcast a message to all actors that possess a specific tag.How to handle events and errors
masterBecause Hollywood is an asynchronous system, many issues that would typically be returned as errors are instead broadcasted as Events via an Event Stream attached to the
Engine.One of the most critical events is the
DeadLetterevent, which is broadcast when a message is sent to an actor that either does not exist or cannot be reached. Refer toevents.gofor a complete list of available events.Using Context in actors
masterThe
Contextis a struct passed to all user-supplied actors. It serves two primary purposes:- Dependency Injection: It should contain all the dependencies an actor requires to perform its work.
- Communication: It is used by the actor to send messages to other actors.
Use the Eventstream for system monitoring
masterThe
Eventstreamallows you to subscribe to system-wide events to handle failures gracefully. You can subscribe an actor to a list of events or broadcast custom events.Commonly used events include:
actor.DeadLetterEvent: Sent when a message cannot be delivered to an actor.actor.ActorStartedEvent/actor.ActorStoppedEvent: Lifecycle events.actor.ActorRestartedEvent: When an actor restarts after a panic.cluster.MemberJoinEvent/cluster.MemberLeaveEvent: Cluster membership changes.
Important: If no actor is subscribed to the event stream, events will be dropped. It is highly recommended to have at least one actor monitoring
DeadLetterEvent.How actors and receivers work
masterIn Hollywood, an actor is defined by implementing the
Receiverinterface. This interface is the mechanism the engine uses to communicate with the actor.Parent & Child Relationships
Actors can form hierarchies. If an actor spawns another actor, the spawning actor becomes the parent of the spawned actor. This hierarchy is primarily used for:
- Supervision: Managing the lifecycle and errors of child actors.
- Logical Routing: Facilitating the flow of messages through a structured tree.
Manage Actor Lifecycles with Poison and SendRepeat
masterIn the Trade-Engine pattern, actors manage their own lifecycle based on system state using the following
hollywoodmechanisms:- Periodic Tasks: Use
(*actor.Engine).SendRepeatto send a specific message to an actor at regular intervals (e.g., for price updates). - Stopping Repetition: Use
(actor.SendRepeater).Stop()to halt a recurring message loop. - Self-Termination: Use
(*actor.Engine).Poisonto signal an actor to shut down once its task is complete or it is no longer needed (e.g., when aPrice Watcherhas no more subscribers or aTrade Executorhas finished a trade).
- Periodic Tasks: Use