Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

read json from response buffer instead of copying #25

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 19 additions & 20 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,11 +248,10 @@ impl Consul {
/// [ConsulError](consul::ConsulError) describes all possible errors returned by this api.
pub async fn read_key(&self, request: ReadKeyRequest<'_>) -> Result<Vec<ReadKeyResponse>> {
let req = self.build_read_key_req(request);
let (mut response_body, _index) = self
let (response_body, _index) = self
.execute_request(req, hyper::Body::empty(), None, READ_KEY_METHOD_NAME)
.await?;
let bytes = response_body.copy_to_bytes(response_body.remaining());
serde_json::from_slice::<Vec<ReadKeyResponse>>(&bytes)
serde_json::from_reader::<_, Vec<ReadKeyResponse>>(response_body.reader())
.map_err(ConsulError::ResponseDeserializationFailed)?
.into_iter()
.map(|mut r| {
Expand Down Expand Up @@ -281,17 +280,17 @@ impl Consul {
) -> Result<(bool, u64)> {
let url = self.build_create_or_update_url(request);
let req = hyper::Request::builder().method(Method::PUT).uri(url);
let (mut response_body, index) = self
let (response_body, index) = self
.execute_request(
req,
Body::from(value),
None,
CREATE_OR_UPDATE_KEY_METHOD_NAME,
)
.await?;
let bytes = response_body.copy_to_bytes(response_body.remaining());
Ok((
serde_json::from_slice(&bytes).map_err(ConsulError::ResponseDeserializationFailed)?,
serde_json::from_reader(response_body.reader())
.map_err(ConsulError::ResponseDeserializationFailed)?,
index,
))
}
Expand Down Expand Up @@ -359,11 +358,11 @@ impl Consul {

url = add_namespace_and_datacenter(url, request.namespace, request.datacenter);
req = req.uri(url);
let (mut response_body, _index) = self
let (response_body, _index) = self
.execute_request(req, hyper::Body::empty(), None, DELETE_KEY_METHOD_NAME)
.await?;
let bytes = response_body.copy_to_bytes(response_body.remaining());
serde_json::from_slice(&bytes).map_err(ConsulError::ResponseDeserializationFailed)
serde_json::from_reader(response_body.reader())
.map_err(ConsulError::ResponseDeserializationFailed)
}

/// Obtains a lock against a specific key in consul. See the [consul docs](https://learn.hashicorp.com/tutorials/consul/application-leader-elections?in=consul/developer-configuration) for more information.
Expand Down Expand Up @@ -473,17 +472,17 @@ impl Consul {
let request = hyper::Request::builder()
.method(Method::GET)
.uri(uri.clone());
let (mut response_body, index) = self
let (response_body, index) = self
.execute_request(
request,
hyper::Body::empty(),
query_opts.timeout,
GET_ALL_REGISTERED_SERVICE_NAMES_METHOD_NAME,
)
.await?;
let bytes = response_body.copy_to_bytes(response_body.remaining());
let service_tags_by_name = serde_json::from_slice::<HashMap<String, Vec<String>>>(&bytes)
.map_err(ConsulError::ResponseDeserializationFailed)?;
let service_tags_by_name =
serde_json::from_reader::<_, HashMap<String, Vec<String>>>(response_body.reader())
.map_err(ConsulError::ResponseDeserializationFailed)?;

Ok(ResponseMeta {
response: service_tags_by_name.keys().cloned().collect(),
Expand All @@ -505,17 +504,17 @@ impl Consul {
) -> Result<ResponseMeta<GetServiceNodesResponse>> {
let query_opts = query_opts.unwrap_or_default();
let req = self.build_get_service_nodes_req(request, &query_opts);
let (mut response_body, index) = self
let (response_body, index) = self
.execute_request(
req,
hyper::Body::empty(),
query_opts.timeout,
GET_SERVICE_NODES_METHOD_NAME,
)
.await?;
let bytes = response_body.copy_to_bytes(response_body.remaining());
let response = serde_json::from_slice::<GetServiceNodesResponse>(&bytes)
.map_err(ConsulError::ResponseDeserializationFailed)?;
let response =
serde_json::from_reader::<_, GetServiceNodesResponse>(response_body.reader())
.map_err(ConsulError::ResponseDeserializationFailed)?;
Ok(ResponseMeta { response, index })
}

Expand Down Expand Up @@ -623,16 +622,16 @@ impl Consul {
req = req.uri(url);
let create_session_json =
serde_json::to_string(&session_req).map_err(ConsulError::InvalidRequest)?;
let (mut response_body, _index) = self
let (response_body, _index) = self
.execute_request(
req,
hyper::Body::from(create_session_json),
None,
GET_SESSION_METHOD_NAME,
)
.await?;
let bytes = response_body.copy_to_bytes(response_body.remaining());
serde_json::from_slice(&bytes).map_err(ConsulError::ResponseDeserializationFailed)
serde_json::from_reader(response_body.reader())
.map_err(ConsulError::ResponseDeserializationFailed)
}

fn build_get_service_nodes_req(
Expand Down
Loading