-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathbind.rs
202 lines (168 loc) · 4.78 KB
/
bind.rs
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
use std::marker::PhantomData;
use std::net::SocketAddr;
use std::sync::Arc;
use std::sync::atomic::AtomicUsize;
use std::time::Duration;
use http;
use tokio_core::reactor::Handle;
use tower;
use tower_h2;
use tower_reconnect::Reconnect;
use conduit_proxy_controller_grpc;
use control;
use ctx;
use telemetry::{self, sensor};
use transparency::{self, HttpBody};
use transport;
use ::timeout::Timeout;
const DEFAULT_TIMEOUT_MS: u64 = 300;
/// Binds a `Service` from a `SocketAddr`.
///
/// The returned `Service` buffers request until a connection is established.
///
/// # TODO
///
/// Buffering is not bounded and no timeouts are applied.
pub struct Bind<C, B> {
ctx: C,
sensors: telemetry::Sensors,
executor: Handle,
req_ids: Arc<AtomicUsize>,
connect_timeout: Duration,
_p: PhantomData<B>,
}
/// Binds a `Service` from a `SocketAddr` for a pre-determined protocol.
pub struct BindProtocol<C, B> {
bind: Bind<C, B>,
protocol: Protocol,
}
/// Mark whether to use HTTP/1 or HTTP/2
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Protocol {
Http1,
Http2
}
pub type Service<B> = Reconnect<NewHttp<B>>;
pub type NewHttp<B> = sensor::NewHttp<Client<B>, B, HttpBody>;
pub type HttpResponse = http::Response<sensor::http::ResponseBody<HttpBody>>;
pub type Client<B> = transparency::Client<
sensor::Connect<transport::TimeoutConnect<transport::Connect>>,
B,
>;
impl<B> Bind<(), B> {
pub fn new(executor: Handle) -> Self {
Self {
executor,
ctx: (),
sensors: telemetry::Sensors::null(),
req_ids: Default::default(),
connect_timeout: Duration::from_millis(DEFAULT_TIMEOUT_MS),
_p: PhantomData,
}
}
pub fn with_connect_timeout(self, connect_timeout: Duration) -> Self {
Self {
connect_timeout,
..self
}
}
pub fn with_sensors(self, sensors: telemetry::Sensors) -> Self {
Self {
sensors,
..self
}
}
pub fn with_ctx<C>(self, ctx: C) -> Bind<C, B> {
Bind {
ctx,
sensors: self.sensors,
executor: self.executor,
req_ids: self.req_ids,
connect_timeout: self.connect_timeout,
_p: PhantomData,
}
}
}
impl<C: Clone, B> Clone for Bind<C, B> {
fn clone(&self) -> Self {
Self {
ctx: self.ctx.clone(),
sensors: self.sensors.clone(),
executor: self.executor.clone(),
req_ids: self.req_ids.clone(),
connect_timeout: self.connect_timeout,
_p: PhantomData,
}
}
}
impl<C, B> Bind<C, B> {
pub fn connect_timeout(&self) -> Duration {
self.connect_timeout
}
// pub fn ctx(&self) -> &C {
// &self.ctx
// }
pub fn executor(&self) -> &Handle {
&self.executor
}
// pub fn req_ids(&self) -> &Arc<AtomicUsize> {
// &self.req_ids
// }
// pub fn sensors(&self) -> &telemetry::Sensors {
// &self.sensors
// }
}
impl<B> Bind<Arc<ctx::Proxy>, B>
where
B: tower_h2::Body + 'static,
{
pub fn bind_service(&self, addr: &SocketAddr, protocol: Protocol) -> Service<B> {
trace!("bind_service addr={}, protocol={:?}", addr, protocol);
let client_ctx = ctx::transport::Client::new(
&self.ctx,
addr,
conduit_proxy_controller_grpc::common::Protocol::Http,
);
// Map a socket address to a connection.
let connect = {
let c = Timeout::new(
transport::Connect::new(*addr, &self.executor),
self.connect_timeout,
&self.executor,
);
self.sensors.connect(c, &client_ctx)
};
let client = transparency::Client::new(
protocol,
connect,
self.executor.clone(),
);
let proxy = self.sensors.http(self.req_ids.clone(), client, &client_ctx);
// Automatically perform reconnects if the connection fails.
//
// TODO: Add some sort of backoff logic.
Reconnect::new(proxy)
}
}
// ===== impl BindProtocol =====
impl<C, B> Bind<C, B> {
pub fn with_protocol(self, protocol: Protocol) -> BindProtocol<C, B> {
BindProtocol {
bind: self,
protocol,
}
}
}
impl<B> control::discovery::Bind for BindProtocol<Arc<ctx::Proxy>, B>
where
B: tower_h2::Body + 'static,
{
type Request = http::Request<B>;
type Response = HttpResponse;
type Error = <Service<B> as tower::Service>::Error;
type Service = Service<B>;
type BindError = ();
fn bind(&self, addr: &SocketAddr) -> Result<Self::Service, Self::BindError> {
Ok(self.bind.bind_service(addr, self.protocol))
}
}