From 276324ae09f4fa60c65518991cf89ce6bd7900ac Mon Sep 17 00:00:00 2001 From: philippeitis <33013301+philippeitis@users.noreply.github.com> Date: Fri, 30 Sep 2022 07:35:55 +0000 Subject: [PATCH 1/4] Make remove_json_null_values O(n) instead of O(n^2) --- google-apis-common/src/lib.rs | 32 +++++++------------------------- 1 file changed, 7 insertions(+), 25 deletions(-) diff --git a/google-apis-common/src/lib.rs b/google-apis-common/src/lib.rs index 3540fc9b46..a9c696cf05 100644 --- a/google-apis-common/src/lib.rs +++ b/google-apis-common/src/lib.rs @@ -752,32 +752,14 @@ where // TODO(ST): Allow sharing common code between program types pub fn remove_json_null_values(value: &mut json::value::Value) { - match *value { - json::value::Value::Object(ref mut map) => { - let mut for_removal = Vec::new(); - - for (key, mut value) in map.iter_mut() { - if value.is_null() { - for_removal.push(key.clone()); - } else { - remove_json_null_values(&mut value); - } - } - - for key in &for_removal { - map.remove(key); - } + match value { + json::value::Value::Object(map) => { + map.retain(|_, value| !value.is_null()); + map.values_mut().for_each(remove_json_null_values); } - json::value::Value::Array(ref mut arr) => { - let mut i = 0; - while i < arr.len() { - if arr[i].is_null() { - arr.remove(i); - } else { - remove_json_null_values(&mut arr[i]); - i += 1; - } - } + json::value::Value::Array(arr) => { + arr.retain(|value| !value.is_null()); + arr.iter_mut().for_each(remove_json_null_values); } _ => {} } From 811ed3d016c7465e95628b91c0828837aeef2f88 Mon Sep 17 00:00:00 2001 From: philippeitis <33013301+philippeitis@users.noreply.github.com> Date: Fri, 30 Sep 2022 07:42:11 +0000 Subject: [PATCH 2/4] Replace std::thread::sleep with tokio::thread::sleep --- google-apis-common/src/lib.rs | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/google-apis-common/src/lib.rs b/google-apis-common/src/lib.rs index a9c696cf05..5fd46e5efd 100644 --- a/google-apis-common/src/lib.rs +++ b/google-apis-common/src/lib.rs @@ -5,7 +5,6 @@ use std::future::Future; use std::io::{self, Cursor, Read, Seek, SeekFrom, Write}; use std::pin::Pin; use std::str::FromStr; -use std::thread::sleep; use std::time::Duration; use itertools::Itertools; @@ -21,6 +20,7 @@ use mime::{Attr, Mime, SubLevel, TopLevel, Value}; use serde_json as json; use tokio::io::{AsyncRead, AsyncWrite}; +use tokio::time::sleep; use tower_service; pub use yup_oauth2 as oauth2; @@ -597,7 +597,6 @@ where pub media_type: Mime, pub content_length: u64, } - impl<'a, A, S> ResumableUploadHelper<'a, A, S> where S: tower_service::Service + Clone + Send + Sync + 'static, @@ -639,7 +638,7 @@ where } None | Some(_) => { if let Retry::After(d) = self.delegate.http_failure(&r, None) { - sleep(d); + sleep(d).await; continue; } return Err(Ok(r)); @@ -649,7 +648,7 @@ where } Err(err) => { if let Retry::After(d) = self.delegate.http_error(&err) { - sleep(d); + sleep(d).await; continue; } return Err(Err(err)); @@ -732,7 +731,7 @@ where &reconstructed_result, json::from_str(&res_body_string).ok(), ) { - sleep(d); + sleep(d).await; continue; } } @@ -740,7 +739,7 @@ where } Err(err) => { if let Retry::After(d) = self.delegate.http_error(&err) { - sleep(d); + sleep(d).await; continue; } return Some(Err(err)); From 27891b8a10ae3f62d13f8455fe57afc7ca5f9c4f Mon Sep 17 00:00:00 2001 From: philippeitis <33013301+philippeitis@users.noreply.github.com> Date: Sat, 1 Oct 2022 01:34:56 +0000 Subject: [PATCH 3/4] Use tokio::time::sleep over std::thread::sleep in api.rs as well --- src/generator/templates/api/lib/mbuild.mako | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/generator/templates/api/lib/mbuild.mako b/src/generator/templates/api/lib/mbuild.mako index 7ba7c06a75..4e45eba0e5 100644 --- a/src/generator/templates/api/lib/mbuild.mako +++ b/src/generator/templates/api/lib/mbuild.mako @@ -819,7 +819,7 @@ else { match req_result { Err(err) => { if let client::Retry::After(d) = dlg.http_error(&err) { - sleep(d); + sleep(d).await; continue; } ${delegate_finish}(false); @@ -835,7 +835,7 @@ else { let server_response = json::from_str::(&res_body_string).ok(); if let client::Retry::After(d) = dlg.http_failure(&restored_response, server_response.clone()) { - sleep(d); + sleep(d).await; continue; } From 2b5f119242137e72ac2961a0b704109a277797a5 Mon Sep 17 00:00:00 2001 From: philippeitis <33013301+philippeitis@users.noreply.github.com> Date: Sat, 1 Oct 2022 01:36:23 +0000 Subject: [PATCH 4/4] Change dependency in api.rs --- src/generator/templates/api/api.rs.mako | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/generator/templates/api/api.rs.mako b/src/generator/templates/api/api.rs.mako index 4de2146c9c..8bab33388c 100644 --- a/src/generator/templates/api/api.rs.mako +++ b/src/generator/templates/api/api.rs.mako @@ -24,11 +24,11 @@ use serde_json as json; use std::io; use std::fs; use std::mem; -use std::thread::sleep; use http::Uri; use hyper::client::connect; use tokio::io::{AsyncRead, AsyncWrite}; +use tokio::time::sleep; use tower_service; use serde::{Serialize, Deserialize};