Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove inactivity timeout #27

Merged
merged 1 commit into from
Aug 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ serde = { version = "1.0.126", features = ["derive"] }
serde_json = "1.0.64"
url = "2.2.2"

tokio = { version = "1.6.1", features = ["fs", "process", "macros", "rt", "rt-multi-thread", "time"] }
tokio = { version = "1.6.1", features = ["fs", "process", "macros", "rt", "rt-multi-thread"] }
tokio-util = { version = "0.6.7", features = ["codec"] }
warp = { version = "0.3.1", default-features = false, features = ["websocket"] }

Expand Down
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ WebSocket proxy for Language Servers.
```
$ lsp-ws-proxy --help

Usage: lsp-ws-proxy [-l <listen>] [-t <timeout>] [-s] [-r] [-v]
Usage: lsp-ws-proxy [-l <listen>] [-s] [-r] [-v]

Start WebSocket proxy for the LSP Server.
Anything after the option delimiter is used to start the server.
Expand All @@ -21,7 +21,6 @@ Examples:

Options:
-l, --listen address or port to listen on (default: 0.0.0.0:9999)
-t, --timeout inactivity timeout in seconds
-s, --sync write text document to disk on save, and enable `/files`
endpoint
-r, --remap remap relative uri (source://)
Expand All @@ -39,7 +38,6 @@ See [qualified/lsps] for an example of using proxied [Rust Analyzer][rust-analyz
## Features

- [x] Proxy messages
- [x] Inactivity timeout
- [x] Synchronize files
- [x] Manipulate remote files with `POST /files`
- [x] Remap relative `DocumentUri` (`source://`)
Expand Down
25 changes: 4 additions & 21 deletions src/api/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@ use futures_util::{
future::{select, Either},
SinkExt, StreamExt,
};
use tokio::{
fs,
process::Command,
time::{Duration, Instant},
};
use tokio::{fs, process::Command};
use url::Url;
use warp::{Filter, Rejection, Reply};

Expand All @@ -22,7 +18,6 @@ pub struct Context {
pub sync: bool,
pub remap: bool,
pub cwd: Url,
pub timeout: Duration,
}

/// Handler for WebSocket connection.
Expand Down Expand Up @@ -81,13 +76,11 @@ async fn connected(

let mut client_msg = client_recv.next();
let mut server_msg = server_recv.next();
let timer = tokio::time::sleep(ctx.timeout);
tokio::pin!(timer);

loop {
match select(select(client_msg, server_msg), timer).await {
match select(client_msg, server_msg).await {
// From Client
Either::Left((Either::Left((from_client, p_server_msg)), p_timer)) => {
Either::Left((from_client, p_server_msg)) => {
match from_client {
// Valid LSP message
Some(Ok(Message::Message(mut msg))) => {
Expand Down Expand Up @@ -130,12 +123,10 @@ async fn connected(

client_msg = client_recv.next();
server_msg = p_server_msg;
timer = p_timer;
timer.as_mut().reset(Instant::now() + ctx.timeout);
}

// From Server
Either::Left((Either::Right((from_server, p_client_msg)), p_timer)) => {
Either::Right((from_server, p_client_msg)) => {
match from_server {
// Serialized LSP Message
Some(Ok(text)) => {
Expand Down Expand Up @@ -171,14 +162,6 @@ async fn connected(

client_msg = p_client_msg;
server_msg = server_recv.next();
timer = p_timer;
timer.as_mut().reset(Instant::now() + ctx.timeout);
}

Either::Right(_) => {
tracing::info!("inactivity timeout reached, closing");
client_send.send(warp::ws::Message::close()).await?;
break;
}
}
}
Expand Down
14 changes: 0 additions & 14 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::net::SocketAddr;

use argh::FromArgs;
use tokio::time::Duration;
use url::Url;
use warp::{http, Filter};

Expand Down Expand Up @@ -31,10 +30,6 @@ struct Options {
from_str_fn(parse_listen)
)]
listen: String,
// TODO Using seconds for now for simplicity. Maybe accept duration strings like `1h` instead.
/// inactivity timeout in seconds
#[argh(option, short = 't', default = "0")]
timeout: u64,
/// write text document to disk on save, and enable `/files` endpoint
#[argh(switch, short = 's')]
sync: bool,
Expand All @@ -46,21 +41,13 @@ struct Options {
version: bool,
}

// Large enough value used to disable inactivity timeout.
const NO_TIMEOUT: u64 = 60 * 60 * 24 * 30 * 12;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
tracing_subscriber::fmt()
.with_env_filter(std::env::var("RUST_LOG").unwrap_or_else(|_| "info".to_owned()))
.init();

let (opts, command) = get_opts_and_command();
let timeout = if opts.timeout == 0 {
Duration::from_secs(NO_TIMEOUT)
} else {
Duration::from_secs(opts.timeout)
};

let cwd = std::env::current_dir()?;
// TODO Move these to `api` module.
Expand All @@ -75,7 +62,6 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
sync: opts.sync,
remap: opts.remap,
cwd: Url::from_directory_path(&cwd).expect("valid url from current dir"),
timeout,
});
let healthz = warp::path::end().and(warp::get()).map(|| "OK");
let addr = opts.listen.parse::<SocketAddr>().expect("valid addr");
Expand Down