From ccf368df6d0cb1dfec1e56e1a82d8fcfa6b6c40a Mon Sep 17 00:00:00 2001 From: winstxnhdw Date: Sun, 2 Jul 2023 04:50:49 +0800 Subject: [PATCH] feat: add retry functionality --- Cargo.lock | 10 ++++++++++ Cargo.toml | 1 + src/compile.rs | 18 +++++++++--------- src/main.rs | 7 ++----- 4 files changed, 22 insertions(+), 14 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8487a43..45fa72b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2082,6 +2082,15 @@ dependencies = [ "winreg", ] +[[package]] +name = "retry" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9166d72162de3575f950507683fac47e30f6f2c3836b71b7fbc61aa517c9c5f4" +dependencies = [ + "rand", +] + [[package]] name = "rustc-demangle" version = "0.1.23" @@ -2410,6 +2419,7 @@ name = "tectonic-api" version = "0.1.0" dependencies = [ "axum", + "retry", "serde", "tectonic", "tokio", diff --git a/Cargo.toml b/Cargo.toml index e59ee88..24a5243 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,6 +5,7 @@ edition = "2021" [dependencies] axum = "0.6.18" +retry = "2.0.0" serde = { version = "^1.0.164", features = ["derive"] } tectonic = "0.14.1" tokio = { version = "1.29.1", features = ["full"] } diff --git a/src/compile.rs b/src/compile.rs index 5711888..b09b8ac 100644 --- a/src/compile.rs +++ b/src/compile.rs @@ -1,11 +1,10 @@ -use axum::{ - body::Body, - http::header::{CONTENT_DISPOSITION, CONTENT_TYPE}, - http::StatusCode, - response::{IntoResponse, Response}, - Json, -}; - +use axum::body::Body; +use axum::http::header::{CONTENT_DISPOSITION, CONTENT_TYPE}; +use axum::http::StatusCode; +use axum::response::{IntoResponse, Response}; +use axum::Json; +use retry::delay::NoDelay; +use retry::retry; use serde::Deserialize; use tectonic::latex_to_pdf; @@ -15,7 +14,8 @@ pub struct CompileSchema { } pub async fn compile(Json(payload): Json) -> impl IntoResponse { - let pdf = latex_to_pdf(payload.latex).expect("Unable to convert LaTeX to PDF"); + let pdf = retry(NoDelay.take(1), || latex_to_pdf(&payload.latex)) + .expect("Unable to convert LaTeX to PDF"); let body = Body::from(pdf); Response::builder() diff --git a/src/main.rs b/src/main.rs index d8a512d..5155124 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,11 +1,8 @@ mod compile; mod index; -use axum::{ - routing::{get, post}, - Router, Server, -}; - +use axum::routing::{get, post}; +use axum::{Router, Server}; use compile::compile; use index::index; use std::env::var;