liminal_sdk/remote/tcp/push_client/pending_connect.rs
1use alloc::vec::Vec;
2use core::time::Duration;
3
4use liminal::protocol::WorkerRegistration;
5
6use super::PushClient;
7use crate::SdkError;
8
9/// A pending push-client connection with a caller-selected setup deadline.
10///
11/// The duration has two setup-only roles: it is the maximum wait for any one
12/// socket read, and it supplies the wall-clock deadline for each synchronous
13/// control-frame reply. A registration connect performs two control exchanges
14/// (`Connect`/`ConnectAck` and `WorkerRegister`/`WorkerRegisterAck`), each with
15/// its own deadline. The duration does not bound the TCP open or socket writes,
16/// and it is removed before the background reader starts.
17#[non_exhaustive]
18pub struct PendingPushConnect<'address> {
19 address: &'address str,
20 setup_deadline: Duration,
21 auth_token: Vec<u8>,
22 registration: Option<WorkerRegistration>,
23}
24
25impl<'address> PendingPushConnect<'address> {
26 pub(super) const fn new(address: &'address str, setup_deadline: Duration) -> Self {
27 Self {
28 address,
29 setup_deadline,
30 auth_token: Vec::new(),
31 registration: None,
32 }
33 }
34
35 /// Adds the authentication token carried by the connect handshake.
36 #[must_use]
37 pub fn with_auth_token(mut self, auth_token: &[u8]) -> Self {
38 self.auth_token = auth_token.to_vec();
39 self
40 }
41
42 /// Adds the worker registration exchange that runs after the connect
43 /// handshake and before the background reader starts.
44 #[must_use]
45 pub fn with_registration(mut self, registration: WorkerRegistration) -> Self {
46 self.registration = Some(registration);
47 self
48 }
49
50 /// Connects, performs the configured synchronous control exchanges, and
51 /// starts the background reader.
52 ///
53 /// # Errors
54 ///
55 /// Returns [`SdkError::Connection`] when the TCP connection or socket
56 /// configuration fails or authentication is rejected, and
57 /// [`SdkError::Protocol`] when another control exchange is rejected or the
58 /// socket cannot be cloned for the reader thread.
59 pub fn connect(self) -> Result<PushClient, SdkError> {
60 let Self {
61 address,
62 setup_deadline,
63 auth_token,
64 registration,
65 } = self;
66 PushClient::connect_configured(address, &auth_token, registration, setup_deadline)
67 }
68}