2024-03-19 15:17:38 +00:00
|
|
|
use crate::cnf::{
|
|
|
|
PKG_NAME, PKG_VERSION, WEBSOCKET_MAX_CONCURRENT_REQUESTS, WEBSOCKET_PING_FREQUENCY,
|
|
|
|
};
|
2023-11-20 19:08:07 +00:00
|
|
|
use crate::dbs::DB;
|
2024-01-09 15:27:03 +00:00
|
|
|
use crate::rpc::failure::Failure;
|
2024-04-17 18:56:08 +00:00
|
|
|
use crate::rpc::format::WsFormat;
|
2024-03-19 15:17:38 +00:00
|
|
|
use crate::rpc::response::{failure, IntoRpcResponse};
|
2024-07-05 09:34:43 +00:00
|
|
|
use crate::rpc::CONN_CLOSED_ERR;
|
2023-11-20 19:08:07 +00:00
|
|
|
use crate::telemetry;
|
|
|
|
use crate::telemetry::metrics::ws::RequestContext;
|
|
|
|
use crate::telemetry::traces::rpc::span_for_request;
|
2023-08-16 12:27:53 +00:00
|
|
|
use axum::extract::ws::{Message, WebSocket};
|
|
|
|
use futures_util::stream::{SplitSink, SplitStream};
|
|
|
|
use futures_util::{SinkExt, StreamExt};
|
|
|
|
use opentelemetry::trace::FutureExt;
|
|
|
|
use opentelemetry::Context as TelemetryContext;
|
|
|
|
use std::collections::BTreeMap;
|
|
|
|
use std::sync::Arc;
|
|
|
|
use surrealdb::channel::{self, Receiver, Sender};
|
|
|
|
use surrealdb::dbs::Session;
|
2024-03-19 15:17:38 +00:00
|
|
|
use surrealdb::kvs::Datastore;
|
|
|
|
use surrealdb::rpc::args::Take;
|
2024-04-17 18:56:08 +00:00
|
|
|
use surrealdb::rpc::format::Format;
|
2024-03-19 15:17:38 +00:00
|
|
|
use surrealdb::rpc::method::Method;
|
|
|
|
use surrealdb::rpc::RpcContext;
|
|
|
|
use surrealdb::rpc::{Data, RpcError};
|
2023-11-20 19:08:07 +00:00
|
|
|
use surrealdb::sql::Array;
|
|
|
|
use surrealdb::sql::Value;
|
|
|
|
use tokio::sync::{RwLock, Semaphore};
|
2023-08-16 12:27:53 +00:00
|
|
|
use tokio::task::JoinSet;
|
|
|
|
use tokio_util::sync::CancellationToken;
|
2023-11-20 19:08:07 +00:00
|
|
|
use tracing::Instrument;
|
|
|
|
use tracing::Span;
|
2023-08-16 12:27:53 +00:00
|
|
|
use uuid::Uuid;
|
|
|
|
|
2024-07-05 09:34:43 +00:00
|
|
|
use super::RpcState;
|
|
|
|
|
2023-08-16 12:27:53 +00:00
|
|
|
pub struct Connection {
|
2024-01-15 19:38:30 +00:00
|
|
|
pub(crate) id: Uuid,
|
|
|
|
pub(crate) format: Format,
|
|
|
|
pub(crate) session: Session,
|
|
|
|
pub(crate) vars: BTreeMap<String, Value>,
|
|
|
|
pub(crate) limiter: Arc<Semaphore>,
|
|
|
|
pub(crate) canceller: CancellationToken,
|
|
|
|
pub(crate) channels: (Sender<Message>, Receiver<Message>),
|
2024-07-05 09:34:43 +00:00
|
|
|
pub(crate) state: Arc<RpcState>,
|
2023-08-16 12:27:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Connection {
|
|
|
|
/// Instantiate a new RPC
|
2024-07-05 09:34:43 +00:00
|
|
|
pub fn new(
|
|
|
|
state: Arc<RpcState>,
|
|
|
|
id: Uuid,
|
|
|
|
mut session: Session,
|
|
|
|
format: Format,
|
|
|
|
) -> Arc<RwLock<Connection>> {
|
2023-08-16 12:27:53 +00:00
|
|
|
// Enable real-time mode
|
|
|
|
session.rt = true;
|
|
|
|
// Create and store the RPC connection
|
|
|
|
Arc::new(RwLock::new(Connection {
|
2024-01-09 15:27:03 +00:00
|
|
|
id,
|
2023-11-20 19:08:07 +00:00
|
|
|
format,
|
2024-01-15 19:38:30 +00:00
|
|
|
session,
|
2024-01-09 15:27:03 +00:00
|
|
|
vars: BTreeMap::new(),
|
2023-11-20 19:08:07 +00:00
|
|
|
limiter: Arc::new(Semaphore::new(*WEBSOCKET_MAX_CONCURRENT_REQUESTS)),
|
|
|
|
canceller: CancellationToken::new(),
|
2024-01-15 19:38:30 +00:00
|
|
|
channels: channel::bounded(*WEBSOCKET_MAX_CONCURRENT_REQUESTS),
|
2024-07-05 09:34:43 +00:00
|
|
|
state,
|
2023-08-16 12:27:53 +00:00
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Serve the RPC endpoint
|
|
|
|
pub async fn serve(rpc: Arc<RwLock<Connection>>, ws: WebSocket) {
|
2024-01-15 19:38:30 +00:00
|
|
|
// Get the WebSocket ID
|
2024-07-05 09:34:43 +00:00
|
|
|
let rpc_lock = rpc.read().await;
|
|
|
|
// Get the WebSocket ID
|
|
|
|
let id = rpc_lock.id;
|
|
|
|
let state = rpc_lock.state.clone();
|
|
|
|
|
2024-01-15 19:38:30 +00:00
|
|
|
// Split the socket into sending and receiving streams
|
|
|
|
let (sender, receiver) = ws.split();
|
|
|
|
// Create an internal channel for sending and receiving
|
2024-07-05 09:34:43 +00:00
|
|
|
let internal_sender = rpc_lock.channels.0.clone();
|
|
|
|
let internal_receiver = rpc_lock.channels.1.clone();
|
|
|
|
|
|
|
|
// drop the lock early so rpc is free to be written to.
|
|
|
|
std::mem::drop(rpc_lock);
|
2023-08-16 12:27:53 +00:00
|
|
|
|
2024-01-09 15:27:03 +00:00
|
|
|
trace!("WebSocket {} connected", id);
|
2023-08-16 12:27:53 +00:00
|
|
|
|
|
|
|
if let Err(err) = telemetry::metrics::ws::on_connect() {
|
|
|
|
error!("Error running metrics::ws::on_connect hook: {}", err);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add this WebSocket to the list
|
2024-07-05 09:34:43 +00:00
|
|
|
state.web_sockets.write().await.insert(id, rpc.clone());
|
2023-08-19 09:01:37 +00:00
|
|
|
|
2023-09-12 09:38:28 +00:00
|
|
|
// Spawn async tasks for the WebSocket
|
2023-08-16 12:27:53 +00:00
|
|
|
let mut tasks = JoinSet::new();
|
|
|
|
tasks.spawn(Self::ping(rpc.clone(), internal_sender.clone()));
|
|
|
|
tasks.spawn(Self::read(rpc.clone(), receiver, internal_sender.clone()));
|
|
|
|
tasks.spawn(Self::write(rpc.clone(), sender, internal_receiver.clone()));
|
|
|
|
|
|
|
|
// Wait until all tasks finish
|
|
|
|
while let Some(res) = tasks.join_next().await {
|
|
|
|
if let Err(err) = res {
|
|
|
|
error!("Error handling RPC connection: {}", err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-20 19:08:07 +00:00
|
|
|
internal_sender.close();
|
|
|
|
|
2024-01-09 15:27:03 +00:00
|
|
|
trace!("WebSocket {} disconnected", id);
|
2023-09-12 09:38:28 +00:00
|
|
|
|
2023-08-16 12:27:53 +00:00
|
|
|
// Remove this WebSocket from the list
|
2024-07-05 09:34:43 +00:00
|
|
|
state.web_sockets.write().await.remove(&id);
|
2023-08-16 12:27:53 +00:00
|
|
|
|
2023-09-12 09:38:28 +00:00
|
|
|
// Remove all live queries
|
|
|
|
let mut gc = Vec::new();
|
2024-07-05 09:34:43 +00:00
|
|
|
state.live_queries.write().await.retain(|key, value| {
|
2024-01-09 15:27:03 +00:00
|
|
|
if value == &id {
|
2023-09-12 09:38:28 +00:00
|
|
|
trace!("Removing live query: {}", key);
|
|
|
|
gc.push(*key);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
true
|
|
|
|
});
|
|
|
|
|
|
|
|
// Garbage collect queries
|
|
|
|
if let Err(e) = DB.get().unwrap().garbage_collect_dead_session(gc.as_slice()).await {
|
|
|
|
error!("Failed to garbage collect dead sessions: {:?}", e);
|
|
|
|
}
|
2023-08-16 12:27:53 +00:00
|
|
|
|
|
|
|
if let Err(err) = telemetry::metrics::ws::on_disconnect() {
|
|
|
|
error!("Error running metrics::ws::on_disconnect hook: {}", err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Send Ping messages to the client
|
|
|
|
async fn ping(rpc: Arc<RwLock<Connection>>, internal_sender: Sender<Message>) {
|
|
|
|
// Create the interval ticker
|
|
|
|
let mut interval = tokio::time::interval(WEBSOCKET_PING_FREQUENCY);
|
2023-11-20 19:08:07 +00:00
|
|
|
// Clone the WebSocket cancellation token
|
|
|
|
let canceller = rpc.read().await.canceller.clone();
|
|
|
|
// Loop, and listen for messages to write
|
2023-08-16 12:27:53 +00:00
|
|
|
loop {
|
|
|
|
tokio::select! {
|
2023-11-20 19:08:07 +00:00
|
|
|
//
|
|
|
|
biased;
|
|
|
|
// Check if this has shutdown
|
|
|
|
_ = canceller.cancelled() => break,
|
|
|
|
// Send a regular ping message
|
2023-08-16 12:27:53 +00:00
|
|
|
_ = interval.tick() => {
|
2023-11-20 19:08:07 +00:00
|
|
|
// Create a new ping message
|
2023-08-16 12:27:53 +00:00
|
|
|
let msg = Message::Ping(vec![]);
|
2023-11-20 19:08:07 +00:00
|
|
|
// Close the connection if the message fails
|
2023-08-16 12:27:53 +00:00
|
|
|
if internal_sender.send(msg).await.is_err() {
|
2023-11-20 19:08:07 +00:00
|
|
|
// Cancel the WebSocket tasks
|
|
|
|
rpc.read().await.canceller.cancel();
|
|
|
|
// Exit out of the loop
|
2023-08-16 12:27:53 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
},
|
2023-11-20 19:08:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Write messages to the client
|
|
|
|
async fn write(
|
|
|
|
rpc: Arc<RwLock<Connection>>,
|
|
|
|
mut sender: SplitSink<WebSocket, Message>,
|
|
|
|
mut internal_receiver: Receiver<Message>,
|
|
|
|
) {
|
|
|
|
// Clone the WebSocket cancellation token
|
|
|
|
let canceller = rpc.read().await.canceller.clone();
|
|
|
|
// Loop, and listen for messages to write
|
|
|
|
loop {
|
|
|
|
tokio::select! {
|
|
|
|
//
|
|
|
|
biased;
|
|
|
|
// Check if this has shutdown
|
|
|
|
_ = canceller.cancelled() => break,
|
|
|
|
// Wait for the next message to send
|
2023-12-05 09:28:29 +00:00
|
|
|
Some(res) = internal_receiver.next() => {
|
|
|
|
// Send the message to the client
|
|
|
|
if let Err(err) = sender.send(res).await {
|
|
|
|
// Output any errors if not a close error
|
|
|
|
if err.to_string() != CONN_CLOSED_ERR {
|
|
|
|
debug!("WebSocket error: {:?}", err);
|
2023-11-20 19:08:07 +00:00
|
|
|
}
|
2023-12-05 09:28:29 +00:00
|
|
|
// Cancel the WebSocket tasks
|
|
|
|
rpc.read().await.canceller.cancel();
|
|
|
|
// Exit out of the loop
|
|
|
|
break;
|
2023-11-20 19:08:07 +00:00
|
|
|
}
|
|
|
|
},
|
2023-08-16 12:27:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Read messages sent from the client
|
|
|
|
async fn read(
|
|
|
|
rpc: Arc<RwLock<Connection>>,
|
|
|
|
mut receiver: SplitStream<WebSocket>,
|
|
|
|
internal_sender: Sender<Message>,
|
|
|
|
) {
|
2023-11-20 19:08:07 +00:00
|
|
|
// Store spawned tasks so we can wait for them
|
2023-08-16 12:27:53 +00:00
|
|
|
let mut tasks = JoinSet::new();
|
2023-11-20 19:08:07 +00:00
|
|
|
// Clone the WebSocket cancellation token
|
|
|
|
let canceller = rpc.read().await.canceller.clone();
|
|
|
|
// Loop, and listen for messages to write
|
2023-08-16 12:27:53 +00:00
|
|
|
loop {
|
|
|
|
tokio::select! {
|
2023-11-20 19:08:07 +00:00
|
|
|
//
|
|
|
|
biased;
|
|
|
|
// Check if this has shutdown
|
|
|
|
_ = canceller.cancelled() => break,
|
2023-12-05 09:28:29 +00:00
|
|
|
// Remove any completed tasks
|
|
|
|
Some(out) = tasks.join_next() => match out {
|
|
|
|
// The task completed successfully
|
|
|
|
Ok(_) => continue,
|
|
|
|
// There was an uncaught panic in the task
|
|
|
|
Err(err) => {
|
|
|
|
// There was an error with the task
|
|
|
|
trace!("WebSocket request error: {:?}", err);
|
|
|
|
// Cancel the WebSocket tasks
|
|
|
|
rpc.read().await.canceller.cancel();
|
|
|
|
// Exit out of the loop
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
// Wait for the next received message
|
|
|
|
Some(msg) = receiver.next() => match msg {
|
|
|
|
// We've received a message from the client
|
|
|
|
Ok(msg) => match msg {
|
|
|
|
Message::Text(_) => {
|
|
|
|
tasks.spawn(Connection::handle_message(rpc.clone(), msg, internal_sender.clone()));
|
|
|
|
}
|
|
|
|
Message::Binary(_) => {
|
|
|
|
tasks.spawn(Connection::handle_message(rpc.clone(), msg, internal_sender.clone()));
|
|
|
|
}
|
|
|
|
Message::Close(_) => {
|
|
|
|
// Respond with a close message
|
|
|
|
if let Err(err) = internal_sender.send(Message::Close(None)).await {
|
|
|
|
trace!("WebSocket error when replying to the Close frame: {:?}", err);
|
|
|
|
};
|
|
|
|
// Cancel the WebSocket tasks
|
|
|
|
rpc.read().await.canceller.cancel();
|
|
|
|
// Exit out of the loop
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
// Ignore everything else
|
2023-08-16 12:27:53 +00:00
|
|
|
}
|
2023-12-05 09:28:29 +00:00
|
|
|
},
|
|
|
|
Err(err) => {
|
|
|
|
// There was an error with the WebSocket
|
|
|
|
trace!("WebSocket error: {:?}", err);
|
|
|
|
// Cancel the WebSocket tasks
|
|
|
|
rpc.read().await.canceller.cancel();
|
|
|
|
// Exit out of the loop
|
|
|
|
break;
|
2023-08-16 12:27:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Wait for all tasks to finish
|
|
|
|
while let Some(res) = tasks.join_next().await {
|
|
|
|
if let Err(err) = res {
|
2023-12-05 09:28:29 +00:00
|
|
|
// There was an error with the task
|
|
|
|
trace!("WebSocket request error: {:?}", err);
|
2023-08-16 12:27:53 +00:00
|
|
|
}
|
|
|
|
}
|
2023-12-05 09:28:29 +00:00
|
|
|
// Abort all tasks
|
|
|
|
tasks.shutdown().await;
|
2023-08-16 12:27:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Handle individual WebSocket messages
|
2023-11-20 19:08:07 +00:00
|
|
|
async fn handle_message(rpc: Arc<RwLock<Connection>>, msg: Message, chn: Sender<Message>) {
|
2023-08-16 12:27:53 +00:00
|
|
|
// Get the current output format
|
2024-01-09 15:27:03 +00:00
|
|
|
let mut fmt = rpc.read().await.format;
|
2023-08-16 12:27:53 +00:00
|
|
|
// Prepare Span and Otel context
|
2024-01-09 15:27:03 +00:00
|
|
|
let span = span_for_request(&rpc.read().await.id);
|
2023-11-20 19:08:07 +00:00
|
|
|
// Acquire concurrent request rate limiter
|
|
|
|
let permit = rpc.read().await.limiter.clone().acquire_owned().await.unwrap();
|
2024-01-09 15:27:03 +00:00
|
|
|
// Calculate the length of the message
|
|
|
|
let len = match msg {
|
|
|
|
Message::Text(ref msg) => {
|
|
|
|
// If no format was specified, default to JSON
|
|
|
|
if fmt.is_none() {
|
|
|
|
fmt = Format::Json;
|
|
|
|
rpc.write().await.format = fmt;
|
|
|
|
}
|
|
|
|
// Retrieve the length of the message
|
|
|
|
msg.len()
|
|
|
|
}
|
|
|
|
Message::Binary(ref msg) => {
|
|
|
|
// If no format was specified, default to Bincode
|
|
|
|
if fmt.is_none() {
|
|
|
|
fmt = Format::Bincode;
|
|
|
|
rpc.write().await.format = fmt;
|
|
|
|
}
|
|
|
|
// Retrieve the length of the message
|
|
|
|
msg.len()
|
|
|
|
}
|
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
2023-08-16 12:27:53 +00:00
|
|
|
// Parse the request
|
2023-08-29 12:04:53 +00:00
|
|
|
async move {
|
|
|
|
let span = Span::current();
|
|
|
|
let req_cx = RequestContext::default();
|
2024-02-12 11:54:38 +00:00
|
|
|
let otel_cx = Arc::new(TelemetryContext::new().with_value(req_cx.clone()));
|
2024-01-09 15:27:03 +00:00
|
|
|
// Parse the RPC request structure
|
2024-03-19 15:17:38 +00:00
|
|
|
match fmt.req_ws(msg) {
|
2023-08-29 12:04:53 +00:00
|
|
|
Ok(req) => {
|
|
|
|
// Now that we know the method, we can update the span and create otel context
|
|
|
|
span.record("rpc.method", &req.method);
|
|
|
|
span.record("otel.name", format!("surrealdb.rpc/{}", req.method));
|
|
|
|
span.record(
|
2023-11-20 19:08:07 +00:00
|
|
|
"rpc.request_id",
|
|
|
|
req.id.clone().map(Value::as_string).unwrap_or_default(),
|
2023-08-29 12:04:53 +00:00
|
|
|
);
|
2024-02-12 11:54:38 +00:00
|
|
|
let otel_cx = Arc::new(TelemetryContext::current_with_value(
|
2024-01-09 15:27:03 +00:00
|
|
|
req_cx.with_method(&req.method).with_size(len),
|
2024-02-12 11:54:38 +00:00
|
|
|
));
|
2023-11-20 19:08:07 +00:00
|
|
|
// Process the message
|
2023-08-29 12:04:53 +00:00
|
|
|
let res =
|
2023-11-20 19:08:07 +00:00
|
|
|
Connection::process_message(rpc.clone(), &req.method, req.params).await;
|
2023-08-29 12:04:53 +00:00
|
|
|
// Process the response
|
2024-02-12 11:54:38 +00:00
|
|
|
res.into_response(req.id)
|
|
|
|
.send(otel_cx.clone(), fmt, &chn)
|
|
|
|
.with_context(otel_cx.as_ref().clone())
|
|
|
|
.await
|
2023-08-29 12:04:53 +00:00
|
|
|
}
|
|
|
|
Err(err) => {
|
|
|
|
// Process the response
|
2024-02-12 11:54:38 +00:00
|
|
|
failure(None, err)
|
|
|
|
.send(otel_cx.clone(), fmt, &chn)
|
|
|
|
.with_context(otel_cx.as_ref().clone())
|
|
|
|
.await
|
2023-08-29 12:04:53 +00:00
|
|
|
}
|
2023-08-16 12:27:53 +00:00
|
|
|
}
|
|
|
|
}
|
2023-08-29 12:04:53 +00:00
|
|
|
.instrument(span)
|
|
|
|
.await;
|
2023-11-20 19:08:07 +00:00
|
|
|
// Drop the rate limiter permit
|
|
|
|
drop(permit);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn process_message(
|
|
|
|
rpc: Arc<RwLock<Connection>>,
|
|
|
|
method: &str,
|
|
|
|
params: Array,
|
|
|
|
) -> Result<Data, Failure> {
|
|
|
|
debug!("Process RPC request");
|
2024-03-19 15:17:38 +00:00
|
|
|
let method = Method::parse(method);
|
|
|
|
if !method.is_valid() {
|
|
|
|
return Err(Failure::METHOD_NOT_FOUND);
|
2023-11-20 19:08:07 +00:00
|
|
|
}
|
|
|
|
|
2024-03-19 15:17:38 +00:00
|
|
|
// if the write lock is a bottleneck then execute could be refactored into execute_mut and execute
|
|
|
|
// rpc.write().await.execute(method, params).await.map_err(Into::into)
|
|
|
|
match method.needs_mut() {
|
|
|
|
true => rpc.write().await.execute(method, params).await.map_err(Into::into),
|
|
|
|
false => rpc.read().await.execute_immut(method, params).await.map_err(Into::into),
|
2023-11-20 19:08:07 +00:00
|
|
|
}
|
|
|
|
}
|
2024-03-19 15:17:38 +00:00
|
|
|
}
|
2023-11-20 19:08:07 +00:00
|
|
|
|
2024-03-19 15:17:38 +00:00
|
|
|
impl RpcContext for Connection {
|
|
|
|
fn kvs(&self) -> &Datastore {
|
|
|
|
DB.get().unwrap()
|
2023-11-20 19:08:07 +00:00
|
|
|
}
|
|
|
|
|
2024-03-19 15:17:38 +00:00
|
|
|
fn session(&self) -> &Session {
|
|
|
|
&self.session
|
2023-11-20 19:08:07 +00:00
|
|
|
}
|
|
|
|
|
2024-03-19 15:17:38 +00:00
|
|
|
fn session_mut(&mut self) -> &mut Session {
|
|
|
|
&mut self.session
|
2023-11-20 19:08:07 +00:00
|
|
|
}
|
|
|
|
|
2024-03-19 15:17:38 +00:00
|
|
|
fn vars(&self) -> &BTreeMap<String, Value> {
|
|
|
|
&self.vars
|
2023-11-20 19:08:07 +00:00
|
|
|
}
|
|
|
|
|
2024-03-19 15:17:38 +00:00
|
|
|
fn vars_mut(&mut self) -> &mut BTreeMap<String, Value> {
|
|
|
|
&mut self.vars
|
2023-11-20 19:08:07 +00:00
|
|
|
}
|
|
|
|
|
2024-03-19 15:17:38 +00:00
|
|
|
fn version_data(&self) -> impl Into<Data> {
|
|
|
|
format!("{PKG_NAME}-{}", *PKG_VERSION)
|
2023-11-20 19:08:07 +00:00
|
|
|
}
|
|
|
|
|
2024-03-19 15:17:38 +00:00
|
|
|
const LQ_SUPPORT: bool = true;
|
2023-11-20 19:08:07 +00:00
|
|
|
|
2024-03-19 15:17:38 +00:00
|
|
|
async fn handle_live(&self, lqid: &Uuid) {
|
2024-07-05 09:34:43 +00:00
|
|
|
self.state.live_queries.write().await.insert(*lqid, self.id);
|
2024-03-19 15:17:38 +00:00
|
|
|
trace!("Registered live query {} on websocket {}", lqid, self.id);
|
2023-11-20 19:08:07 +00:00
|
|
|
}
|
|
|
|
|
2024-03-19 15:17:38 +00:00
|
|
|
async fn handle_kill(&self, lqid: &Uuid) {
|
2024-07-05 09:34:43 +00:00
|
|
|
if let Some(id) = self.state.live_queries.write().await.remove(lqid) {
|
2024-03-19 15:17:38 +00:00
|
|
|
trace!("Unregistered live query {} on websocket {}", lqid, id);
|
2023-11-20 19:08:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-19 15:17:38 +00:00
|
|
|
// reimplimentaions
|
2023-11-20 19:08:07 +00:00
|
|
|
|
2024-03-19 15:17:38 +00:00
|
|
|
async fn signup(&mut self, params: Array) -> Result<impl Into<Data>, RpcError> {
|
|
|
|
let Ok(Value::Object(v)) = params.needs_one() else {
|
|
|
|
return Err(RpcError::InvalidParams);
|
2023-11-20 19:08:07 +00:00
|
|
|
};
|
2024-03-19 15:17:38 +00:00
|
|
|
let out: Result<Value, RpcError> =
|
|
|
|
surrealdb::iam::signup::signup(DB.get().unwrap(), &mut self.session, v)
|
|
|
|
.await
|
|
|
|
.map(Into::into)
|
|
|
|
.map_err(Into::into);
|
2023-11-20 19:08:07 +00:00
|
|
|
|
2024-03-19 15:17:38 +00:00
|
|
|
out
|
2023-11-20 19:08:07 +00:00
|
|
|
}
|
|
|
|
|
2024-03-19 15:17:38 +00:00
|
|
|
async fn signin(&mut self, params: Array) -> Result<impl Into<Data>, RpcError> {
|
|
|
|
let Ok(Value::Object(v)) = params.needs_one() else {
|
|
|
|
return Err(RpcError::InvalidParams);
|
2023-11-20 19:08:07 +00:00
|
|
|
};
|
2024-03-19 15:17:38 +00:00
|
|
|
let out: Result<Value, RpcError> =
|
|
|
|
surrealdb::iam::signin::signin(DB.get().unwrap(), &mut self.session, v)
|
|
|
|
.await
|
|
|
|
.map(Into::into)
|
|
|
|
.map_err(Into::into);
|
|
|
|
out
|
2023-11-20 19:08:07 +00:00
|
|
|
}
|
|
|
|
|
2024-03-19 15:17:38 +00:00
|
|
|
async fn authenticate(&mut self, params: Array) -> Result<impl Into<Data>, RpcError> {
|
|
|
|
let Ok(Value::Strand(token)) = params.needs_one() else {
|
|
|
|
return Err(RpcError::InvalidParams);
|
2023-11-20 19:08:07 +00:00
|
|
|
};
|
2024-03-19 15:17:38 +00:00
|
|
|
surrealdb::iam::verify::token(DB.get().unwrap(), &mut self.session, &token.0).await?;
|
|
|
|
Ok(Value::None)
|
2023-08-16 12:27:53 +00:00
|
|
|
}
|
|
|
|
}
|