backend: use gloable timeout for http request

This commit is contained in:
Julian Sparber 2019-02-02 19:55:02 +01:00
parent dd9a14ddce
commit 29a87501af
6 changed files with 16 additions and 29 deletions

View file

@ -5,7 +5,6 @@ use std::thread;
use url::Url;
use crate::error::Error;
use crate::globals;
use crate::util::json_q;
use crate::types::LoginRequest;
@ -16,6 +15,8 @@ use crate::types::RegisterResponse;
use crate::backend::types::BKResponse;
use crate::backend::types::Backend;
use crate::globals;
pub fn guest(bk: &Backend, server: &str) -> Result<(), Error> {
let baseu = Url::parse(server)?;
let url = baseu

View file

@ -453,8 +453,7 @@ pub fn set_room_avatar(bk: &Backend, roomid: &str, avatar: &str) -> Result<(), E
&roomurl,
&attrs,
|_| tx.send(BKResponse::SetRoomAvatar).unwrap(),
|err| tx.send(BKResponse::SetRoomAvatarError(err)).unwrap(),
0
|err| tx.send(BKResponse::SetRoomAvatarError(err)).unwrap()
);
}
};
@ -577,7 +576,7 @@ pub fn direct_chat(bk: &Backend, user: &Member, internal_id: String) -> Result<(
}
let attrs = json!(directs.clone());
put!(&direct_url, &attrs, |_| {}, |err| error!("{:?}", err), 0);
put!(&direct_url, &attrs, |_| {}, |err| error!("{:?}", err));
},
|err| {
tx.send(BKResponse::NewRoomError(err, internal_id)).unwrap();

View file

@ -8,8 +8,6 @@ use std::sync::{Arc, Mutex};
use std::thread;
use url::Url;
use crate::globals;
//use std::thread;
use crate::error::Error;
use crate::backend::types::BKCommand;
@ -187,12 +185,12 @@ fn get_scalar_token(data: &Arc<Mutex<BackendData>>) -> Result<String, Error> {
let uid = data.lock().unwrap().user_id.clone();
let url = url(data, &format!("user/{}/openid/request_token", uid), vec![])?;
let js = json_q("post", &url, &json!({}), globals::TIMEOUT)?;
let js = json_q("post", &url, &json!({}))?;
let vurl = base
.join("/api/register")
.expect("Wrong URL in get_scalar_token()");
let js = json_q("post", &vurl, &js, globals::TIMEOUT)?;
let js = json_q("post", &vurl, &js)?;
match js["scalar_token"].as_str() {
Some(st) => {

View file

@ -33,9 +33,7 @@ pub fn sync(bk: &Backend, new_since: Option<String>, initial: bool) -> Result<()
params.push(("since", since));
}
let timeout = if !initial {
time::Duration::from_secs(30)
} else {
if initial {
let filter = Filter {
room: Some(RoomFilter {
state: Some(RoomEventFilter {
@ -71,10 +69,9 @@ pub fn sync(bk: &Backend, new_since: Option<String>, initial: bool) -> Result<()
let filter_str =
serde_json::to_string(&filter).expect("Failed to serialize sync request filter");
params.push(("filter", filter_str));
Default::default()
};
let timeout = time::Duration::from_secs(30);
params.push(("timeout", timeout.as_secs().to_string()));
let baseu = bk.get_base_url();
@ -192,8 +189,7 @@ pub fn sync(bk: &Backend, new_since: Option<String>, initial: bool) -> Result<()
thread::sleep(time::Duration::from_secs(10));
tx.send(BKResponse::SyncError(err)).unwrap();
},
timeout.as_secs()
}
);
Ok(())

View file

@ -6,7 +6,6 @@ use std::io::prelude::*;
use crate::backend::types::BKResponse;
use crate::backend::types::Backend;
use crate::error::Error;
use crate::globals;
use crate::util::encode_uid;
use crate::util::get_user_avatar;
use crate::util::get_user_avatar_img;
@ -496,8 +495,7 @@ pub fn set_user_avatar(bk: &Backend, avatar: String) -> Result<(), Error> {
&url,
&attrs,
|_| tx.send(BKResponse::SetUserAvatar(avatar)).unwrap(),
|err| tx.send(BKResponse::SetUserAvatarError(err)).unwrap(),
0
|err| tx.send(BKResponse::SetUserAvatarError(err)).unwrap()
);
}
};

View file

@ -127,9 +127,9 @@ macro_rules! put {
#[macro_export]
macro_rules! query {
($method: expr, $url: expr, $attrs: expr, $okcb: expr, $errcb: expr, $timeout: expr) => {
($method: expr, $url: expr, $attrs: expr, $okcb: expr, $errcb: expr) => {
thread::spawn(move || {
let js = json_q($method, $url, $attrs, $timeout);
let js = json_q($method, $url, $attrs);
match js {
Ok(r) => $okcb(r),
@ -177,7 +177,7 @@ pub fn get_prev_batch_from(
let path = format!("rooms/{}/context/{}", roomid, evid);
let url = client_url(baseu, &path, params)?;
let r = json_q("get", &url, &json!(null), globals::TIMEOUT)?;
let r = json_q("get", &url, &json!(null))?;
let prev_batch = r["start"].to_string().trim_matches('"').to_string();
Ok(prev_batch)
@ -218,7 +218,7 @@ pub fn get_room_media_list(
let path = format!("rooms/{}/messages", roomid);
let url = client_url(baseu, &path, &params)?;
let r = json_q("get", &url, &json!(null), globals::TIMEOUT)?;
let r = json_q("get", &url, &json!(null))?;
let array = r["chunk"].as_array();
let prev_batch = r["end"].to_string().trim_matches('"').to_string();
if array.is_none() || array.unwrap().is_empty() {
@ -348,12 +348,7 @@ pub fn download_file(url: &str, fname: String, dest: Option<&str>) -> Result<Str
Ok(fname)
}
pub fn json_q(
method: &str,
url: &Url,
attrs: &JsonValue,
_timeout: u64,
) -> Result<JsonValue, Error> {
pub fn json_q(method: &str, url: &Url, attrs: &JsonValue) -> Result<JsonValue, Error> {
let mut conn = match method {
"post" => HTTP_CLIENT.post(url.as_str()),
"put" => HTTP_CLIENT.put(url.as_str()),
@ -398,7 +393,7 @@ pub fn get_user_avatar(baseu: &Url, userid: &str) -> Result<(String, String), Er
let url = client_url(baseu, &format!("profile/{}", encode_uid(userid)), &[])?;
let attrs = json!(null);
match json_q("get", &url, &attrs, globals::TIMEOUT) {
match json_q("get", &url, &attrs) {
Ok(js) => {
let name = match js["displayname"].as_str() {
Some(n) if n.is_empty() => userid.to_string(),