shep-client 0.15.2

Async client for the shep daemon: typed requests, event subscriptions, and starting a shepherd that is not running yet
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
//! The connected [`Client`] handle: [`Client::connect`], [`Client::request`],
//! [`RequestError`].
//!
//! A `Client` is a thin, actor-backed handle: the socket itself is owned by
//! the task [`crate::actor::spawn`] starts, and every method here sends a
//! command to that task and awaits the answer. `&self` is enough for every
//! method, so concurrent callers share one `Client` (behind an `Arc`, or
//! just a shared reference) instead of cloning a handle per caller.

use core::fmt;
use std::path::{Path, PathBuf};
use std::time::Duration;

use tokio::sync::{mpsc, oneshot};

use shep_core::protocol::{HelloAck, Request, Response, RpcError, WireError};

use crate::actor::{self, Command};
use crate::connection::{ConnectError, Connection, HANDSHAKE_TIMEOUT};
use crate::events::EventStream;

/// Daemon-side budget applied when a caller names none. Mirrors the daemon's
/// own `DEFAULT_DEADLINE_MS = 5_000` (`shep-daemon/src/rpc.rs`).
pub const DEFAULT_DEADLINE: Duration = Duration::from_secs(5);

/// Budget for `Request::Start`. A cold spawn plus a readiness probe routinely
/// outruns the 5s default, and the daemon clamps anything over
/// `MAX_DEADLINE_MS = 60_000` (`shep-daemon/src/rpc.rs:38`), so this is well
/// inside what the daemon will honour.
pub const START_DEADLINE: Duration = Duration::from_secs(30);

/// Budget for `Request::Reload`.
///
/// A reload matching several sheep is walked in dependency order, and the
/// daemon holds every stage for the drains and readiness waits of the apps a
/// later stage needs, so two stages at the default timeouts already clear
/// [`START_DEADLINE`]. A client that gave up there would drop the walk with
/// its later stages never issued and hand the operator a timeout over a
/// half-reloaded fold. This asks for the daemon's whole `MAX_DEADLINE_MS`
/// (60s), which is the most it will honour, so the client is no longer the
/// one that gives up first.
///
/// It does not make the budget big enough, and no client-side value can. A
/// reload stage is bounded by `max(listen_timeout + graceful_timeout) *
/// swaps + STAGE_SLACK`, 16s per single-instance stage at the defaults: four
/// such stages is 64s, and two stages of a three-instance app is 76s. Both
/// are past the daemon's own 60s clamp, where the shepherd drops the walk
/// exactly as the client used to.
pub const RELOAD_DEADLINE: Duration = Duration::from_secs(60);

/// Budget for the log-plane verbs that walk the flock file by file:
/// `Request::Reopen` and `Request::Flush`.
///
/// The daemon visits matched sheep one after another with no per-sheep
/// bound, so a wedged or NFS-backed log directory can make the whole walk
/// take as long as the kernel does. Same 30s as [`START_DEADLINE`],
/// comfortably inside the daemon's own clamp.
pub const LOG_PLANE_DEADLINE: Duration = Duration::from_secs(30);

/// Budget for `Request::Trigger`.
///
/// An app's own `AppConfig::action_timeout` can reach `MAX_ACTION_TIMEOUT`
/// (58s), 2s under the daemon's own 60s clamp, so this asks for the full
/// 60s rather than abandon a reply the daemon is still building.
pub const TRIGGER_DEADLINE: Duration = Duration::from_secs(60);

/// Budget for `Request::Stop` and `Request::Delete`.
///
/// A stop runs the kill ladder, then the lamb sweep, each bounded by a
/// sheep's `kill_timeout`, and a selector can match many sheep. The client
/// cannot size that: asking the daemon for each `kill_timeout` costs a round
/// trip and still races the actor, so this asks for the daemon's whole
/// `MAX_DEADLINE_MS` (60s) instead. A ladder plus sweep past about 58s still
/// meets the daemon's clamp.
pub const STOP_DEADLINE: Duration = Duration::from_secs(60);

/// How much longer the client waits than the deadline it asked the daemon
/// to honour, so it doesn't report a timeout for work that succeeded.
pub const DEADLINE_GRACE: Duration = Duration::from_secs(2);

