Line data Source code
1 : //! Routers for our serverless APIs
2 : //!
3 : //! Handles both SQL over HTTP and SQL over Websockets.
4 :
5 : mod backend;
6 : pub mod cancel_set;
7 : mod conn_pool;
8 : mod http_util;
9 : mod json;
10 : mod sql_over_http;
11 : mod websocket;
12 :
13 : use atomic_take::AtomicTake;
14 : use bytes::Bytes;
15 : pub use conn_pool::GlobalConnPoolOptions;
16 :
17 : use anyhow::Context;
18 : use futures::future::{select, Either};
19 : use futures::TryFutureExt;
20 : use http::{Method, Response, StatusCode};
21 : use http_body_util::Full;
22 : use hyper1::body::Incoming;
23 : use hyper_util::rt::TokioExecutor;
24 : use hyper_util::server::conn::auto::Builder;
25 : use rand::rngs::StdRng;
26 : use rand::SeedableRng;
27 : pub use reqwest_middleware::{ClientWithMiddleware, Error};
28 : pub use reqwest_retry::{policies::ExponentialBackoff, RetryTransientMiddleware};
29 : use tokio::time::timeout;
30 : use tokio_rustls::{server::TlsStream, TlsAcceptor};
31 : use tokio_util::task::TaskTracker;
32 :
33 : use crate::cancellation::CancellationHandlerMain;
34 : use crate::config::ProxyConfig;
35 : use crate::context::RequestMonitoring;
36 : use crate::metrics::Metrics;
37 : use crate::protocol2::{read_proxy_protocol, ChainRW};
38 : use crate::proxy::run_until_cancelled;
39 : use crate::rate_limiter::EndpointRateLimiter;
40 : use crate::serverless::backend::PoolingBackend;
41 : use crate::serverless::http_util::{api_error_into_response, json_response};
42 :
43 : use std::net::{IpAddr, SocketAddr};
44 : use std::pin::pin;
45 : use std::sync::Arc;
46 : use tokio::net::{TcpListener, TcpStream};
47 : use tokio_util::sync::CancellationToken;
48 : use tracing::{error, info, warn, Instrument};
49 : use utils::http::error::ApiError;
50 :
51 : pub const SERVERLESS_DRIVER_SNI: &str = "api";
52 :
53 0 : pub async fn task_main(
54 0 : config: &'static ProxyConfig,
55 0 : ws_listener: TcpListener,
56 0 : cancellation_token: CancellationToken,
57 0 : cancellation_handler: Arc<CancellationHandlerMain>,
58 0 : endpoint_rate_limiter: Arc<EndpointRateLimiter>,
59 0 : ) -> anyhow::Result<()> {
60 : scopeguard::defer! {
61 : info!("websocket server has shut down");
62 : }
63 :
64 0 : let conn_pool = conn_pool::GlobalConnPool::new(&config.http_config);
65 0 : {
66 0 : let conn_pool = Arc::clone(&conn_pool);
67 0 : tokio::spawn(async move {
68 0 : conn_pool.gc_worker(StdRng::from_entropy()).await;
69 0 : });
70 0 : }
71 0 :
72 0 : // shutdown the connection pool
73 0 : tokio::spawn({
74 0 : let cancellation_token = cancellation_token.clone();
75 0 : let conn_pool = conn_pool.clone();
76 0 : async move {
77 0 : cancellation_token.cancelled().await;
78 0 : tokio::task::spawn_blocking(move || conn_pool.shutdown())
79 0 : .await
80 0 : .unwrap();
81 0 : }
82 0 : });
83 0 :
84 0 : let backend = Arc::new(PoolingBackend {
85 0 : pool: Arc::clone(&conn_pool),
86 0 : config,
87 0 : endpoint_rate_limiter: Arc::clone(&endpoint_rate_limiter),
88 0 : });
89 :
90 0 : let tls_config = match config.tls_config.as_ref() {
91 0 : Some(config) => config,
92 : None => {
93 0 : warn!("TLS config is missing, WebSocket Secure server will not be started");
94 0 : return Ok(());
95 : }
96 : };
97 0 : let mut tls_server_config = rustls::ServerConfig::clone(&tls_config.to_server_config());
98 0 : // prefer http2, but support http/1.1
99 0 : tls_server_config.alpn_protocols = vec![b"h2".to_vec(), b"http/1.1".to_vec()];
100 0 : let tls_acceptor: tokio_rustls::TlsAcceptor = Arc::new(tls_server_config).into();
101 0 :
102 0 : let connections = tokio_util::task::task_tracker::TaskTracker::new();
103 0 : connections.close(); // allows `connections.wait to complete`
104 :
105 0 : while let Some(res) = run_until_cancelled(ws_listener.accept(), &cancellation_token).await {
106 0 : let (conn, peer_addr) = res.context("could not accept TCP stream")?;
107 0 : if let Err(e) = conn.set_nodelay(true) {
108 0 : tracing::error!("could not set nodelay: {e}");
109 0 : continue;
110 0 : }
111 0 : let conn_id = uuid::Uuid::new_v4();
112 0 : let http_conn_span = tracing::info_span!("http_conn", ?conn_id);
113 :
114 0 : let n_connections = Metrics::get()
115 0 : .proxy
116 0 : .client_connections
117 0 : .sample(crate::metrics::Protocol::Http);
118 0 : tracing::trace!(?n_connections, threshold = ?config.http_config.client_conn_threshold, "check");
119 0 : if n_connections > config.http_config.client_conn_threshold {
120 0 : tracing::trace!("attempting to cancel a random connection");
121 0 : if let Some(token) = config.http_config.cancel_set.take() {
122 0 : tracing::debug!("cancelling a random connection");
123 0 : token.cancel()
124 0 : }
125 0 : }
126 :
127 0 : let conn_token = cancellation_token.child_token();
128 0 : let tls_acceptor = tls_acceptor.clone();
129 0 : let backend = backend.clone();
130 0 : let connections2 = connections.clone();
131 0 : let cancellation_handler = cancellation_handler.clone();
132 0 : let endpoint_rate_limiter = endpoint_rate_limiter.clone();
133 0 : connections.spawn(
134 0 : async move {
135 0 : let conn_token2 = conn_token.clone();
136 0 : let _cancel_guard = config.http_config.cancel_set.insert(conn_id, conn_token2);
137 0 :
138 0 : let session_id = uuid::Uuid::new_v4();
139 0 :
140 0 : let _gauge = Metrics::get()
141 0 : .proxy
142 0 : .client_connections
143 0 : .guard(crate::metrics::Protocol::Http);
144 :
145 0 : let startup_result = Box::pin(connection_startup(
146 0 : config,
147 0 : tls_acceptor,
148 0 : session_id,
149 0 : conn,
150 0 : peer_addr,
151 0 : ))
152 0 : .await;
153 0 : let Some((conn, peer_addr)) = startup_result else {
154 0 : return;
155 : };
156 :
157 0 : Box::pin(connection_handler(
158 0 : config,
159 0 : backend,
160 0 : connections2,
161 0 : cancellation_handler,
162 0 : endpoint_rate_limiter,
163 0 : conn_token,
164 0 : conn,
165 0 : peer_addr,
166 0 : session_id,
167 0 : ))
168 0 : .await;
169 0 : }
170 0 : .instrument(http_conn_span),
171 0 : );
172 : }
173 :
174 0 : connections.wait().await;
175 :
176 0 : Ok(())
177 0 : }
178 :
179 : /// Handles the TCP startup lifecycle.
180 : /// 1. Parses PROXY protocol V2
181 : /// 2. Handles TLS handshake
182 0 : async fn connection_startup(
183 0 : config: &ProxyConfig,
184 0 : tls_acceptor: TlsAcceptor,
185 0 : session_id: uuid::Uuid,
186 0 : conn: TcpStream,
187 0 : peer_addr: SocketAddr,
188 0 : ) -> Option<(TlsStream<ChainRW<TcpStream>>, IpAddr)> {
189 : // handle PROXY protocol
190 0 : let (conn, peer) = match read_proxy_protocol(conn).await {
191 0 : Ok(c) => c,
192 0 : Err(e) => {
193 0 : tracing::error!(?session_id, %peer_addr, "failed to accept TCP connection: invalid PROXY protocol V2 header: {e:#}");
194 0 : return None;
195 : }
196 : };
197 :
198 0 : let peer_addr = peer.unwrap_or(peer_addr).ip();
199 0 : let has_private_peer_addr = match peer_addr {
200 0 : IpAddr::V4(ip) => ip.is_private(),
201 0 : _ => false,
202 : };
203 0 : info!(?session_id, %peer_addr, "accepted new TCP connection");
204 :
205 : // try upgrade to TLS, but with a timeout.
206 0 : let conn = match timeout(config.handshake_timeout, tls_acceptor.accept(conn)).await {
207 0 : Ok(Ok(conn)) => {
208 0 : info!(?session_id, %peer_addr, "accepted new TLS connection");
209 0 : conn
210 : }
211 : // The handshake failed
212 0 : Ok(Err(e)) => {
213 0 : if !has_private_peer_addr {
214 0 : Metrics::get().proxy.tls_handshake_failures.inc();
215 0 : }
216 0 : warn!(?session_id, %peer_addr, "failed to accept TLS connection: {e:?}");
217 0 : return None;
218 : }
219 : // The handshake timed out
220 0 : Err(e) => {
221 0 : if !has_private_peer_addr {
222 0 : Metrics::get().proxy.tls_handshake_failures.inc();
223 0 : }
224 0 : warn!(?session_id, %peer_addr, "failed to accept TLS connection: {e:?}");
225 0 : return None;
226 : }
227 : };
228 :
229 0 : Some((conn, peer_addr))
230 0 : }
231 :
232 : /// Handles HTTP connection
233 : /// 1. With graceful shutdowns
234 : /// 2. With graceful request cancellation with connection failure
235 : /// 3. With websocket upgrade support.
236 : #[allow(clippy::too_many_arguments)]
237 0 : async fn connection_handler(
238 0 : config: &'static ProxyConfig,
239 0 : backend: Arc<PoolingBackend>,
240 0 : connections: TaskTracker,
241 0 : cancellation_handler: Arc<CancellationHandlerMain>,
242 0 : endpoint_rate_limiter: Arc<EndpointRateLimiter>,
243 0 : cancellation_token: CancellationToken,
244 0 : conn: TlsStream<ChainRW<TcpStream>>,
245 0 : peer_addr: IpAddr,
246 0 : session_id: uuid::Uuid,
247 0 : ) {
248 0 : let session_id = AtomicTake::new(session_id);
249 0 :
250 0 : // Cancel all current inflight HTTP requests if the HTTP connection is closed.
251 0 : let http_cancellation_token = CancellationToken::new();
252 0 : let _cancel_connection = http_cancellation_token.clone().drop_guard();
253 0 :
254 0 : let server = Builder::new(TokioExecutor::new());
255 0 : let conn = server.serve_connection_with_upgrades(
256 0 : hyper_util::rt::TokioIo::new(conn),
257 0 : hyper1::service::service_fn(move |req: hyper1::Request<Incoming>| {
258 0 : // First HTTP request shares the same session ID
259 0 : let session_id = session_id.take().unwrap_or_else(uuid::Uuid::new_v4);
260 0 :
261 0 : // Cancel the current inflight HTTP request if the requets stream is closed.
262 0 : // This is slightly different to `_cancel_connection` in that
263 0 : // h2 can cancel individual requests with a `RST_STREAM`.
264 0 : let http_request_token = http_cancellation_token.child_token();
265 0 : let cancel_request = http_request_token.clone().drop_guard();
266 0 :
267 0 : // `request_handler` is not cancel safe. It expects to be cancelled only at specific times.
268 0 : // By spawning the future, we ensure it never gets cancelled until it decides to.
269 0 : let handler = connections.spawn(
270 0 : request_handler(
271 0 : req,
272 0 : config,
273 0 : backend.clone(),
274 0 : connections.clone(),
275 0 : cancellation_handler.clone(),
276 0 : session_id,
277 0 : peer_addr,
278 0 : http_request_token,
279 0 : endpoint_rate_limiter.clone(),
280 0 : )
281 0 : .in_current_span()
282 0 : .map_ok_or_else(api_error_into_response, |r| r),
283 0 : );
284 0 : async move {
285 0 : let res = handler.await;
286 0 : cancel_request.disarm();
287 0 : res
288 0 : }
289 0 : }),
290 0 : );
291 :
292 : // On cancellation, trigger the HTTP connection handler to shut down.
293 0 : let res = match select(pin!(cancellation_token.cancelled()), pin!(conn)).await {
294 0 : Either::Left((_cancelled, mut conn)) => {
295 0 : tracing::debug!(%peer_addr, "cancelling connection");
296 0 : conn.as_mut().graceful_shutdown();
297 0 : conn.await
298 : }
299 0 : Either::Right((res, _)) => res,
300 : };
301 :
302 0 : match res {
303 0 : Ok(()) => tracing::info!(%peer_addr, "HTTP connection closed"),
304 0 : Err(e) => tracing::warn!(%peer_addr, "HTTP connection error {e}"),
305 : }
306 0 : }
307 :
308 : #[allow(clippy::too_many_arguments)]
309 0 : async fn request_handler(
310 0 : mut request: hyper1::Request<Incoming>,
311 0 : config: &'static ProxyConfig,
312 0 : backend: Arc<PoolingBackend>,
313 0 : ws_connections: TaskTracker,
314 0 : cancellation_handler: Arc<CancellationHandlerMain>,
315 0 : session_id: uuid::Uuid,
316 0 : peer_addr: IpAddr,
317 0 : // used to cancel in-flight HTTP requests. not used to cancel websockets
318 0 : http_cancellation_token: CancellationToken,
319 0 : endpoint_rate_limiter: Arc<EndpointRateLimiter>,
320 0 : ) -> Result<Response<Full<Bytes>>, ApiError> {
321 0 : let host = request
322 0 : .headers()
323 0 : .get("host")
324 0 : .and_then(|h| h.to_str().ok())
325 0 : .and_then(|h| h.split(':').next())
326 0 : .map(|s| s.to_string());
327 0 :
328 0 : // Check if the request is a websocket upgrade request.
329 0 : if framed_websockets::upgrade::is_upgrade_request(&request) {
330 0 : let ctx = RequestMonitoring::new(
331 0 : session_id,
332 0 : peer_addr,
333 0 : crate::metrics::Protocol::Ws,
334 0 : &config.region,
335 0 : );
336 0 :
337 0 : let span = ctx.span.clone();
338 : info!(parent: &span, "performing websocket upgrade");
339 :
340 0 : let (response, websocket) = framed_websockets::upgrade::upgrade(&mut request)
341 0 : .map_err(|e| ApiError::BadRequest(e.into()))?;
342 :
343 0 : ws_connections.spawn(
344 0 : async move {
345 0 : if let Err(e) = websocket::serve_websocket(
346 0 : config,
347 0 : ctx,
348 0 : websocket,
349 0 : cancellation_handler,
350 0 : endpoint_rate_limiter,
351 0 : host,
352 0 : )
353 0 : .await
354 : {
355 0 : error!("error in websocket connection: {e:#}");
356 0 : }
357 0 : }
358 0 : .instrument(span),
359 0 : );
360 0 :
361 0 : // Return the response so the spawned future can continue.
362 0 : Ok(response.map(|_: http_body_util::Empty<Bytes>| Full::new(Bytes::new())))
363 0 : } else if request.uri().path() == "/sql" && *request.method() == Method::POST {
364 0 : let ctx = RequestMonitoring::new(
365 0 : session_id,
366 0 : peer_addr,
367 0 : crate::metrics::Protocol::Http,
368 0 : &config.region,
369 0 : );
370 0 : let span = ctx.span.clone();
371 0 :
372 0 : sql_over_http::handle(config, ctx, request, backend, http_cancellation_token)
373 0 : .instrument(span)
374 0 : .await
375 0 : } else if request.uri().path() == "/sql" && *request.method() == Method::OPTIONS {
376 0 : Response::builder()
377 0 : .header("Allow", "OPTIONS, POST")
378 0 : .header("Access-Control-Allow-Origin", "*")
379 0 : .header(
380 0 : "Access-Control-Allow-Headers",
381 0 : "Neon-Connection-String, Neon-Raw-Text-Output, Neon-Array-Mode, Neon-Pool-Opt-In, Neon-Batch-Read-Only, Neon-Batch-Isolation-Level",
382 0 : )
383 0 : .header("Access-Control-Max-Age", "86400" /* 24 hours */)
384 0 : .status(StatusCode::OK) // 204 is also valid, but see: https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/OPTIONS#status_code
385 0 : .body(Full::new(Bytes::new()))
386 0 : .map_err(|e| ApiError::InternalServerError(e.into()))
387 : } else {
388 0 : json_response(StatusCode::BAD_REQUEST, "query is not supported")
389 : }
390 0 : }
|