web3-proxy/web3_proxy/src/frontend/streaming.rs
Bryan Stitt e917a11d6c
Suprisingly large refactor to get ids everywhere (#222)
* cargo upgrade --incompatible and update

* first draft at suprisingly_large_refactor_to_get_ids_everywhere

* put app in a task_local

* ref cleanup

* use a OnceLock instead of a tokio local

* test more methods

* APP isn't set in all tests

* it compiles. tests fail. todos still open

* use the app only when necessary

* more tests. less panic

* less verbose debug impl

* short enum names

* move kafka and secrets to their own files

* main tests pass

* add debug chain block time

* helper for stats that ignores internal stats

* Update Jenkinsfile (#223)

* more tests

---------

Co-authored-by: Pewxz <124064710+pewxz@users.noreply.github.com>
2023-10-03 13:46:27 -07:00

41 lines
1.1 KiB
Rust

use axum::{body::BoxBody, response::IntoResponse};
use bytes::Bytes;
use futures::StreamExt;
use http::Response;
use std::pin::Pin;
use std::task::{Context, Poll};
use tokio::stream::Stream;
struct SizingBody<B> {
inner: B,
web3_request: RequestMetadata,
}
impl<B> SizingBody<B> {
fn new(inner: B) -> Self {
Self { inner, size: 0 }
}
}
impl<B> Stream for SizingBody<B>
where
B: Stream<Item = Result<Bytes, Box<dyn std::error::Error + Send + Sync>>> + Unpin,
{
type Item = Result<Bytes, Box<dyn std::error::Error + Send + Sync>>;
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
match Pin::new(&mut self.inner).poll_next(cx) {
Poll::Ready(Some(Ok(chunk))) => {
self.size += chunk.len();
Poll::Ready(Some(Ok(chunk)))
}
Poll::Ready(Some(Err(e))) => Poll::Ready(Some(Err(e))),
Poll::Ready(None) => {
println!("Final response size: {}", self.size);
Poll::Ready(None)
}
Poll::Pending => Poll::Pending,
}
}
}