1
0

added concurrent HTTP

This commit is contained in:
Kozlov Maxim
2023-08-04 16:51:30 +06:00
parent 0d99194c21
commit ff99e5a8f6
3 changed files with 52 additions and 4 deletions

View File

@ -6,6 +6,7 @@ edition = "2021"
[lib]
crate-type = ["cdylib"]
# from https://github.com/johnthagen/min-sized-rust
[profile.release]
opt-level = "z" # Optimize for size.
lto = true # Enable Link Time Optimization
@ -14,6 +15,7 @@ panic = "abort" # Abort on panic
strip = true # Automatically strip symbols from the binary.
[dependencies]
base64 = "0.21.2"
color-eyre = "0.6.2"
log = "0.4.19"
log4rs = "1.2.0"

View File

@ -1,6 +1,6 @@
use std::ffi::{c_long, c_ushort};
use super::{types::TVariant, utils::os_string};
use super::{types::TVariant, utils::os_string_nil};
#[repr(C)]
struct ConnectionVTable {
@ -64,9 +64,9 @@ impl Connection {
pub fn external_event(&self, caller: &str, name: &str, data: &str) -> bool {
unsafe {
let caller_ptr = os_string(caller).as_mut_ptr();
let name_ptr = os_string(name).as_mut_ptr();
let data_ptr = os_string(data).as_mut_ptr();
let caller_ptr = os_string_nil(caller).as_mut_ptr();
let name_ptr = os_string_nil(name).as_mut_ptr();
let data_ptr = os_string_nil(data).as_mut_ptr();
(self.vptr1.external_event)(self, caller_ptr, name_ptr, data_ptr)
}
}

View File

@ -2,6 +2,7 @@ use super::MyAddInDescription;
use crate::add_in::ComponentFuncDescription;
use crate::ffi::utils::os_string;
use crate::ffi::{types::ParamValue, utils::from_os_string};
use base64::{engine::general_purpose, Engine as _};
use color_eyre::eyre::{eyre, Result};
use log::LevelFilter;
use log4rs::{
@ -47,6 +48,14 @@ impl MyAddInDescription {
),
callback: Self::fetch,
},
FunctionListElement {
description: ComponentFuncDescription::new::<1>(
&["ПолучитьХэТэТэПэПараллельно", "FetchHTTPConcurrently"],
true,
&[None],
),
callback: Self::concurrent_fetch,
},
FunctionListElement {
description: ComponentFuncDescription::new::<1>(
&[("ИнициализироватьЛоггер"), ("InitLogger")],
@ -103,6 +112,43 @@ impl MyAddInDescription {
Ok(Some(ParamValue::Str(os_string(&body))))
}
fn concurrent_fetch(
&mut self,
params: &[ParamValue],
) -> Result<Option<ParamValue>> {
let request_count = match params.get(0) {
Some(ParamValue::I32(val)) => *val,
_ => return Err(eyre!("Invalid parameter")),
};
if request_count < 0 {
return Err(eyre!("Invalid parameter"));
}
let request_count = request_count as u64;
let mut threads_handles = Vec::new();
for _ in 0..request_count {
let handle = thread::spawn(move || {
let Ok(result) = ureq::post("https://echo.hoppscotch.io").send_string("smth") else {return Err(eyre!("Failed to fetch"));};
let Ok(body) = result.into_string() else { return Err(eyre!("Failed to get body"));};
Ok(body)
});
threads_handles.push(handle);
}
let results: Vec<String> = threads_handles
.into_iter()
.map(|h| -> String {
let Ok(req_result) =
h.join() else { return format!("ERR")};
let Ok(req_text) = req_result else { return format!("ERR")};
let base64_text =
general_purpose::STANDARD_NO_PAD.encode(req_text);
base64_text
})
.collect();
Ok(Some(ParamValue::Str(os_string(&results.join(";")))))
}
fn init_logger(
&mut self,
params: &[ParamValue],