Skip to content

Commit bd14bbd

Browse files
committed
feat: use reader for passing body to avoid expensive clone
1 parent fccd5ba commit bd14bbd

File tree

2 files changed

+33
-11
lines changed

2 files changed

+33
-11
lines changed

llm/bedrock/src/client.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,15 +177,13 @@ impl WasiSleep {
177177
}
178178

179179
impl AsyncSleep for WasiSleep {
180-
#[allow(clippy::missing_transmute_annotations)]
181180
fn sleep(&self, duration: std::time::Duration) -> Sleep {
182181
let reactor = self.reactor.clone();
183-
184182
let fut = async move {
185183
let nanos = duration.as_nanos() as u64;
186184
let pollable = monotonic_clock::subscribe_duration(nanos);
187185

188-
reactor.clone().wait_for(pollable).await;
186+
reactor.wait_for(pollable).await;
189187
};
190188
Sleep::new(Box::pin(UnsafeFuture::new(fut)))
191189
}

llm/bedrock/src/wasi_client.rs

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{str::FromStr, sync::Arc};
1+
use std::{io::Read, str::FromStr, sync::Arc};
22

33
use aws_sdk_bedrockruntime::{config, error::ConnectorError};
44
use aws_smithy_runtime_api::{
@@ -82,13 +82,10 @@ impl WasiConnector {
8282
);
8383
}
8484

85-
let mut request = self.0.request(method, url).headers(header_map);
86-
87-
if let Some(bytes) = parts.body.bytes() {
88-
request = request.body(bytes.to_owned());
89-
}
90-
91-
request
85+
self.0
86+
.request(method, url)
87+
.headers(header_map)
88+
.body(reqwest::Body::new::<BodyReader>(parts.body.into()))
9289
.send()
9390
.await
9491
.map_err(|e| ConnectorError::other(e.into(), None))
@@ -138,3 +135,30 @@ impl HttpConnector for SharedWasiConnector {
138135
HttpConnectorFuture::new(UnsafeFuture::new(future))
139136
}
140137
}
138+
139+
struct BodyReader {
140+
body: SdkBody,
141+
}
142+
143+
impl From<SdkBody> for BodyReader {
144+
fn from(value: SdkBody) -> Self {
145+
Self::new(value)
146+
}
147+
}
148+
149+
impl BodyReader {
150+
fn new(body: SdkBody) -> Self {
151+
Self { body }
152+
}
153+
}
154+
155+
impl Read for BodyReader {
156+
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
157+
let bytes = self.body.bytes();
158+
159+
match bytes {
160+
Some(mut bytes) => bytes.read(buf),
161+
None => Ok(0),
162+
}
163+
}
164+
}

0 commit comments

Comments
 (0)