COROIO: NNet::NActors::TThreadedNode< TPoller, TLock > Class Template Reference
COROIO
Loading...
Searching...
No Matches
NNet::NActors::TThreadedNode< TPoller, TLock > Class Template Referencefinal

INode that writes to its socket on another thread. More...

#include <threaded_node.hpp>

Inheritance diagram for NNet::NActors::TThreadedNode< TPoller, TLock >:
NNet::NActors::INode NNet::NActors::IOutputStream

Public Types

using TSocket = typename TPoller::TSocket

Public Member Functions

 TThreadedNode (TPoller &poller, TMessagesFactory &factory, TResolver &resolver, const std::function< TSocket(const TAddress &)> &socketFactory, const THostPort &hostPort)
 Constructs the node and its I/O coroutine; does not connect.
 ~TThreadedNode ()
TLock & ProducerLock ()
 The lock of the producer side. Hold it across Offset, Acquire and Commit of one frame.
size_t Segments () const
void Send (TEnvelope &&envelope) override
 Serializes envelope and appends it to the outbound buffer.
std::span< char > Acquire (size_t size) override
void Commit (size_t size) override
 Marks size previously acquired bytes as ready to send.
uint64_t Offset () const override
void StartConnect () override
 Initiates an async TCP connection if not already connected or connecting.
void Drain () override
 Calls StartConnect() then asynchronously writes all buffered bytes to the socket.
THostPort GetHostPort () const override
 Returns the remote host:port this node connects to.

Static Public Attributes

static constexpr size_t MaxSpareSegments = 4

Detailed Description

template<typename TPoller, typename TLock = TNullLock>
class NNet::NActors::TThreadedNode< TPoller, TLock >

INode that writes to its socket on another thread.

The node has two sides:

  • Producer side — the thread of the actor system. Send, Acquire, Commit and Offset put frames into a segment in memory, exactly as TNode puts them into its buffer. Drain hands the segment to the I/O side and takes an empty one.
  • I/O side — a coroutine on poller, which belongs to another thread (see TLoopThread). It connects, writes the segments to the socket in the order of Drain calls and returns them for reuse. At most MaxSpareSegments written segments are kept, each with the memory of its buffer; the rest are deleted.

The byte stream is the one TNode writes: reconnects with a 1 s back-off, a failed write drops the segment being written, and a reconnected stream starts with a filler frame once a frame with PayloadAlignment has been written (see WriteFiller). Unlike TNode, committed bytes are not sent until the next Drain.

Send defers the serialization of a Near object of a type registered with TMessagesFactory::RegisterPayloadWriter, which states what such a type must allow. The producer side takes the size of its payload and frames it, so the frame keeps its place among the frames written around it and the stream stays that of TNode. The I/O side writes the payload into the frame with SerializeTo before the segment goes to the socket, and destroys the object there; it does not read the factory. All other payloads are serialized in Send, as by TNode. A segment grows without zero-filling (TOutputBuffer): the producer writes the headers, the padding and the other payloads, the I/O side the deferred ones, and every byte of a frame is written before it is sent.

TLock guards the producer side. With the default TNullLock it has one producer thread. With a real lock (std::mutex) any thread may produce: Send, StartConnect and Drain take the lock themselves, and a caller of Offset, Acquire and Commit holds ProducerLock() across the three calls that write one frame.

TResolver resolver(io.Poller());
auto& poller = io.Poller();
sys.AddNode(2, std::make_unique<TThreadedNode<TDefaultPoller>>(
poller, factory, resolver,
[&poller](const TAddress& addr) { return TDefaultPoller::TSocket(poller, addr.Domain()); },
THostPort("host", 9002)));
io.Start();
...
io.Stop();
io.Join();
// the actor system and its nodes are destroyed here, before io
TThreadedNode(TPoller &poller, TMessagesFactory &factory, TResolver &resolver, const std::function< TSocket(const TAddress &)> &socketFactory, const THostPort &hostPort)
Constructs the node and its I/O coroutine; does not connect.
Definition threaded_node.hpp:101
A class representing an IPv4 or IPv6 address (with port).
Definition address.hpp:38
int Domain() const
Gets the domain (address family) of the stored address.
Definition address.cpp:45
A host:port pair that resolves to a TAddress.
Definition resolver.hpp:261
A thread that runs its own event loop.
Definition loop_thread.hpp:61
void Join()
Waits for the loop thread to exit. Owner thread only; does nothing if it is not running.
Definition loop_thread.hpp:131
TPoller & Poller()
The poller of the loop. See the class description for which thread may use it.
Definition loop_thread.hpp:86
void Start(TFailureHook onFailure={})
Starts the loop thread. Call once, from the owner thread.
Definition loop_thread.hpp:99
NNet::TSocket TSocket
Alias for the socket type.
Definition poll.hpp:42
Async DNS resolver over UDP.
Definition resolver.hpp:115
Template Parameters
TPollerPoller type of the I/O thread; determines the socket type.
TLockBasicLockable guarding the producer side.
Note
Construct the node before the loop of poller starts and destroy it after that loop has stopped: both touch the poller. resolver and the sockets of socketFactory must belong to the same poller.
Not available on Windows: it needs TNotifier.

