19#ifndef A11_SERVICE_SESSION_H_
20#define A11_SERVICE_SESSION_H_
31#include <absl/status/status.h>
32#include <absl/status/statusor.h>
33#include <absl/time/time.h>
90 std::optional<data::WireMessage>, std::shared_ptr<net::WireStream>,
91 std::shared_ptr<Session>)>;
94 std::shared_ptr<net::WireStream>, std::shared_ptr<Session>)>;
112class Session :
public std::enable_shared_from_this<Session> {
129 static absl::StatusOr<std::shared_ptr<Session>>
Create(
134 std::shared_ptr<nodes::NodeMap>
node_map =
nullptr,
145 std::vector<std::pair<std::string, std::shared_ptr<net::WireStream>>>>
154 absl::StatusOr<std::shared_ptr<net::WireStream>>
GetStream(
155 std::string_view stream_id)
const;
200 std::shared_ptr<actions::ActionRegistry>
registry);
208 std::pair<std::string, std::shared_ptr<actions::Action>>>
216 absl::StatusOr<std::shared_ptr<actions::Action>>
GetAction(
310 absl::StatusOr<a11::Task>
AddStream(std::shared_ptr<net::WireStream> stream,
325 virtual absl::Status
Abort(absl::Status status);
361 std::shared_ptr<State> state_;
365 std::optional<data::WireMessage> message);
366 void ProcessStreamMessages(
const std::shared_ptr<StreamState>&
stream_state);
368 void RemoveStream(
const std::shared_ptr<StreamState>&
stream_state);
369 void FinishIfPossible();
370 void NotifyStateChanged();
372 absl::Status TrackAction(
const std::shared_ptr<actions::Action>&
action);
373 void UntrackAction(
const std::shared_ptr<actions::Action>&
action);
374 std::shared_ptr<actions::ActionLimiter> GetActionLimiter(
bool nested)
const;
407 static absl::StatusOr<std::shared_ptr<SessionWithRecv>>
Create(
410 std::shared_ptr<nodes::NodeMap>
node_map =
nullptr,
420 absl::Time
deadline = absl::InfiniteFuture());
429 absl::Time
deadline = absl::InfiniteFuture());
436 absl::Status
Abort(absl::Status status)
override;
440 std::shared_ptr<ReceiveState> receive_state_;
442 a11::Task OnMessage(std::optional<data::WireMessage> message,
443 std::shared_ptr<net::WireStream> stream);
444 a11::Task OnDone(std::shared_ptr<net::WireStream> stream);
445 void SignalReceiveError(absl::Status status);
A11's unit of work: the Action and its supporting types.
A11's unit of work: a schema-described, asynchronously run operation.
Definition action.h:112
A Session variant that buffers inbound messages for pull-style reception.
Definition session.h:394
a11::Future< std::optional< data::WireMessage > > Receive(absl::Time deadline=absl::InfiniteFuture())
Await the next inbound wire message.
Definition session.cc:1682
static absl::StatusOr< std::shared_ptr< SessionWithRecv > > Create(std::string session_id={}, data::ByteMap headers={}, SessionOptions options={}, std::shared_ptr< nodes::NodeMap > node_map=nullptr, std::shared_ptr< actions::ActionRegistry > action_registry=nullptr)
Create a pull-style session.
Definition session.cc:1508
absl::Status Abort(absl::Status status) override
Abort the session, also failing any pending receivers.
Definition session.cc:1722
a11::Future< std::optional< ReceivedSessionMessage > > ReceiveWithStreamId(absl::Time deadline=absl::InfiniteFuture())
Await the next inbound message together with its stream id.
Definition session.cc:1631
A connection-scoped runtime that multiplexes wire streams and runs actions.
Definition session.h:112
static absl::StatusOr< std::shared_ptr< Session > > Create(std::string session_id={}, OnSessionStreamMessage on_stream_message={}, OnSessionStreamDone on_stream_done={}, data::ByteMap headers={}, SessionOptions options={}, std::shared_ptr< nodes::NodeMap > node_map=nullptr, std::shared_ptr< actions::ActionRegistry > action_registry=nullptr)
Create a session.
Definition session.cc:231
virtual absl::Status Abort(absl::Status status)
Abort the session immediately, cancelling streams and actions.
Definition session.cc:1379
absl::Status HalfClose()
Signal that this side will send no more messages.
Definition session.cc:1341
bool IsDone() const
Report whether the session has fully finished.
Definition session.cc:893
absl::StatusOr< std::vector< std::pair< std::string, std::shared_ptr< net::WireStream > > > > Streams() const
Return the streams currently attached to the session.
Definition session.cc:382
std::shared_ptr< nodes::NodeMap > GetNodeMap() const
Return the NodeMap backing this session's node state.
Definition session.cc:411
a11::Task Done() const
Await the session's full completion.
Definition session.cc:898
absl::Status Initialize(const std::shared_ptr< Session > &self, std::string session_id, OnSessionStreamMessage on_stream_message, OnSessionStreamDone on_stream_done, data::ByteMap headers, SessionOptions options, std::shared_ptr< nodes::NodeMap > node_map, std::shared_ptr< actions::ActionRegistry > action_registry)
Definition session.cc:248
std::vector< std::pair< std::string, std::shared_ptr< actions::Action > > > Actions() const
Return the actions currently running in the session.
Definition session.cc:459
absl::Status GetStatus() const
Return the session's terminal status.
Definition session.cc:903
absl::Status Send(data::WireMessage message, std::string_view stream_id={})
Enqueue a wire message for delivery.
Definition session.cc:1440
absl::Status CancelAction(std::string_view action_id)
Request cancellation of a running action.
Definition session.cc:475
std::string GetId() const
Return the session's unique identifier.
Definition session.cc:406
a11::Task AwaitAllActions(absl::Duration timeout=absl::InfiniteDuration())
Wait for all in-flight actions to finish.
Definition session.cc:496
absl::StatusOr< a11::Task > AddStream(std::shared_ptr< net::WireStream > stream, StreamMode mode=StreamMode::kStart)
Attach a wire stream and begin pumping its messages.
Definition session.cc:917
a11::Task DispatchWireMessage(data::WireMessage message, std::shared_ptr< net::WireStream > origin_stream=nullptr)
Route a wire message through the session as if it arrived on a stream.
Definition session.cc:813
absl::Time deadline() const
Return the absolute deadline after which the session is aborted.
Definition session.cc:1478
bool IsClosed() const
Report whether the session has been closed.
Definition session.cc:888
std::shared_ptr< actions::ActionRegistry > GetActionRegistry() const
Return the registry used to resolve incoming action messages.
Definition session.cc:435
virtual ~Session()=default
a11::Task DispatchAction(std::shared_ptr< actions::Action > action)
Run an already-constructed action within the session.
Definition session.cc:793
absl::Status SetDeadline(absl::Time deadline=absl::InfiniteFuture())
Set the absolute deadline after which the session is aborted.
Definition session.cc:1483
absl::Status SetActionRegistry(std::shared_ptr< actions::ActionRegistry > registry)
Replace the registry used to resolve incoming action messages.
Definition session.cc:440
absl::Status CancelAllActions()
Request cancellation of every running action.
Definition session.cc:481
a11::Future< std::uint32_t > DispatchNodeFragment(data::NodeFragment fragment)
Dispatch a node fragment into the session's NodeMap.
Definition session.cc:591
absl::Status SetNodeMap(std::shared_ptr< nodes::NodeMap > node_map)
Replace the NodeMap backing this session's node state.
Definition session.cc:416
absl::StatusOr< std::shared_ptr< actions::Action > > GetAction(std::string_view action_id) const
Look up a running action by id.
Definition session.cc:464
absl::StatusOr< std::shared_ptr< net::WireStream > > GetStream(std::string_view stream_id) const
Look up an attached stream by id.
Definition session.cc:395
a11::Task DispatchActionMessage(data::ActionMessage message, std::shared_ptr< net::WireStream > origin_stream=nullptr)
Resolve an action message against the registry and run it.
Definition session.cc:669
Completion values used by every asynchronous A11 operation.
absl::flat_hash_map< std::string, Bytes > ByteMap
String-keyed map of byte values (headers, attributes, etc.).
Definition types.h:42
absl::StatusOr< data::ByteMap > NormalizeSessionHeaders(data::ByteMap headers)
Validate and case-normalize session headers.
Definition session.cc:220
StreamMode
Whether this side starts (kStart) or accepts (kAccept) a stream during its startup handshake.
Definition session.h:98
constexpr size_t kMaxSingleMessageSize
Hard upper bound for any one WireMessage admitted by a Session.
Definition session.h:47
constexpr std::string_view kSessionStatusHeader
Trailer/header used to communicate the session's structured terminal status.
Definition session.h:45
std::function< a11::Task(std::shared_ptr< net::WireStream >, std::shared_ptr< Session >)> OnSessionStreamDone
Callback invoked once a session stream has finished; may be a coroutine.
Definition session.h:94
std::function< a11::Task(std::optional< data::WireMessage >, std::shared_ptr< net::WireStream >, std::shared_ptr< Session >)> OnSessionStreamMessage
Callback invoked for each message received on a session stream (nullopt signals end-of-stream); may b...
Definition session.h:91
Future< Unit > Task
Asynchronous operation whose only successful result is completion itself.
Definition future.h:403
Future< T > SubmitWithCancellationHook(absl::AnyInvocable< absl::StatusOr< T >() && > work, std::function< void()> cancellation_hook, thread::TreeOptions tree_options)
Run work on A11's fiber pool with application-specific cancellation.
Definition executor.h:30
A registry of AsyncNodes keyed by node id.
The action registry: a catalogue of named schema/handler pairs.
The wire description of an action invocation.
Definition types.h:239
One piece of a node's stream: an inline chunk or a node reference.
Definition types.h:167
The top-level frame exchanged between two A11 endpoints.
Definition types.h:275
An inbound wire message paired with the id of the stream it arrived on.
Definition session.h:380
std::string stream_id
Id of the transport that delivered it.
Definition session.h:382
data::WireMessage message
Received application message.
Definition session.h:381
Limits and timeouts governing a Session's buffering, concurrency, and lifetime.
Definition session.h:58
size_t max_buffered_messages_per_stream
Maximum number of messages buffered per stream.
Definition session.h:62
size_t max_concurrent_nested_actions
Maximum number of concurrently running nested (child) actions.
Definition session.h:66
size_t max_concurrent_root_actions
Maximum number of concurrently running root (top-level) actions.
Definition session.h:64
absl::Time deadline
Absolute time after which the session is aborted.
Definition session.h:76
absl::Duration no_stream_timeout
How long the session waits with no active stream before finishing.
Definition session.h:74
size_t max_buffered_messages_total
Maximum number of messages buffered across all streams.
Definition session.h:60
size_t max_buffered_bytes_total
Maximum total bytes buffered across all streams.
Definition session.h:70
size_t max_buffered_bytes_per_stream
Maximum bytes buffered per stream.
Definition session.h:72
size_t max_single_message_size
Maximum size in bytes of a single wire message.
Definition session.h:68
absl::Status Validate() const
Validate the option values.
Definition session.cc:192
Definition session.cc:1496
Definition session.cc:129
Definition session.cc:104
A11's core wire value types: chunks, node fragments and messages.
A11's transport abstraction: the bidirectional WireStream channel and the options/callbacks that driv...