Skip to content

Commit da45ca4

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

File tree

1 file changed

+32
-8
lines changed

1 file changed

+32
-8
lines changed

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)