Expand description
A rust runtime for Tencent Cloud Serverless Compute Function
This library provides a basic custom runtime for a rust application on Tencent Cloud as Serverless Compute Function. For complete setup, see Setup.
§start
vs start_uncatched
There are two varaints of start
function to start the runtime, to sum up:
start
catches panic from the provided serverless compute function, but this requires the function to beRefUnwindSafe
. Most pure function should satisfy this.start_uncatched
allows the serverless compute function to panic the whole process and relies on the cloud to tear down the executor and restart. The downside of this is when a query causes panic on the function, the cloud typically waits until timeout before acknowledging the failure. Sometimes this is necessary to reference!RefUnwindSafe
types. Connection pool, for example, usually is not unwind-safe.
§Built-in Events
When enabling the correspondent builtin-*
feature, auto serialization and deserialization of
certain built-in events and reponses are supported.
Current supported built-in events/responses are:
-
API Gateway: enabled by feature
builtin-api-gateway
, supports trigger as described in API Gateway Trigger.builtin::api::Request
andbuiltin::api::Response
will be provided, which are re-export ofhttp::Request
andhttp::Response
with support of auto deserialization/serialization.For more information, see the module-level documentation.
use std::convert::Infallible; use tencent_scf::{ builtin::api::{Request, Response, ResponseBuilder}, make_scf, start, Context, }; fn main() { let scf = make_scf( |event: Request<String>, _context: Context| -> Result<Response<String>, Infallible> { Ok(ResponseBuilder::new() .status(200) .body("Hello World".to_string()) .unwrap()) }, ); start(scf); }
§Custom Event and Response
Custom types of event and reponse are supported.
§Auto Serialization/Deserialization
Any type that implements serde::Deserialize
and is marked as convert::AsJson
can be
deserialized from JSON automatically when feature “json” is enabled. Similarly for types
implementing serde::Serialize
and is marked as convert::AsJson
.
§Example
use std::convert::Infallible;
use serde::{Deserialize, Serialize};
// with "json" feature enabled
use tencent_scf::{convert::AsJson, make_scf, start, Context};
// define a custom event
#[derive(Deserialize)]
struct CustomEvent {
a: i32,
b: i32,
}
// mark the custom event as json for auto deserialization
impl AsJson for CustomEvent {}
// define a custom response
#[derive(Serialize)]
struct CustomResponse {
a_plus_b: i32,
}
// mark the custom response as json for auto serialization
impl AsJson for CustomResponse {}
// make the scf
let scf = make_scf(
|event: CustomEvent, _context: Context| -> Result<CustomResponse, Infallible> {
Ok(CustomResponse {
a_plus_b: event.a + event.b,
})
},
);
// start the runtime in the main function
start(scf);
§Manual Serialization/Deserialization
User can also chose to implement convert::FromReader
for incoming events and
convert::IntoBytes
for outgoing response.
Modules§
- builtin
- Collection of built-in
Event
andResponse
for auto-deserialization/serialization - convert
- Module that contains traits for converting incoming payload to
Event
andResponse
to outgoing payload.
Structs§
- Context
- The context of the invocation, assembled from environment variables and invocation headers.
Traits§
- Serverless
Compute Function - Main trait for a serverless compute function.
Functions§
- make_
scf - Create a
ServerlessComputeFunction
from a closure. - start
- Start the runtime with the given serverless compute function.
- start_
uncatched - Start the runtime with the given serverless compute function without catching panics.