1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
use super::{client::Client, request_builder::SSRequestBuilder, service::SSService};
use crate::sinks::aws_s_s::retry::SSRetryLogic;
use crate::sinks::prelude::*;

#[derive(Clone)]
pub(super) struct SSSink<C, E>
where
    C: Client<E> + Clone + Send + Sync + 'static,
    E: std::fmt::Debug + std::fmt::Display + std::error::Error + Sync + Send + 'static,
{
    request_builder: SSRequestBuilder,
    service: SSService<C, E>,
    request: TowerRequestConfig,
}

impl<C, E> SSSink<C, E>
where
    C: Client<E> + Clone + Send + Sync + 'static,
    E: std::fmt::Debug + std::fmt::Display + std::error::Error + Sync + Send + 'static,
{
    pub(super) fn new(
        request_builder: SSRequestBuilder,
        request: TowerRequestConfig,
        publisher: C,
    ) -> crate::Result<Self> {
        Ok(SSSink {
            request_builder,
            service: SSService::new(publisher),
            request,
        })
    }

    async fn run_inner(self: Box<Self>, input: BoxStream<'_, Event>) -> Result<(), ()> {
        let request = self.request.into_settings();
        let retry_logic: SSRetryLogic<E> = super::retry::SSRetryLogic::new();
        let service = tower::ServiceBuilder::new()
            .settings(request, retry_logic)
            .service(self.service);

        input
            .request_builder(
                default_request_builder_concurrency_limit(),
                self.request_builder,
            )
            .filter_map(|req| async move {
                req.map_err(|error| {
                    emit!(SinkRequestBuildError { error });
                })
                .ok()
            })
            .into_driver(service)
            .run()
            .await
    }
}

#[async_trait::async_trait]
impl<C, E> StreamSink<Event> for SSSink<C, E>
where
    C: Client<E> + Clone + Send + Sync + 'static,
    E: std::fmt::Debug + std::fmt::Display + std::error::Error + Sync + Send + 'static,
{
    async fn run(mut self: Box<Self>, input: BoxStream<'_, Event>) -> Result<(), ()> {
        self.run_inner(input).await
    }
}