feat: add fetch timeout

This commit is contained in:
2022-07-17 11:59:13 +08:00
parent cbc05226e0
commit 15bd8076b1

View File

@@ -1,3 +1,4 @@
use std::time::Duration;
use anyhow::Result;
use serde::{Deserialize, Serialize};
use wasmtime::{Config, Engine, Instance, Linker, Module, Store};
@@ -15,6 +16,13 @@ struct JsResult {
message: String,
}
pub fn get(url: &str) -> reqwest::Result<reqwest::blocking::Response> {
let client = reqwest::blocking::Client::builder()
.timeout(Duration::from_secs(8))
.build()?;
client.get(url).send()
}
#[derive(Default)]
pub struct MyContainer;
@@ -23,7 +31,7 @@ impl container::Container for MyContainer {
println!("fetch arguments: {}", s);
let url: String = s.chars().skip(1).take(s.len() - 2).collect();
println!("fetch arguments URL: {}", url);
let r = match reqwest::blocking::get(&url) {
let r = match get(&url) {
Err(e) => return serde_json::to_string(&FetchResult {
error: Some(serde_json::to_string(&JsResult {
message: format!("failed: {}", e)
@@ -109,6 +117,7 @@ fn default_config() -> Result<Config> {
// Create an engine with caching enabled to assist with iteration in this project.
let mut config = Config::new();
config.cache_config_load_default()?;
// config.debug_info(true);
config.wasm_backtrace_details(wasmtime::WasmBacktraceDetails::Enable);
Ok(config)
}