-
Notifications
You must be signed in to change notification settings - Fork 204
Expand file tree
/
Copy pathws_to_tunnel_task.rs
More file actions
114 lines (109 loc) · 3.13 KB
/
Copy pathws_to_tunnel_task.rs
File metadata and controls
114 lines (109 loc) · 3.13 KB
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
use anyhow::Result;
use futures_util::TryStreamExt;
use gas::prelude::*;
use rivet_envoy_protocol as protocol;
use rivet_guard_core::websocket_handle::WebSocketReceiver;
use std::sync::{
Arc,
atomic::{AtomicU64, Ordering},
};
use std::time::Duration;
use tokio::sync::{Mutex, watch};
use tokio_tungstenite::tungstenite::Message;
use super::LifecycleResult;
use crate::shared_state::{InFlightRequestHandle, display_id};
#[tracing::instrument(name = "ws_to_tunnel_task", skip_all)]
pub async fn task(
ctx: StandaloneCtx,
in_flight_req: InFlightRequestHandle,
ws_rx: Arc<Mutex<WebSocketReceiver>>,
ingress_bytes: Arc<AtomicU64>,
mut ws_to_tunnel_abort_rx: watch::Receiver<()>,
) -> Result<LifecycleResult> {
let mut ws_rx = ws_rx.lock().await;
// Leaky bucket rate limit on consuming ws messages
let mut rate_limit = rivet_util::throttle::RateLimiter::new(
rivet_util::throttle::RateLimitMethod::LeakyBucket {
requests: ctx
.config()
.pegboard()
.gateway_websocket_rate_limit_requests(),
drip_rate: Duration::from_millis(
ctx.config()
.pegboard()
.gateway_websocket_rate_limit_drip_rate_ms(),
),
},
);
loop {
tokio::select! {
res = async {
rate_limit.acquire().await;
ws_rx.try_next().await
} => {
if let Some(msg) = res? {
ingress_bytes.fetch_add(msg.len() as u64, Ordering::AcqRel);
match msg {
Message::Binary(data) => {
let data_len = data.len();
tracing::trace!(
request_id=%display_id(&in_flight_req.request_id),
data_len,
binary = true,
"received websocket message from client"
);
let ws_message =
protocol::ToEnvoyTunnelMessageKind::ToEnvoyWebSocketMessage(
protocol::ToEnvoyWebSocketMessage {
data: data.into(),
binary: true,
},
);
in_flight_req.send_message(ws_message, false).await?;
tracing::trace!(
request_id=%display_id(&in_flight_req.request_id),
data_len,
binary = true,
"sent websocket message toward envoy"
);
}
Message::Text(text) => {
let data_len = text.as_bytes().len();
tracing::trace!(
request_id=%display_id(&in_flight_req.request_id),
data_len,
binary = false,
"received websocket message from client"
);
let ws_message =
protocol::ToEnvoyTunnelMessageKind::ToEnvoyWebSocketMessage(
protocol::ToEnvoyWebSocketMessage {
data: text.as_bytes().to_vec(),
binary: false,
},
);
in_flight_req.send_message(ws_message, false).await?;
tracing::trace!(
request_id=%display_id(&in_flight_req.request_id),
data_len,
binary = false,
"sent websocket message toward envoy"
);
}
Message::Close(close) => {
return Ok(LifecycleResult::ClientClose(close));
}
_ => {}
}
} else {
tracing::debug!("websocket stream closed");
return Ok(LifecycleResult::ClientClose(None));
}
}
_ = ws_to_tunnel_abort_rx.changed() => {
tracing::debug!("task aborted");
return Ok(LifecycleResult::Aborted);
}
};
}
}