Skip to content
This repository has been archived by the owner on Jul 15, 2024. It is now read-only.

Commit

Permalink
SwClient timings + in flight limit fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
MalteJanz committed Jun 29, 2024
1 parent dfcec5a commit 7a32696
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 23 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# NEXT-RELEASE
- "To-One-Association" values are now imported correctly
- Added profile `product_with_manufacturer.yaml` as an example
- Fixed reported request timings (they were measured wrong, longer than actual)
- Fixed `--in-flight-limit` to actually be respected (wasn't implemented correctly)
- Changed default `in_flight_limit` to `8` (from `16`) as that seemed like a better performing number

# v0.3.0
- Added `associations` entry for schema (used on export only)
Expand Down
43 changes: 22 additions & 21 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@ impl SwClient {
// and that doesn't have the association data as part of the entity object
default_headers.insert(header::ACCEPT, HeaderValue::from_static("application/json"));
let client = Client::builder()
.timeout(Duration::from_secs(10))
.timeout(Duration::from_secs(15))
.default_headers(default_headers)
.build()?;
let credentials = Arc::new(credentials);
let auth_response = Self::authenticate(&client, credentials.as_ref()).await?;

println!("Shopware API client with in_flight_limit={} created and authenticated", in_flight_limit);
Ok(Self {
client,
in_flight_semaphore: Arc::new(Semaphore::new(in_flight_limit)),
Expand All @@ -47,7 +48,6 @@ impl SwClient {
payload: &[T],
) -> Result<(), SwApiError> {
let entity: String = entity.into();
let start_instant = Instant::now();
println!(
"sync {:?} '{}' with payload size {}",
action,
Expand All @@ -65,16 +65,22 @@ impl SwClient {
};

let response = {
let _lock = self.in_flight_semaphore.acquire();
self.client
let _lock = self.in_flight_semaphore.acquire().await.unwrap();
let start_instant = Instant::now();
let res = self.client
.post(format!("{}/api/_action/sync", self.credentials.base_url))
.bearer_auth(access_token)
.header("single-operation", 1)
.header("indexing-behavior", "disable-indexing")
.header("sw-skip-trigger-flow", 1)
.json(&body)
.send()
.await?
.await?;
println!(
"sync request finished after {} ms",
start_instant.elapsed().as_millis()
);
res
};

if !response.status().is_success() {
Expand All @@ -83,11 +89,6 @@ impl SwClient {
return Err(SwApiError::Server(status, body));
}

println!(
"sync finished after {} ms",
start_instant.elapsed().as_millis()
);

Ok(())
}

Expand All @@ -97,7 +98,7 @@ impl SwClient {
// ToDo: implement retry on auth fail
let access_token = self.access_token.lock().unwrap().clone();
let response = {
let _lock = self.in_flight_semaphore.acquire();
let _lock = self.in_flight_semaphore.acquire().await.unwrap();
self.client
.get(format!(
"{}/api/_info/entity-schema.json",
Expand Down Expand Up @@ -130,7 +131,7 @@ impl SwClient {
let access_token = self.access_token.lock().unwrap().clone();

let response = {
let _lock = self.in_flight_semaphore.acquire();
let _lock = self.in_flight_semaphore.acquire().await.unwrap();
self.client
.post(format!(
"{}/api/search/{}",
Expand Down Expand Up @@ -175,23 +176,28 @@ impl SwClient {
entity: &str,
criteria: &Criteria,
) -> Result<SwListResponse, SwApiError> {
let start_instant = Instant::now();
// entity needs to be provided as kebab-case instead of snake_case
let entity = entity.replace('_', "-");

// ToDo: implement retry on auth fail
let access_token = self.access_token.lock().unwrap().clone();
let response = {
let _lock = self.in_flight_semaphore.acquire();
self.client
let _lock = self.in_flight_semaphore.acquire().await.unwrap();
let start_instant = Instant::now();
let res = self.client
.post(format!(
"{}/api/search/{}",
self.credentials.base_url, entity
))
.bearer_auth(access_token)
.json(criteria)
.send()
.await?
.await?;
println!(
"search request finished after {} ms",
start_instant.elapsed().as_millis()
);
res
};

if !response.status().is_success() {
Expand All @@ -202,11 +208,6 @@ impl SwClient {

let value: SwListResponse = Self::deserialize(response).await?;

println!(
"search request finished after {} ms",
start_instant.elapsed().as_millis()
);

Ok(value)
}

Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ enum Commands {
verbose: bool,

/// How many requests can be "in-flight" at the same time
#[arg(short, long, default_value = "16")]
#[arg(short, long, default_value = "8")]
in_flight_limit: usize,
},
}
Expand Down Expand Up @@ -138,7 +138,7 @@ async fn auth(domain: String, id: String, secret: String) -> anyhow::Result<()>
};

// check if credentials work
let _ = SwClient::new(credentials.clone(), 16).await?;
let _ = SwClient::new(credentials.clone(), 8).await?;

// write them to file
let serialized = toml::to_string(&credentials)?;
Expand Down

0 comments on commit 7a32696

Please sign in to comment.