1
- use anyhow:: { Result , Error } ;
1
+ use anyhow:: { Error , Result } ;
2
2
use futures_util:: future:: OptionFuture ;
3
- use futures_util:: stream:: { BoxStream , Stream } ;
4
- use futures_util:: { FutureExt , StreamExt , TryStreamExt } ;
3
+ use futures_util:: stream:: { Stream , TryStream } ;
4
+ use futures_util:: { FutureExt , TryStreamExt } ;
5
5
use metrics:: { metrics, metrics_wrapper} ;
6
6
use sqlx:: PgPool ;
7
7
use tokio:: io:: { Error as IoError , Result as IoResult } ;
8
8
use tokio:: net:: TcpListener ;
9
9
use tokio:: net:: ToSocketAddrs ;
10
10
use tokio_stream:: wrappers:: TcpListenerStream ;
11
- use tracing:: { debug_span, info} ;
11
+ use tracing:: info;
12
+ use tokio:: io:: { AsyncWrite , AsyncRead } ;
12
13
13
- use crate :: api:: proxy:: { PeerAddr , ProxyStream } ;
14
- use futures_util:: io:: { AsyncRead , AsyncWrite } ;
14
+ use crate :: api:: proxy:: { ProxyProtocol , ToProxyStream } ;
15
15
16
16
mod metrics;
17
17
mod proxy;
18
18
mod routes;
19
19
mod tls;
20
20
21
- type Connection = Box < dyn PeerAddr < IoError > + Send + Unpin + ' static > ;
22
- type Listener = BoxStream < ' static , IoResult < Connection > > ;
23
-
24
- pub struct Api < H , S > {
21
+ pub struct Api < H , P , S > {
25
22
http : Option < H > ,
26
23
https : Option < S > ,
27
- prom : Option < H > ,
24
+ prom : Option < P > ,
28
25
pool : PgPool ,
29
26
}
30
27
31
- impl < H , S > Api < H , S > {
32
- fn prepare_listener ( listener : TcpListener , proxy : bool ) -> Listener {
33
- let listener = match listener {
34
- Some ( listener) => TcpListenerStream :: new ( listener) ,
35
- None => return None ,
36
- } ;
37
-
38
- let mapper = match proxy {
39
- true => |stream| Box :: new ( ProxyStream :: from ( stream) ) as Connection ,
40
- false => |stream| Box :: new ( stream) as Connection
41
- } ;
42
-
43
- let listener = listener
44
- . map_ok ( mapper)
45
- . boxed ( ) ;
46
-
47
- Some ( listener)
48
- }
28
+ pub async fn new < A : ToSocketAddrs > (
29
+ ( http, http_proxy) : ( Option < A > , bool ) ,
30
+ ( https, https_proxy) : ( Option < A > , bool ) ,
31
+ ( prom, prom_proxy) : ( Option < A > , bool ) ,
32
+ pool : PgPool ,
33
+ ) -> Result <
34
+ Api <
35
+ impl Stream < Item = IoResult < impl AsyncRead + AsyncWrite + Send + Unpin > > + Send ,
36
+ impl Stream < Item = IoResult < impl AsyncRead + AsyncWrite + Send + Unpin > > + Send ,
37
+ impl Stream < Item = Result < impl AsyncRead + AsyncWrite + Send + Unpin , Error > >
38
+ + Send ,
39
+ > ,
40
+ > {
41
+ let http = OptionFuture :: from ( http. map ( TcpListener :: bind) ) . map ( Option :: transpose) ;
42
+ let https = OptionFuture :: from ( https. map ( TcpListener :: bind) ) . map ( Option :: transpose) ;
43
+ let prom = OptionFuture :: from ( prom. map ( TcpListener :: bind) ) . map ( Option :: transpose) ;
44
+
45
+ let ( http, https, prom) = tokio:: try_join!( http, https, prom) ?;
46
+
47
+ let http = http. map ( |http| {
48
+ let http =
49
+ TcpListenerStream :: new ( http) . map_ok ( |stream| stream. source ( ProxyProtocol :: Enabled ) ) ;
50
+ proxy:: wrap ( http) . try_buffer_unordered ( 100 )
51
+ } ) ;
52
+ let https = https. map ( |https| {
53
+ let https =
54
+ TcpListenerStream :: new ( https) . map_ok ( |stream| stream. source ( ProxyProtocol :: Enabled ) ) ;
55
+ tls:: stream ( https, pool. clone ( ) )
56
+ } ) ;
57
+ let prom = prom. map ( |prom| {
58
+ let prom =
59
+ TcpListenerStream :: new ( prom) . map_ok ( |stream| stream. source ( ProxyProtocol :: Enabled ) ) ;
60
+ proxy:: wrap ( prom) . try_buffer_unordered ( 100 )
61
+ } ) ;
62
+
63
+ Ok ( Api {
64
+ http,
65
+ https,
66
+ prom,
67
+ pool,
68
+ } )
69
+ }
49
70
50
- pub async fn new < A : ToSocketAddrs > (
51
- ( http, http_proxy) : ( Option < A > , bool ) ,
52
- ( https, https_proxy) : ( Option < A > , bool ) ,
53
- ( prom, prom_proxy) : ( Option < A > , bool ) ,
54
- pool : PgPool ,
55
- ) -> Result < Api <
56
- impl Stream < Item = IoResult < impl AsyncRead + AsyncWrite + Send + Unpin + ' static > > + Send ,
57
- impl Stream < Item = Result < impl AsyncRead + AsyncWrite + Send + Unpin + ' static , Error > > + Send
58
- > > {
59
- let http = OptionFuture :: from ( http. map ( TcpListener :: bind) ) . map ( Option :: transpose) ;
60
- let https = OptionFuture :: from ( https. map ( TcpListener :: bind) ) . map ( Option :: transpose) ;
61
- let prom = OptionFuture :: from ( prom. map ( TcpListener :: bind) ) . map ( Option :: transpose) ;
62
-
63
- let ( http, https, prom) = tokio:: try_join!( http, https, prom) ?;
64
-
65
- let http = Api :: prepare_listener ( http, http_proxy) ;
66
- let https = Api :: prepare_listener ( https, https_proxy) ;
67
- let prom = Api :: prepare_listener ( prom, prom_proxy) ;
68
-
69
- Ok ( Api {
70
- http : proxy:: wrap ( http) . try_buffer_unordered ( 100 ) ,
71
- https : tls:: stream ( https, pool. clone ( ) ) ,
72
- prom : proxy:: wrap ( prom) . try_buffer_unordered ( 100 ) ,
73
- pool,
74
- } )
75
- }
71
+ impl < H , P , S > Api < H , P , S >
72
+ where
73
+ H : TryStream < Error = IoError > + Send + Unpin + ' static ,
74
+ H :: Ok : AsyncRead + AsyncWrite + Send + Unpin + ' static ,
75
+ P : TryStream < Error = IoError > + Send + Unpin + ' static ,
76
+ P :: Ok : AsyncRead + AsyncWrite + Send + Unpin + ' static ,
77
+ S : TryStream < Error = Error > + Send + Unpin + ' static ,
78
+ S :: Ok : AsyncRead + AsyncWrite + Send + Unpin + ' static ,
79
+ {
76
80
77
81
#[ tracing:: instrument( name = "Api::spawn" , skip( self ) ) ]
78
82
pub async fn spawn ( self ) -> Result < ( ) > {
@@ -82,30 +86,17 @@ impl <H, S> Api<H, S> {
82
86
83
87
let http = self
84
88
. http
85
- . map ( |http|
86
- proxy:: wrap ( http) . try_buffer_unordered ( 100 )
87
- )
88
89
. map ( |http| warp:: serve ( routes. clone ( ) ) . serve_incoming ( http) )
89
90
. map ( tokio:: spawn) ;
90
91
91
92
let pool = self . pool . clone ( ) ;
92
93
let https = self
93
94
. https
94
- . map ( |https| {
95
- //let addr = https.as_ref().local_addr();
96
- tls:: stream ( https, pool)
97
- //.instrument(debug_span!("HTTPS", local.addr = ?addr))
98
- } )
99
95
. map ( |https| warp:: serve ( routes) . serve_incoming ( https) )
100
96
. map ( tokio:: spawn) ;
101
97
102
98
let prom = self
103
99
. prom
104
- . map ( |prom| {
105
- //let addr = prom.as_ref().local_addr();
106
- prom
107
- //.instrument(debug_span!("PROM", local.addr = ?addr))
108
- } )
109
100
. map ( |prom| warp:: serve ( metrics ( ) ) . serve_incoming ( prom) )
110
101
. map ( tokio:: spawn) ;
111
102
0 commit comments