diff --git a/src/goose.rs b/src/goose.rs index 9a299caa..e0fb57c1 100644 --- a/src/goose.rs +++ b/src/goose.rs @@ -872,27 +872,15 @@ impl GooseUser { base_url: Url, configuration: &GooseConfiguration, load_test_hash: u64, + reqwest_client: Option, ) -> Result { trace!("new GooseUser"); - // Either use manually configured timeout, or default. - let timeout = if configuration.timeout.is_some() { - match crate::util::get_float_from_string(configuration.timeout.clone()) { - Some(f) => f as u64 * 1_000, - None => GOOSE_REQUEST_TIMEOUT, - } - } else { - GOOSE_REQUEST_TIMEOUT + let client = match reqwest_client { + Some(c) => c, + None => create_reqwest_client(configuration)?, }; - let client = Client::builder() - .user_agent(APP_USER_AGENT) - .cookie_store(true) - .timeout(Duration::from_millis(timeout)) - // Enable gzip unless `--no-gzip` flag is enabled. - .gzip(!configuration.no_gzip) - .build()?; - Ok(GooseUser { started: Instant::now(), iterations: 0, @@ -919,7 +907,7 @@ impl GooseUser { /// Create a new single-use user. pub fn single(base_url: Url, configuration: &GooseConfiguration) -> Result { - let mut single_user = GooseUser::new(0, "".to_string(), base_url, configuration, 0)?; + let mut single_user = GooseUser::new(0, "".to_string(), base_url, configuration, 0, None)?; // Only one user, so index is 0. single_user.weighted_users_index = 0; // Do not throttle [`test_start`](../struct.GooseAttack.html#method.test_start) (setup) and @@ -2310,6 +2298,29 @@ impl GooseUser { } } +/// Internal helper function to create the default GooseUser reqwest client +pub(crate) fn create_reqwest_client( + configuration: &GooseConfiguration, +) -> Result { + // Either use manually configured timeout, or default. + let timeout = if configuration.timeout.is_some() { + match crate::util::get_float_from_string(configuration.timeout.clone()) { + Some(f) => f as u64 * 1_000, + None => GOOSE_REQUEST_TIMEOUT, + } + } else { + GOOSE_REQUEST_TIMEOUT + }; + + Client::builder() + .user_agent(APP_USER_AGENT) + .cookie_store(true) + .timeout(Duration::from_millis(timeout)) + // Enable gzip unless `--no-gzip` flag is enabled. + .gzip(!configuration.no_gzip) + .build() +} + /// Defines the HTTP requests that Goose makes. /// /// Can be manually created and configured with [`GooseRequest::builder`], but it's typically @@ -3175,7 +3186,7 @@ mod tests { const HOST: &str = "http://example.com/"; let configuration = GooseConfiguration::parse_args_default(&EMPTY_ARGS).unwrap(); let base_url = get_base_url(Some(HOST.to_string()), None, None).unwrap(); - let user = GooseUser::new(0, "".to_string(), base_url, &configuration, 0).unwrap(); + let user = GooseUser::new(0, "".to_string(), base_url, &configuration, 0, None).unwrap(); assert_eq!(user.scenarios_index, 0); assert_eq!(user.weighted_users_index, usize::max_value()); @@ -3202,7 +3213,7 @@ mod tests { Some("http://www.example.com/".to_string()), ) .unwrap(); - let user2 = GooseUser::new(0, "".to_string(), base_url, &configuration, 0).unwrap(); + let user2 = GooseUser::new(0, "".to_string(), base_url, &configuration, 0, None).unwrap(); // Confirm the URLs are correctly built using the scenario_host. let url = user2.build_url("/foo").unwrap(); @@ -3215,7 +3226,7 @@ mod tests { // Confirm Goose can build a base_url that includes a path. const HOST_WITH_PATH: &str = "http://example.com/with/path/"; let base_url = get_base_url(Some(HOST_WITH_PATH.to_string()), None, None).unwrap(); - let user = GooseUser::new(0, "".to_string(), base_url, &configuration, 0).unwrap(); + let user = GooseUser::new(0, "".to_string(), base_url, &configuration, 0, None).unwrap(); // Confirm the URLs are correctly built using the default_host that includes a path. let url = user.build_url("foo").unwrap(); diff --git a/src/lib.rs b/src/lib.rs index cfa2aa79..f8bfdef2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -750,6 +750,8 @@ impl GooseAttack { "initializing {} user states...", self.test_plan.total_users() ); + + let reqwest_client = goose::create_reqwest_client(&self.configuration)?; let mut weighted_users = Vec::new(); let mut user_count = 0; loop { @@ -770,6 +772,7 @@ impl GooseAttack { base_url, &self.configuration, self.metrics.hash, + Some(reqwest_client.clone()), )?); user_count += 1; if user_count == total_users {