Reactor pattern
Encyclopedia
The reactor design pattern
Design pattern (computer science)
In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design. A design pattern is not a finished design that can be transformed directly into code. It is a description or template for how to solve a problem that...

 is an event handling pattern for handling service requests delivered concurrently
Concurrency (computer science)
In computer science, concurrency is a property of systems in which several computations are executing simultaneously, and potentially interacting with each other...

 to a service handler by one or more inputs. The service handler then demultiplexes the incoming requests and dispatches them synchronously to the associated request handlers.

Structure

  • Resources: Any resource that can provide input from or output to the system.
  • Synchronous Event Demultiplexer: Uses an event loop
    Event loop
    In computer science, the event loop, message dispatcher, message loop, message pump, or run loop is a programming construct that waits for and dispatches events or messages in a program...

     to block on all resources. When it is possible to start a synchronous operation on a resource without blocking, the demultiplexer sends the resource to the dispatcher.
  • Dispatcher: Handles registering and unregistering of request handlers. Dispatches resources from the demultiplexer to the associated request handler.
  • Request Handler: An application defined request handler and its associated resource.

Properties

All reactor systems are single threaded by definition, but can exist in a multithreaded
Thread (computer science)
In computer science, a thread of execution is the smallest unit of processing that can be scheduled by an operating system. The implementation of threads and processes differs from one operating system to another, but in most cases, a thread is contained inside a process...

 environment.

Benefits

The reactor pattern completely separates application specific code from the reactor implementation, which means that application components can be divided into modular, reusable parts. Also, due to the synchronous calling of request handlers, the reactor pattern allows for simple coarse-grain concurrency while not adding the complexity of multiple threads to the system.

Limitations

The reactor pattern can be more difficult to debug
Debugging
Debugging is a methodical process of finding and reducing the number of bugs, or defects, in a computer program or a piece of electronic hardware, thus making it behave as expected. Debugging tends to be harder when various subsystems are tightly coupled, as changes in one may cause bugs to emerge...

than a procedural pattern due to the inverted flow of control. Also, by only calling request handlers synchronously, the reactor pattern limits maximum concurrency, especially on SMP
Symmetric multiprocessing
In computing, symmetric multiprocessing involves a multiprocessor computer hardware architecture where two or more identical processors are connected to a single shared main memory and are controlled by a single OS instance. Most common multiprocessor systems today use an SMP architecture...

 hardware. The scalability of the reactor pattern is limited not only by calling request handlers synchronously, but also by the demultiplexer. The original Unix select
Select (Unix)
select is a system call and application programming interface in Unix-like and POSIX-compliant operating systems for examining the status of file descriptors of open input/output channels...

 and poll calls, for instance, have a maximum number of descriptors that may be polled and have performance issues with a high number of descriptors. (More recently, more scalable variants of these interfaces have been made available: /dev/poll in Solaris, epoll
Epoll
epoll is a scalable I/O event notification mechanism for Linux, first introduced in Linux 2.5.44 . It is meant to replace the older POSIX select and poll system calls, to achieve better performance in more demanding applications, where the number of watched file descriptors is large...

in Linux and kqueue / kevent
Kqueue
Kqueue is a scalable event notification interface introduced in FreeBSD 4.1, also supported in NetBSD, OpenBSD, DragonflyBSD, and Mac OS X. It's the foundation of Apple's Grand Central Dispatch....

 in BSD-based systems, allowing the implementation of very high performance systems with large numbers of open descriptors.)

C++


Java

  • Apache MINA
    Apache MINA
    Apache MINA is an open source Java network application framework. MINA can be used to create scalable, high performance network applications. MINA provides unified API's for various transports like TCP, UDP, serial communication. It also makes it easy to make an implementation of custom transport...

  • Apache Cocoon
    Apache Cocoon
    Apache Cocoon, usually just called Cocoon, is a web application framework built around the concepts of pipeline, separation of concerns and component-based web development. The framework focuses on XML and XSLT publishing and is built using the Java programming language...

     (for XML processing)
  • JBoss Netty
    JBoss Netty
    JBoss Netty is a New I/O client-server framework for the development of Java network applications such as protocol servers and clients. The asynchronous event-driven network application framework and tools is used to simplify network programming such as TCP and UDP socket servers. Netty includes...

  • xSocket

Lua


Perl

  • AnyEvent
  • POE
    Perl Object Environment
    The Perl Object Environment or POE is a library of Perl modules written in the Perl programming language by Rocco Caputo et al.From CPAN:-POE Architecture: Layers of Abstraction :...


Python

  • Gevent
  • Twisted
    Twisted (software)
    Twisted is an event-driven network programming framework written in Python and licensed under the MIT License.Twisted projects variously support TCP, UDP, SSL/TLS, IP Multicast, Unix domain sockets, a large number of protocols , and much more...

  • Eventlet

Ruby


See also

  • Proactor pattern
    Proactor pattern
    Proactor is a software design pattern for Event handling in which long running activities are running in an asynchronous part. A Completion Handler is called after the asynchronous part has terminated....

     (a pattern that also demultiplexes and dispatches events, but asynchronously)

External links

The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK