trillium-grpc 0.1.0

gRPC server and client for trillium, built on trillium-http's h2/h2c/h3 support
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
use crate::{
    Code, Codec, Encoding, Status,
    client::{ResponseStream, response_stream::race_against_deadline},
    frame::{reader::MessageStream, writer::encode_frame},
    server::content_type::parse_grpc_content_type,
    timeout::parse_grpc_timeout,
};
use futures_lite::{AsyncWriteExt, Stream, StreamExt};
use std::time::Instant;
use trillium::{KnownHeaderName, Transport};
use trillium_client::{Conn, ConnExt, Version};
use trillium_http::{Status as HttpStatus, Upgrade as HttpUpgrade};

type Upgrade = HttpUpgrade<Box<dyn Transport>>;

/// Client-side dispatch methods, available on any codec type via a blanket
/// impl. Generated code calls these as `Prost::unary_call(client, path, req)`
/// etc., which resolves through the trait without requiring a turbofish.
///
/// The `_call` suffix distinguishes these from the server-side `unary` /
/// `server_streaming` / etc. methods on the [`Server`](crate::Server) trait,
/// since both traits are blanket-implemented on every codec type.
#[allow(async_fn_in_trait)]
pub trait Client: Sized + 'static {
    /// Unary RPC: send one request, await one response.
    async fn unary_call<Req, Resp>(
        client: &trillium_client::Client,
        path: &str,
        req: Req,
    ) -> Result<Resp, Status>
    where
        Self: Codec<Req> + Codec<Resp>,
        Req: Send + 'static,
        Resp: Send + 'static,
    {
        let deadline = deadline_from_client(client);
        with_deadline(
            client,
            deadline,
            unary_call_impl::<Self, Req, Resp>(client, path, req),
        )
        .await
    }

    /// Server-streaming RPC: send one request, return a stream of responses.
    async fn server_streaming_call<Req, Resp>(
        client: &trillium_client::Client,
        path: &str,
        req: Req,
    ) -> Result<ResponseStream<Self, Resp>, Status>
    where
        Self: Codec<Req> + Codec<Resp>,
        Req: Send + 'static,
        Resp: Send + 'static,
    {
        let deadline = deadline_from_client(client);
        with_deadline(
            client,
            deadline,
            server_streaming_call_impl::<Self, Req, Resp>(client, path, req, deadline),
        )
        .await
    }

    /// Client-streaming RPC: send a stream of requests, await one response.
    async fn client_streaming_call<Req, Resp, S>(
        client: &trillium_client::Client,
        path: &str,
        requests: S,
    ) -> Result<Resp, Status>
    where
        Self: Codec<Req> + Codec<Resp>,
        Req: Send + 'static,
        Resp: Send + 'static,
        S: Stream<Item = Req> + Send + 'static,
    {
        let deadline = deadline_from_client(client);
        with_deadline(
            client,
            deadline,
            client_streaming_call_impl::<Self, Req, Resp, S>(client, path, requests),
        )
        .await
    }

    /// Bidirectional-streaming RPC: send a stream of requests, return a
    /// stream of responses.
    ///
    /// The request stream is drained in full before the response stream is
    /// read, so the two directions don't overlap on the client — fine for
    /// request-then-response exchanges, not for protocols that interleave.
    /// Open an issue if you need concurrent duplex.
    async fn bidi_call<Req, Resp, S>(
        client: &trillium_client::Client,
        path: &str,
        requests: S,
    ) -> Result<ResponseStream<Self, Resp>, Status>
    where
        Self: Codec<Req> + Codec<Resp>,
        Req: Send + 'static,
        Resp: Send + 'static,
        S: Stream<Item = Req> + Send + 'static,
    {
        let deadline = deadline_from_client(client);
        with_deadline(
            client,
            deadline,
            bidi_call_impl::<Self, Req, Resp, S>(client, path, requests, deadline),
        )
        .await
    }
}

impl<T: Sized + 'static> Client for T {}

async fn unary_call_impl<C, Req, Resp>(
    client: &trillium_client::Client,
    path: &str,
    req: Req,
) -> Result<Resp, Status>
where
    C: Codec<Req> + Codec<Resp>,
    Req: Send + 'static,
    Resp: Send + 'static,
{
    let outbound_encoding = outbound_encoding_from_client(client);
    let frame = encode_frame::<C, Req>(&req, outbound_encoding)?;
    let (mut upgrade, response_encoding) = open_upgrade::<C, Req>(client, path)
        .await?
        .into_streaming()?;

    write_frame(&mut upgrade, &frame).await?;
    close_outbound(&mut upgrade).await?;

    let response = read_one_response::<C, Resp>(&mut upgrade, response_encoding).await?;
    finish_with_trailers(&upgrade)?;
    Ok(response)
}

async fn server_streaming_call_impl<C, Req, Resp>(
    client: &trillium_client::Client,
    path: &str,
    req: Req,
    deadline: Option<Instant>,
) -> Result<ResponseStream<C, Resp>, Status>
where
    C: Codec<Req> + Codec<Resp>,
    Req: Send + 'static,
    Resp: Send + 'static,
{
    let outbound_encoding = outbound_encoding_from_client(client);
    let frame = encode_frame::<C, Req>(&req, outbound_encoding)?;
    let opened = open_upgrade::<C, Req>(client, path).await?;
    let response_stream = match opened {
        OpenUpgrade::Streaming(mut upgrade, response_encoding) => {
            write_frame(&mut upgrade, &frame).await?;
            close_outbound(&mut upgrade).await?;
            ResponseStream::spawn(client, upgrade, response_encoding, deadline)
        }
        OpenUpgrade::TrailersOnly(result) => ResponseStream::trailers_only(result),
    };
    Ok(response_stream)
}

async fn client_streaming_call_impl<C, Req, Resp, S>(
    client: &trillium_client::Client,
    path: &str,
    requests: S,
) -> Result<Resp, Status>
where
    C: Codec<Req> + Codec<Resp>,
    Req: Send + 'static,
    Resp: Send + 'static,
    S: Stream<Item = Req> + Send + 'static,
{
    let outbound_encoding = outbound_encoding_from_client(client);
    let (mut upgrade, response_encoding) = open_upgrade::<C, Req>(client, path)
        .await?
        .into_streaming()?;

    write_request_stream::<C, Req, S>(&mut upgrade, requests, outbound_encoding).await?;
    close_outbound(&mut upgrade).await?;

    let response = read_one_response::<C, Resp>(&mut upgrade, response_encoding).await?;
    finish_with_trailers(&upgrade)?;
    Ok(response)
}

async fn bidi_call_impl<C, Req, Resp, S>(
    client: &trillium_client::Client,
    path: &str,
    requests: S,
    deadline: Option<Instant>,
) -> Result<ResponseStream<C, Resp>, Status>
where
    C: Codec<Req> + Codec<Resp>,
    Req: Send + 'static,
    Resp: Send + 'static,
    S: Stream<Item = Req> + Send + 'static,
{
    let outbound_encoding = outbound_encoding_from_client(client);
    let opened = open_upgrade::<C, Req>(client, path).await?;
    let response_stream = match opened {
        OpenUpgrade::Streaming(mut upgrade, response_encoding) => {
            write_request_stream::<C, Req, S>(&mut upgrade, requests, outbound_encoding).await?;
            close_outbound(&mut upgrade).await?;
            ResponseStream::spawn(client, upgrade, response_encoding, deadline)
        }
        OpenUpgrade::TrailersOnly(result) => ResponseStream::trailers_only(result),
    };
    Ok(response_stream)
}

/// Result of awaiting an Upgrade-marked conn. Either the upgrade is live
/// (body + trailers to follow) or the server returned a trailers-only
/// response (HEADERS+END_STREAM with `grpc-status` in the initial frame).
enum OpenUpgrade {
    Streaming(Upgrade, Encoding),
    TrailersOnly(Result<(), Status>),
}

impl OpenUpgrade {
    /// Force the streaming variant. Used by unary + client-streaming which
    /// always expect a body. Trailers-only responses bypass the upgrade,
    /// surfacing as a Status error.
    fn into_streaming(self) -> Result<(Upgrade, Encoding), Status> {
        match self {
            OpenUpgrade::Streaming(u, enc) => Ok((u, enc)),
            OpenUpgrade::TrailersOnly(Ok(())) => {
                Err(Status::internal("response missing message body"))
            }
            OpenUpgrade::TrailersOnly(Err(status)) => Err(status),
        }
    }
}

async fn open_upgrade<C, Req>(
    client: &trillium_client::Client,
    path: &str,
) -> Result<OpenUpgrade, Status>
where
    C: Codec<Req>,
{
    let conn = grpc_request(client, path, <C as Codec<Req>>::content_type_suffix())
        .upgrade()
        .await
        .map_err(transport_error)?;

    validate_response_headers(&conn)?;
    let response_encoding = extract_response_encoding(&conn)?;

    if conn.response_headers().get_str("grpc-status").is_some() {
        return Ok(OpenUpgrade::TrailersOnly(Status::from_trailers(
            conn.response_headers(),
        )));
    }

    let upgrade: Upgrade = conn.into();
    Ok(OpenUpgrade::Streaming(upgrade, response_encoding))
}

async fn write_frame(upgrade: &mut Upgrade, frame: &[u8]) -> Result<(), Status> {
    upgrade
        .write_all(frame)
        .await
        .map_err(|e| Status::unavailable(format!("write error: {e}")))
}

/// Signal end-of-request-body to the server by closing the upgrade's write
/// side (which translates to an END_STREAM flag on the h2 stream). We have
/// no trailers to send, so this is the only way to mark "no more requests
/// coming" without dropping the upgrade entirely (we still need to read the
/// response).
async fn close_outbound(upgrade: &mut Upgrade) -> Result<(), Status> {
    upgrade
        .close()
        .await
        .map_err(|e| Status::unavailable(format!("close error: {e}")))
}

async fn write_request_stream<C, Req, S>(
    upgrade: &mut Upgrade,
    requests: S,
    outbound_encoding: Encoding,
) -> Result<(), Status>
where
    C: Codec<Req>,
    Req: Send + 'static,
    S: Stream<Item = Req> + Send + 'static,
{
    let mut requests = Box::pin(requests);
    while let Some(req) = requests.next().await {
        let frame = encode_frame::<C, Req>(&req, outbound_encoding)?;
        write_frame(upgrade, &frame).await?;
    }
    Ok(())
}

/// Read exactly one response message and confirm the body ended cleanly.
/// The second `next()` call drives the reader to EOF so trillium-http can
/// populate `received_trailers()`.
async fn read_one_response<C, Resp>(
    upgrade: &mut Upgrade,
    encoding: Encoding,
) -> Result<Resp, Status>
where
    C: Codec<Resp>,
    Resp: Send + 'static,
{
    let (first, second) = {
        let mut messages = MessageStream::<Resp, _>::new(&mut *upgrade, <C as Codec<Resp>>::decode)
            .with_encoding(encoding);
        let first = messages.next().await;
        let second = messages.next().await;
        (first, second)
    };

    match (first, second) {
        (Some(Ok(resp)), None) => Ok(resp),
        (Some(Ok(_)), Some(_)) => Err(Status::internal(
            "expected one response message, got multiple",
        )),
        (Some(Err(status)), _) | (_, Some(Err(status))) => Err(status),
        (None, _) => Err(finish_with_trailers(upgrade)
            .err()
            .unwrap_or_else(|| Status::internal("response missing message body"))),
    }
}

/// Inspect the trailers populated by trillium-http after read-to-EOF.
fn finish_with_trailers(upgrade: &Upgrade) -> Result<(), Status> {
    match upgrade.received_trailers() {
        Some(trailers) => Status::from_trailers(trailers),
        None => Err(Status::internal("stream ended without grpc-status trailer")),
    }
}

/// Compute the per-call deadline from the client's `grpc-timeout` default
/// header. Returns `None` when no timeout is configured.
fn deadline_from_client(client: &trillium_client::Client) -> Option<Instant> {
    let header = client.default_headers().get_str("grpc-timeout")?;
    let duration = parse_grpc_timeout(header)?;
    Some(Instant::now() + duration)
}

/// Race `work` against `deadline`. Without a deadline, just runs `work`
/// to completion.
async fn with_deadline<T, F>(
    client: &trillium_client::Client,
    deadline: Option<Instant>,
    work: F,
) -> Result<T, Status>
where
    F: std::future::Future<Output = Result<T, Status>>,
{
    match deadline {
        None => work.await,
        Some(deadline) => {
            let runtime = client.connector().runtime();
            race_against_deadline(&runtime, deadline, work).await
        }
    }
}

fn grpc_request(client: &trillium_client::Client, path: &str, suffix: &'static str) -> Conn {
    // TEMP: read a version override from a default-header marker so tests can
    // opt into h3 without a public ServiceClientExt method. If we ship h3
    // support this gets a real opt-in (set_http3 etc.) and this falls away.
    let version = match client.default_headers().get_str("x-trillium-grpc-version") {
        Some("h3") => Version::Http3,
        _ => Version::Http2,
    };
    client
        .post(path)
        .with_http_version(version)
        .with_request_header(
            KnownHeaderName::ContentType,
            format!("application/grpc+{suffix}"),
        )
        .with_request_header(KnownHeaderName::Te, "trailers")
        .with_request_header("grpc-accept-encoding", Encoding::accepted_encodings())
}

/// Resolve the inbound message encoding from the response's `grpc-encoding`.
/// Missing → `Identity`. Unknown → `Internal` (the server sent something we
/// can't decode despite advertising what we accept).
fn extract_response_encoding(conn: &Conn) -> Result<Encoding, Status> {
    match conn.response_headers().get_str("grpc-encoding") {
        None => Ok(Encoding::Identity),
        Some(s) => Encoding::from_grpc_encoding(s).ok_or_else(|| {
            Status::internal(format!("server returned unsupported grpc-encoding {s:?}"))
        }),
    }
}

/// Read the `grpc-encoding` header that
/// [`ServiceClientExt::set_outbound_compression`] stashed in the client's
/// default headers. Missing or unrecognized → `Identity`.
fn outbound_encoding_from_client(client: &trillium_client::Client) -> Encoding {
    client
        .default_headers()
        .get_str("grpc-encoding")
        .and_then(Encoding::from_grpc_encoding)
        .unwrap_or(Encoding::Identity)
}

fn validate_response_headers(conn: &Conn) -> Result<(), Status> {
    let http_status = conn.status();
    if http_status != Some(HttpStatus::Ok) {
        let n = http_status.map(|s| s as u16).unwrap_or(0);
        return Err(http_to_grpc_status(n));
    }

    let ct = conn
        .response_headers()
        .get_str(KnownHeaderName::ContentType);
    if ct.and_then(parse_grpc_content_type).is_none() {
        return Err(Status::internal(format!(
            "unexpected response content-type: {ct:?}"
        )));
    }

    Ok(())
}

/// Map an HTTP status code to a gRPC code per the gRPC HTTP/2 spec.
fn http_to_grpc_status(http: u16) -> Status {
    let code = match http {
        400 => Code::Internal,
        401 => Code::Unauthenticated,
        403 => Code::PermissionDenied,
        404 => Code::Unimplemented,
        429 | 502 | 503 | 504 => Code::Unavailable,
        _ => Code::Unknown,
    };
    Status::new(code, format!("HTTP {http}"))
}

fn transport_error(err: trillium_client::Error) -> Status {
    Status::unavailable(format!("transport error: {err}"))
}