A11 (C++ runtime)
Native C++ implementation of the A11 action and streaming runtime
Loading...
Searching...
No Matches
session.h
Go to the documentation of this file.
1// Copyright 2026 The A11 Authors.
2
19#ifndef A11_SERVICE_SESSION_H_
20#define A11_SERVICE_SESSION_H_
21
22#include <cstddef>
23#include <functional>
24#include <memory>
25#include <optional>
26#include <string>
27#include <string_view>
28#include <utility>
29#include <vector>
30
31#include <absl/status/status.h>
32#include <absl/status/statusor.h>
33#include <absl/time/time.h>
34
35#include "a11/actions/action.h"
38#include "a11/data/types.h"
39#include "a11/net/wire_stream.h"
40#include "a11/nodes/node_map.h"
41
42namespace a11::service {
43
45inline constexpr std::string_view kSessionStatusHeader = "x-a11-session-status";
47inline constexpr size_t kMaxSingleMessageSize = 32 * 1024 * 1024;
48
70 size_t max_buffered_bytes_total = 32 * 1024 * 1024;
72 size_t max_buffered_bytes_per_stream = 4 * 1024 * 1024;
74 absl::Duration no_stream_timeout = absl::Seconds(30);
76 absl::Time deadline = absl::InfiniteFuture();
77
82 absl::Status Validate() const;
83};
84
85class Session;
86
89using OnSessionStreamMessage = std::function<a11::Task(
90 std::optional<data::WireMessage>, std::shared_ptr<net::WireStream>,
91 std::shared_ptr<Session>)>;
93using OnSessionStreamDone = std::function<a11::Task(
94 std::shared_ptr<net::WireStream>, std::shared_ptr<Session>)>;
95
98enum class StreamMode { kStart, kAccept };
99
112class Session : public std::enable_shared_from_this<Session> {
113 public:
129 static absl::StatusOr<std::shared_ptr<Session>> Create(
130 std::string session_id = {},
133 SessionOptions options = {},
134 std::shared_ptr<nodes::NodeMap> node_map = nullptr,
135 std::shared_ptr<actions::ActionRegistry> action_registry = nullptr);
136
137 virtual ~Session() = default;
138
144 absl::StatusOr<
145 std::vector<std::pair<std::string, std::shared_ptr<net::WireStream>>>>
146 Streams() const;
147
154 absl::StatusOr<std::shared_ptr<net::WireStream>> GetStream(
155 std::string_view stream_id) const;
156
162 [[nodiscard]] std::string GetId() const;
163
168 [[nodiscard]] std::shared_ptr<nodes::NodeMap> GetNodeMap() const;
169
180 absl::Status SetNodeMap(std::shared_ptr<nodes::NodeMap> node_map);
181
186 [[nodiscard]] std::shared_ptr<actions::ActionRegistry> GetActionRegistry()
187 const;
188
199 absl::Status SetActionRegistry(
200 std::shared_ptr<actions::ActionRegistry> registry);
201
207 [[nodiscard]] std::vector<
208 std::pair<std::string, std::shared_ptr<actions::Action>>>
209 Actions() const;
210
216 absl::StatusOr<std::shared_ptr<actions::Action>> GetAction(
217 std::string_view action_id) const;
218
225 absl::Status CancelAction(std::string_view action_id);
226
232 absl::Status CancelAllActions();
233
240 a11::Task AwaitAllActions(absl::Duration timeout = absl::InfiniteDuration());
241
248
256 data::ActionMessage message,
257 std::shared_ptr<net::WireStream> origin_stream = nullptr);
258
264 a11::Task DispatchAction(std::shared_ptr<actions::Action> action);
265
274 data::WireMessage message,
275 std::shared_ptr<net::WireStream> origin_stream = nullptr);
276
281 [[nodiscard]] bool IsClosed() const;
282
288 [[nodiscard]] bool IsDone() const;
289
295 [[nodiscard]] a11::Task Done() const;
296
301 [[nodiscard]] absl::Status GetStatus() const;
302
310 absl::StatusOr<a11::Task> AddStream(std::shared_ptr<net::WireStream> stream,
312
318 absl::Status HalfClose();
319
325 virtual absl::Status Abort(absl::Status status);
326
334 absl::Status Send(data::WireMessage message, std::string_view stream_id = {});
335
340 [[nodiscard]] absl::Time deadline() const;
341
347 absl::Status SetDeadline(absl::Time deadline = absl::InfiniteFuture());
348
349 protected:
350 Session() = default;
351 absl::Status Initialize(
352 const std::shared_ptr<Session>& self, std::string session_id,
355 SessionOptions options, std::shared_ptr<nodes::NodeMap> node_map,
356 std::shared_ptr<actions::ActionRegistry> action_registry);
357
358 private:
359 struct State;
360 struct StreamState;
361 std::shared_ptr<State> state_;
362
363 a11::Task HandleStreamMessage(
364 const std::shared_ptr<StreamState>& stream_state,
365 std::optional<data::WireMessage> message);
366 void ProcessStreamMessages(const std::shared_ptr<StreamState>& stream_state);
367 a11::Task HandleStreamDone(const std::shared_ptr<StreamState>& stream_state);
368 void RemoveStream(const std::shared_ptr<StreamState>& stream_state);
369 void FinishIfPossible();
370 void NotifyStateChanged();
371
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;
375
376 friend class actions::Action;
377};
378
384
395 public:
407 static absl::StatusOr<std::shared_ptr<SessionWithRecv>> Create(
408 std::string session_id = {}, data::ByteMap headers = {},
409 SessionOptions options = {},
410 std::shared_ptr<nodes::NodeMap> node_map = nullptr,
411 std::shared_ptr<actions::ActionRegistry> action_registry = nullptr);
412
420 absl::Time deadline = absl::InfiniteFuture());
421
429 absl::Time deadline = absl::InfiniteFuture());
430
436 absl::Status Abort(absl::Status status) override;
437
438 private:
439 struct ReceiveState;
440 std::shared_ptr<ReceiveState> receive_state_;
441
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);
446};
447
453absl::StatusOr<data::ByteMap> NormalizeSessionHeaders(data::ByteMap headers);
454
455} // namespace a11::service
456
457#endif // A11_SERVICE_SESSION_H_
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
Definition action.h:55
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: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...