Constructor & Destructor Documentation

◆ TThreadedNode()

template<typename TPoller, typename TLock = TNullLock>
NNet::NActors::TThreadedNode< TPoller, TLock >::TThreadedNode ( TPoller & poller,
TMessagesFactory & factory,
TResolver & resolver,
const std::function< TSocket(const TAddress &)> & socketFactory,
const THostPort & hostPort )
inline

Constructs the node and its I/O coroutine; does not connect.

Parameters
pollerPoller of the I/O thread.
factoryFactory for serializing messages to their Far (wire) form; used on the producer side.
resolverDNS resolver on poller.
socketFactoryCallable that creates a socket on poller given a resolved TAddress.
hostPortRemote host and port to connect to.

◆ ~TThreadedNode()

template<typename TPoller, typename TLock = TNullLock>
NNet::NActors::TThreadedNode< TPoller, TLock >::~TThreadedNode ( )
inline

Destroys the coroutine first: a pending write reads Writing_ and a pending connect uses Socket_, see ~TNode.

Member Function Documentation

◆ Acquire()

template<typename TPoller, typename TLock = TNullLock>
std::span< char > NNet::NActors::TThreadedNode< TPoller, TLock >::Acquire ( size_t size)
inlineoverridevirtual

Reserves size contiguous bytes after the committed ones and returns a span over them. A reservation that is not committed (its writer threw) is reused by the next Acquire.

Implements NNet::NActors::IOutputStream.

◆ Commit()

template<typename TPoller, typename TLock = TNullLock>
void NNet::NActors::TThreadedNode< TPoller, TLock >::Commit ( size_t size)
inlineoverridevirtual

Marks size previously acquired bytes as ready to send.

Implements NNet::NActors::IOutputStream.

◆ Drain()

template<typename TPoller, typename TLock = TNullLock>
void NNet::NActors::TThreadedNode< TPoller, TLock >::Drain ( )
inlineoverridevirtual

Calls StartConnect() then asynchronously writes all buffered bytes to the socket.

Implements NNet::NActors::INode.

◆ GetHostPort()

template<typename TPoller, typename TLock = TNullLock>
THostPort NNet::NActors::TThreadedNode< TPoller, TLock >::GetHostPort ( ) const
inlineoverridevirtual

Returns the remote host:port this node connects to.

Implements NNet::NActors::INode.

◆ Offset()

template<typename TPoller, typename TLock = TNullLock>
uint64_t NNet::NActors::TThreadedNode< TPoller, TLock >::Offset ( ) const
inlineoverridevirtual

Stream offset of the next Acquire, or a value congruent to it mod MaxPayloadAlignment: payloads with PayloadAlignment are padded relative to it, and only their writers call it. The default suits a stream that keeps each frame in its own buffer.

Reimplemented from NNet::NActors::IOutputStream.

◆ Segments()

template<typename TPoller, typename TLock = TNullLock>
size_t NNet::NActors::TThreadedNode< TPoller, TLock >::Segments ( ) const
inline

Segments alive now: the current one, the queued ones, the one being written and the spare ones. An idle node has at most 1 + MaxSpareSegments. Any thread.

◆ Send()

template<typename TPoller, typename TLock = TNullLock>
void NNet::NActors::TThreadedNode< TPoller, TLock >::Send ( TEnvelope && envelope)
inlineoverridevirtual

Serializes envelope and appends it to the outbound buffer.

Implements NNet::NActors::INode.

◆ StartConnect()

template<typename TPoller, typename TLock = TNullLock>
void NNet::NActors::TThreadedNode< TPoller, TLock >::StartConnect ( )
inlineoverridevirtual

Initiates an async TCP connection if not already connected or connecting.

Implements NNet::NActors::INode.

Member Data Documentation

◆ MaxSpareSegments

template<typename TPoller, typename TLock = TNullLock>
size_t NNet::NActors::TThreadedNode< TPoller, TLock >::MaxSpareSegments = 4
staticconstexpr

Written segments kept for reuse. One goes around while the I/O side keeps up; the others let a short backlog pass without allocating.


The documentation for this class was generated from the following file: