cli: reapply "code server-web when offline"

This commit is contained in:
Connor Peet
2024-09-09 08:52:50 -07:00
parent 70849c674d
commit 4d221c6b85
6 changed files with 71 additions and 17 deletions

View File

@@ -81,4 +81,5 @@ codegen-units = 1
[features]
default = []
vsda = []
vscode-encrypt = []

View File

@@ -723,7 +723,7 @@ impl Auth {
match &init_code_json.message {
Some(m) => self.log.result(m),
None => self.log.result(&format!(
None => self.log.result(format!(
"To grant access to the server, please log into {} and use code {}",
init_code_json.verification_uri, init_code_json.user_code
)),

View File

@@ -15,7 +15,7 @@ use std::time::{Duration, Instant};
use hyper::service::{make_service_fn, service_fn};
use hyper::{Body, Request, Response, Server};
use tokio::io::{AsyncBufReadExt, BufReader};
use tokio::pin;
use tokio::{pin, time};
use crate::async_pipe::{
get_socket_name, get_socket_rw_stream, listen_socket_rw_stream, AsyncPipe,
@@ -50,7 +50,7 @@ const SERVER_IDLE_TIMEOUT_SECS: u64 = 60 * 60;
/// (should be large enough to basically never happen)
const SERVER_ACTIVE_TIMEOUT_SECS: u64 = SERVER_IDLE_TIMEOUT_SECS * 24 * 30 * 12;
/// How long to cache the "latest" version we get from the update service.
const RELEASE_CACHE_SECS: u64 = 60 * 60;
const RELEASE_CHECK_INTERVAL: u64 = 60 * 60;
/// Number of bytes for the secret keys. See workbench.ts for their usage.
const SECRET_KEY_BYTES: usize = 32;
@@ -86,7 +86,11 @@ pub async fn serve_web(ctx: CommandContext, mut args: ServeWebArgs) -> Result<i3
}
}
let cm = ConnectionManager::new(&ctx, platform, args.clone());
let cm: Arc<ConnectionManager> = ConnectionManager::new(&ctx, platform, args.clone());
let update_check_interval = 3600;
cm.clone()
.start_update_checker(Duration::from_secs(update_check_interval));
let key = get_server_key_half(&ctx.paths);
let make_svc = move || {
let ctx = HandleContext {
@@ -175,7 +179,7 @@ async fn handle_proxied(ctx: &HandleContext, req: Request<Body>) -> Response<Bod
let release = if let Some((r, _)) = get_release_from_path(req.uri().path(), ctx.cm.platform) {
r
} else {
match ctx.cm.get_latest_release().await {
match ctx.cm.get_release_from_cache().await {
Ok(r) => r,
Err(e) => {
error!(ctx.log, "error getting latest version: {}", e);
@@ -538,21 +542,67 @@ impl ConnectionManager {
pub fn new(ctx: &CommandContext, platform: Platform, args: ServeWebArgs) -> Arc<Self> {
let base_path = normalize_base_path(args.server_base_path.as_deref().unwrap_or_default());
let cache = DownloadCache::new(ctx.paths.web_server_storage());
let target_kind = TargetKind::Web;
let quality = VSCODE_CLI_QUALITY.map_or(Quality::Stable, |q| match Quality::try_from(q) {
Ok(q) => q,
Err(_) => Quality::Stable,
});
let latest_version = tokio::sync::Mutex::new(cache.get().first().map(|latest_commit| {
(
Instant::now() - Duration::from_secs(RELEASE_CHECK_INTERVAL),
Release {
name: String::from("0.0.0"), // Version information not stored on cache
commit: latest_commit.clone(),
platform,
target: target_kind,
quality,
},
)
}));
Arc::new(Self {
platform,
args,
base_path,
log: ctx.log.clone(),
cache: DownloadCache::new(ctx.paths.web_server_storage()),
cache,
update_service: UpdateService::new(
ctx.log.clone(),
Arc::new(ReqwestSimpleHttp::with_client(ctx.http.clone())),
),
state: ConnectionStateMap::default(),
latest_version: tokio::sync::Mutex::default(),
latest_version,
})
}
// spawns a task that checks for updates every n seconds duration
pub fn start_update_checker(self: Arc<Self>, duration: Duration) {
tokio::spawn(async move {
let mut interval = time::interval(duration);
loop {
interval.tick().await;
if let Err(e) = self.get_latest_release().await {
warning!(self.log, "error getting latest version: {}", e);
}
}
});
}
// Returns the latest release from the cache, if one exists.
pub async fn get_release_from_cache(&self) -> Result<Release, CodeError> {
let latest = self.latest_version.lock().await;
if let Some((_, release)) = &*latest {
return Ok(release.clone());
}
drop(latest);
self.get_latest_release().await
}
/// Gets a connection to a server version
pub async fn get_connection(
&self,
@@ -571,11 +621,7 @@ impl ConnectionManager {
pub async fn get_latest_release(&self) -> Result<Release, CodeError> {
let mut latest = self.latest_version.lock().await;
let now = Instant::now();
if let Some((checked_at, release)) = &*latest {
if checked_at.elapsed() < Duration::from_secs(RELEASE_CACHE_SECS) {
return Ok(release.clone());
}
}
let target_kind = TargetKind::Web;
let quality = VSCODE_CLI_QUALITY
.ok_or_else(|| CodeError::UpdatesNotConfigured("no configured quality"))
@@ -585,13 +631,14 @@ impl ConnectionManager {
let release = self
.update_service
.get_latest_commit(self.platform, TargetKind::Web, quality)
.get_latest_commit(self.platform, target_kind, quality)
.await
.map_err(|e| CodeError::UpdateCheckFailed(e.to_string()));
// If the update service is unavailable and we have stale data, use that
if let (Err(e), Some((_, previous))) = (&release, &*latest) {
if let (Err(e), Some((_, previous))) = (&release, latest.clone()) {
warning!(self.log, "error getting latest release, using stale: {}", e);
*latest = Some((now, previous.clone()));
return Ok(previous.clone());
}

View File

@@ -20,6 +20,7 @@ const KEEP_LRU: usize = 5;
const STAGING_SUFFIX: &str = ".staging";
const RENAME_ATTEMPTS: u32 = 20;
const RENAME_DELAY: std::time::Duration = std::time::Duration::from_millis(200);
const PERSISTED_STATE_FILE_NAME: &str = "lru.json";
#[derive(Clone)]
pub struct DownloadCache {
@@ -30,11 +31,16 @@ pub struct DownloadCache {
impl DownloadCache {
pub fn new(path: PathBuf) -> DownloadCache {
DownloadCache {
state: PersistedState::new(path.join("lru.json")),
state: PersistedState::new(path.join(PERSISTED_STATE_FILE_NAME)),
path,
}
}
/// Gets the value stored on the state
pub fn get(&self) -> Vec<String> {
self.state.load()
}
/// Gets the download cache path. Names of cache entries can be formed by
/// joining them to the path.
pub fn path(&self) -> &Path {

View File

@@ -674,7 +674,7 @@ where
let write_line = |line: &str| -> std::io::Result<()> {
if let Some(mut f) = log_file.as_ref() {
f.write_all(line.as_bytes())?;
f.write_all(&[b'\n'])?;
f.write_all(b"\n")?;
}
if write_directly {
println!("{}", line);