/// Why a [`Client::request`] (or [`Client::request_with_deadline`]) call failed.
///
/// Non-exhaustive: expect more variants.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum RequestError {
    /// The daemon accepted the request and answered it with a structured error.
    Rpc(RpcError),
    /// No reply arrived within the request's own deadline plus [`DEADLINE_GRACE`].
    Timeout {
        /// The client-side budget that was exceeded.
        after: Duration,
    },
    /// The connection closed (daemon exit, crash, or a prior [`Client::close`])
    /// before this request's reply arrived.
    Closed,
    /// `body` failed to encode onto the wire.
    Wire(WireError),
    /// The daemon's reply arrived but this build could not decode it.
    ///
    /// Distinct from [`Self::Rpc`]: the daemon did not report a structured
    /// error, it answered with something (a `Reply` or an unrecognized
    /// `ServerFrame` variant) that does not match the shapes this build
    /// knows. The likely cause is a daemon newer than this client, not a
    /// daemon-side failure, so the message says which side could not read.
    Undecodable(WireError),
    /// The daemon answered with a [`Response`] other than the one a typed
    /// request expects, such as a `Flock` for a `DogConfig`.
    ///
    /// Names both by variant alone: a listing or a dog's section never
    /// reaches the message.
    UnexpectedReply {
        /// The request sent, by its `Request` variant name.
        asked: &'static str,
        /// The answer received, by [`Response::name`].
        answered: &'static str,
    },
}

impl RequestError {
    /// The [`shep_core::exit`] code a process stopping on this error uses.
    ///
    /// A refusal takes its own code's number, and a closed connection is
    /// nothing answering. A reply this build cannot decode is a plain
    /// failure, since neither side is known to be at fault.
    #[must_use]
    pub const fn exit_code(&self) -> u8 {
        use shep_core::exit;
        match self {
            Self::Rpc(rpc) => rpc.code.exit_code(),
            Self::Timeout { .. } => exit::DEADLINE_EXCEEDED,
            Self::Closed => exit::DAEMON_UNREACHABLE,
            Self::Wire(_) | Self::UnexpectedReply { .. } => exit::INTERNAL,
            Self::Undecodable(_) => exit::FAILURE,
        }
    }
}

impl fmt::Display for RequestError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Rpc(err) => write!(f, "the daemon reported {:?}: {}", err.code, err.message),
            Self::Timeout { after } => write!(f, "no reply within {after:?}"),
            Self::Closed => f.write_str("the connection closed before a reply arrived"),
            Self::Wire(err) => write!(f, "request frame error: {err}"),
            Self::Undecodable(err) => write!(
                f,
                "this client could not decode the daemon's reply ({err}); the daemon is likely newer than this build"
            ),
            Self::UnexpectedReply { asked, answered } => {
                write!(f, "the daemon answered {asked} with {answered}")
            }
        }
    }
}

impl core::error::Error for RequestError {
    fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
        match self {
            Self::Wire(err) | Self::Undecodable(err) => Some(err),
            Self::Rpc(_) | Self::Timeout { .. } | Self::Closed | Self::UnexpectedReply { .. } => {
                None
            }
        }
    }
}

/// A live connection to the daemon.
///
/// Backed by one actor task (see the crate's `actor` module) that owns the
/// socket; `request`/`request_with_deadline`/`close` all take `&self`, so
/// callers share one `Client` behind an `Arc` or a reference rather than
/// cloning a handle per caller.
pub struct Client {
    commands: mpsc::Sender<Command>,
    ack: HelloAck,
    socket: PathBuf,
    /// The name this client announces itself as a dog under, re-sent on
    /// every [`Client::reconnect`]. See [`Self::connect_as`].
    dog_name: Option<String>,
}

// Manual, not derived: a path, an ack and a dog's name carry no secret, and
// none of the three is what a derived impl would lead with. The command
// channel says nothing a reader can act on.
impl fmt::Debug for Client {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Client")
            .field("socket", &self.socket)
            .field("ack", &self.ack)
            .field("dog_name", &self.dog_name)
            .finish_non_exhaustive()
    }
}

