use crate::sinks::{prelude::*, util::http::HttpRequest};
use super::{batch::HttpBatchSizer, request_builder::HttpRequestBuilder};
pub(super) struct HttpSink<S> {
service: S,
batch_settings: BatcherSettings,
request_builder: HttpRequestBuilder,
}
impl<S> HttpSink<S>
where
S: Service<HttpRequest<()>> + Send + 'static,
S::Future: Send + 'static,
S::Response: DriverResponse + Send + 'static,
S::Error: std::fmt::Debug + Into<crate::Error> + Send,
{
pub(super) const fn new(
service: S,
batch_settings: BatcherSettings,
request_builder: HttpRequestBuilder,
) -> Self {
Self {
service,
batch_settings,
request_builder,
}
}
async fn run_inner(self: Box<Self>, input: BoxStream<'_, Event>) -> Result<(), ()> {
input
.batched(self.batch_settings.as_item_size_config(HttpBatchSizer {
encoder: self.request_builder.encoder.encoder.clone(),
}))
.request_builder(
default_request_builder_concurrency_limit(),
self.request_builder,
)
.filter_map(|request| async move {
match request {
Err(error) => {
emit!(SinkRequestBuildError { error });
None
}
Ok(req) => Some(req),
}
})
.into_driver(self.service)
.run()
.await
}
}
#[async_trait::async_trait]
impl<S> StreamSink<Event> for HttpSink<S>
where
S: Service<HttpRequest<()>> + Send + 'static,
S::Future: Send + 'static,
S::Response: DriverResponse + Send + 'static,
S::Error: std::fmt::Debug + Into<crate::Error> + Send,
{
async fn run(
self: Box<Self>,
input: futures_util::stream::BoxStream<'_, Event>,
) -> Result<(), ()> {
self.run_inner(input).await
}
}