tracing_gelf/connection/
mod.rs1mod tcp;
2mod udp;
3
4use std::{io, net::SocketAddr};
5
6use bytes::Bytes;
7use tokio::net::{lookup_host, ToSocketAddrs};
8use tokio_stream::wrappers::ReceiverStream;
9use tracing_core::subscriber::NoSubscriber;
10use tracing_futures::WithSubscriber;
11
12pub use tcp::*;
13pub use udp::*;
14
15#[derive(Debug)]
20pub struct ConnectionErrors(pub Vec<(SocketAddr, io::Error)>);
21
22#[derive(Debug)]
26#[must_use]
27pub struct ConnectionHandle<A, Conn> {
28 pub(crate) addr: A,
29 pub(crate) receiver: ReceiverStream<Bytes>,
30 pub(crate) conn: Conn,
31}
32
33impl<A, Conn> ConnectionHandle<A, Conn> {
34 pub fn address(&self) -> &A {
36 &self.addr
37 }
38}
39
40impl<A> ConnectionHandle<A, TcpConnection>
41where
42 A: ToSocketAddrs,
43{
44 pub async fn connect(&mut self) -> ConnectionErrors {
48 let addrs = lookup_host(&self.addr).await.into_iter().flatten();
50
51 let mut errors = Vec::new();
53 for addr in addrs {
54 let fut = self
55 .conn
56 .handle(addr, &mut self.receiver)
57 .with_subscriber(NoSubscriber::default());
58 if let Err(err) = fut.await {
59 errors.push((addr, err));
60 }
61 }
62 ConnectionErrors(errors)
63 }
64}
65
66#[cfg(feature = "rustls-tls")]
67impl<A> ConnectionHandle<A, TlsConnection>
68where
69 A: ToSocketAddrs,
70{
71 pub async fn connect(&mut self) -> ConnectionErrors {
75 let addrs = lookup_host(&self.addr).await.into_iter().flatten();
77
78 let mut errors = Vec::new();
80 for addr in addrs {
81 let fut = self
82 .conn
83 .handle(addr, &mut self.receiver)
84 .with_subscriber(NoSubscriber::default());
85 if let Err(err) = fut.await {
86 errors.push((addr, err));
87 }
88 }
89 ConnectionErrors(errors)
90 }
91}
92
93impl<A> ConnectionHandle<A, UdpConnection>
94where
95 A: ToSocketAddrs,
96{
97 pub async fn connect(&mut self) -> ConnectionErrors {
101 let addrs = lookup_host(&self.addr).await.into_iter().flatten();
103
104 let mut errors = Vec::new();
106 for addr in addrs {
107 let fut = self
108 .conn
109 .handle(addr, &mut self.receiver)
110 .with_subscriber(NoSubscriber::default());
111 if let Err(err) = fut.await {
112 errors.push((addr, err));
113 }
114 }
115 ConnectionErrors(errors)
116 }
117}