impl Client {
    /// Connects to `socket` and performs the version handshake, bounded by
    /// [`HANDSHAKE_TIMEOUT`].
    ///
    /// # Errors
    ///
    /// See [`Self::connect_with_timeout`].
    pub async fn connect(socket: &Path) -> Result<Self, ConnectError> {
        Self::connect_with_timeout(socket, HANDSHAKE_TIMEOUT).await
    }

    /// As [`Self::connect`], but with a caller-supplied handshake timeout.
    ///
    /// # Errors
    ///
    /// - [`ConnectError::Connect`]: the initial `connect(2)` call failed.
    /// - [`ConnectError::Wire`]: `Hello` failed to encode, or the reply failed to decode.
    /// - [`ConnectError::Io`]: a framed read or write failed after connect.
    /// - [`ConnectError::HandshakeClosed`]: the peer closed before a `HelloReply`.
    /// - [`ConnectError::HandshakeTimeout`]: no `HelloReply` arrived within `timeout`.
    /// - [`ConnectError::ProtocolMismatch`]: the daemon refused on protocol-version skew.
    pub async fn connect_with_timeout(
        socket: &Path,
        timeout: Duration,
    ) -> Result<Self, ConnectError> {
        Self::connect_as(socket, timeout, None).await
    }

    /// As [`Self::connect_with_timeout`], but announcing this client as the
    /// dog registered under `dog_name`.
    ///
    /// Crate-private: the public constructors above pass `None`, and only
    /// [`ReconnectingClient`](crate::ReconnectingClient) passes a name. A
    /// client that could claim an arbitrary dog name could get that dog
    /// restarted on its own say-so.
    ///
    /// # Errors
    ///
    /// See [`Self::connect_with_timeout`].
    pub(crate) async fn connect_as(
        socket: &Path,
        timeout: Duration,
        dog_name: Option<&str>,
    ) -> Result<Self, ConnectError> {
        let connection = Connection::open(socket, timeout, dog_name).await?;
        let (frames, ack) = connection.into_parts();
        let commands = actor::spawn(frames);
        Ok(Self {
            commands,
            ack,
            socket: socket.to_path_buf(),
            dog_name: dog_name.map(str::to_owned),
        })
    }

    /// The name this client announces itself as a dog under, or `None` for
    /// a caller that is not one.
    ///
    /// Crate-private for the reason [`Self::connect_as`] is: only this
    /// crate may set a name, so only this crate may report one.
    pub(crate) fn dog_name(&self) -> Option<&str> {
        self.dog_name.as_deref()
    }

    /// Takes over `fresh`'s connection, dropping this handle's own.
    ///
    /// Dropping the old command channel signals its actor rather than
    /// stopping it where it stands. An actor already awaiting a write
    /// finishes that write, sees the closed channel when it next reaches
    /// its select, and only then drops the old socket.
    pub(crate) fn replace_connection(&mut self, fresh: Self) {
        *self = fresh;
    }

    /// The daemon's handshake acknowledgement.
    #[must_use]
    pub fn daemon(&self) -> &HelloAck {
        &self.ack
    }

    /// Resolves once this connection has ended: daemon exit, crash,
    /// `execve`, a write failure, or a prior [`Self::close`]. Resolves
    /// immediately if already gone, and may be awaited more than once.
    ///
    /// This does not reconnect, and [`Self::request`] does not retry. See
    /// [`ReconnectingClient`](crate::ReconnectingClient) for the supervised
    /// wrapper.
    pub async fn closed(&self) {
        self.commands.closed().await;
    }

    /// The path this client is connected through.
    ///
    /// `HelloAck` doesn't carry the socket path, so the `Client` keeps the
    /// `PathBuf` it connected with, for a caller that needs it after
    /// teardown.
    #[must_use]
    pub fn socket(&self) -> &Path {
        &self.socket
    }

    /// Sends `body` with [`DEFAULT_DEADLINE`].
    ///
    /// Shorthand for [`Self::request_with_deadline`]`(body, None)`.
    ///
    /// # Errors
    ///
    /// See [`Self::request_with_deadline`].
    pub async fn request(&self, body: Request) -> Result<Response, RequestError> {
        self.request_with_deadline(body, None).await
    }

    /// Sends `body` with `deadline`, or [`DEFAULT_DEADLINE`] if `None`. The
    /// client waits `deadline` plus [`DEADLINE_GRACE`] for a reply before
    /// giving up locally.
    ///
    /// # Errors
    ///
    /// - [`RequestError::Rpc`]: the daemon answered with a structured error.
    /// - [`RequestError::Timeout`]: no reply within `deadline + DEADLINE_GRACE`.
    /// - [`RequestError::Closed`]: the connection closed before a reply arrived.
    /// - [`RequestError::Wire`]: `body` failed to encode.
    pub async fn request_with_deadline(
        &self,
        body: Request,
        deadline: Option<Duration>,
    ) -> Result<Response, RequestError> {
        let deadline = deadline.unwrap_or(DEFAULT_DEADLINE);
        let (reply_to, reply_rx) = oneshot::channel();
        self.commands
            .send(Command::Request {
                body,
                deadline_ms: Some(millis(deadline)),
                reply_to,
            })
            .await
            .map_err(|_send_error| RequestError::Closed)?;

        // Saturating: `deadline` is the caller's, and `Duration`'s `Add`
        // panics rather than saturating on overflow.
        let budget = deadline.saturating_add(DEADLINE_GRACE);
        match tokio::time::timeout(budget, reply_rx).await {
            Ok(Ok(outcome)) => outcome,
            Ok(Err(_recv_error)) => Err(RequestError::Closed),
            Err(_elapsed) => Err(RequestError::Timeout { after: budget }),
        }
    }

    /// Subscribes this connection to `topics`: dotted glob patterns matched
    /// against [`shep_core::protocol::BusEvent::topic`] (`process.*`,
    /// `log.*`, `daemon.*`, ...).
    ///
    /// A second call on the same `Client` replaces the daemon-side filter
    /// rather than adding to it; a caller wanting two topic sets needs two
    /// `Client`s. The returned [`EventStream`]'s receiver is installed
    /// before the `Subscribe` request is sent, so no pushed event is missed.
    ///
    /// # Errors
    ///
    /// Same as [`Self::request`].
    pub async fn subscribe(&self, topics: Vec<String>) -> Result<EventStream, RequestError> {
        let (reply_to, reply_rx) = oneshot::channel();
        self.commands
            .send(Command::Subscribe { reply_to })
            .await
            .map_err(|_send_error| RequestError::Closed)?;
        let receiver = reply_rx.await.map_err(|_recv_error| RequestError::Closed)?;

        // reply is `Response::Subscribed`; unchecked, since interpreting
        // `Response` variants is shep-cli's job, not this crate's
        self.request(Request::Subscribe { topics }).await?;
        Ok(EventStream::new(receiver))
    }

    /// Closes the connection.
    ///
    /// Drops the command channel to the actor task, which ends the actor's
    /// loop and drops the underlying socket.
    ///
    /// # Errors
    ///
    /// Never fails today. `Result` leaves room for a later, more graceful
    /// teardown (draining in-flight requests before dropping, say) to start
    /// returning one without an API break.
    pub async fn close(self) -> Result<(), RequestError> {
        drop(self.commands);
        Ok(())
    }
}

/// Saturating `Duration` to wire milliseconds. A caller-supplied `Duration`
/// above the wire range saturates at `u64::MAX` ms rather than overflowing.
fn millis(d: Duration) -> u64 {
    u64::try_from(d.as_millis()).unwrap_or(u64::MAX)
}

#[cfg(test)]
mod tests {
    use shep_core::exit;
    use shep_core::protocol::RpcErrorCode;

    use super::*;

    #[test]
    fn a_request_error_exits_on_its_causes_code() {
        let unsupported = RequestError::Rpc(RpcError {
            code: RpcErrorCode::Unsupported,
            message: "no such verb".to_owned(),
            daemon_version: None,
        });
        let cases = [
            (unsupported, exit::UNSUPPORTED),
            (RequestError::Closed, exit::DAEMON_UNREACHABLE),
            (
                RequestError::Timeout {
                    after: Duration::from_secs(7),
                },
                exit::DEADLINE_EXCEEDED,
            ),
            (
                RequestError::UnexpectedReply {
                    asked: "ListFlock",
                    answered: "Pong",
                },
                exit::INTERNAL,
            ),
        ];
        for (err, code) in cases {
            assert_eq!(err.exit_code(), code, "{err}");
        }
    }
}