Anki/pylib/rsbridge/lib.rs
Damien Elmes cf45cbf429
Rework syncing code, and replace local sync server (#2329)
This PR replaces the existing Python-driven sync server with a new one in Rust.
The new server supports both collection and media syncing, and is compatible
with both the new protocol mentioned below, and older clients. A setting has
been added to the preferences screen to point Anki to a local server, and a
similar setting is likely to come to AnkiMobile soon.

Documentation is available here: <https://docs.ankiweb.net/sync-server.html>

In addition to the new server and refactoring, this PR also makes changes to the
sync protocol. The existing sync protocol places payloads and metadata inside a
multipart POST body, which causes a few headaches:

- Legacy clients build the request in a non-deterministic order, meaning the
entire request needs to be scanned to extract the metadata.
- Reqwest's multipart API directly writes the multipart body, without exposing
the resulting stream to us, making it harder to track the progress of the
transfer. We've been relying on a patched version of reqwest for timeouts,
which is a pain to keep up to date.

To address these issues, the metadata is now sent in a HTTP header, with the
data payload sent directly in the body. Instead of the slower gzip, we now
use zstd. The old timeout handling code has been replaced with a new implementation
that wraps the request and response body streams to track progress, allowing us
to drop the git dependencies for reqwest, hyper-timeout and tokio-io-timeout.

The main other change to the protocol is that one-way syncs no longer need to
downgrade the collection to schema 11 prior to sending.
2023-01-18 12:43:46 +10:00

89 lines
2.5 KiB
Rust

// Copyright: Ankitects Pty Ltd and contributors
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
use anki::{
backend::{init_backend, Backend as RustBackend},
log::set_global_logger,
sync::http_server::SimpleServer,
};
use pyo3::{
create_exception, exceptions::PyException, prelude::*, types::PyBytes, wrap_pyfunction,
};
#[pyclass(module = "_rsbridge")]
struct Backend {
backend: RustBackend,
}
create_exception!(_rsbridge, BackendError, PyException);
#[pyfunction]
fn buildhash() -> &'static str {
anki::version::buildhash()
}
#[pyfunction]
fn initialize_logging(path: Option<&str>) -> PyResult<()> {
set_global_logger(path).map_err(|e| PyException::new_err(e.to_string()))
}
#[pyfunction]
fn syncserver() -> PyResult<()> {
set_global_logger(None).unwrap();
SimpleServer::run().map_err(|e| PyException::new_err(format!("{e:?}")))
}
#[pyfunction]
fn open_backend(init_msg: &PyBytes) -> PyResult<Backend> {
match init_backend(init_msg.as_bytes()) {
Ok(backend) => Ok(Backend { backend }),
Err(e) => Err(PyException::new_err(e)),
}
}
#[pymethods]
impl Backend {
fn command(
&self,
py: Python,
service: u32,
method: u32,
input: &PyBytes,
) -> PyResult<PyObject> {
let in_bytes = input.as_bytes();
py.allow_threads(|| self.backend.run_method(service, method, in_bytes))
.map(|out_bytes| {
let out_obj = PyBytes::new(py, &out_bytes);
out_obj.into()
})
.map_err(BackendError::new_err)
}
/// This takes and returns JSON, due to Python's slow protobuf
/// encoding/decoding.
fn db_command(&self, py: Python, input: &PyBytes) -> PyResult<PyObject> {
let in_bytes = input.as_bytes();
let out_res = py.allow_threads(|| {
self.backend
.run_db_command_bytes(in_bytes)
.map_err(BackendError::new_err)
});
let out_bytes = out_res?;
let out_obj = PyBytes::new(py, &out_bytes);
Ok(out_obj.into())
}
}
// Module definition
//////////////////////////////////
#[pymodule]
fn _rsbridge(_py: Python, m: &PyModule) -> PyResult<()> {
m.add_class::<Backend>()?;
m.add_wrapped(wrap_pyfunction!(buildhash)).unwrap();
m.add_wrapped(wrap_pyfunction!(open_backend)).unwrap();
m.add_wrapped(wrap_pyfunction!(initialize_logging)).unwrap();
m.add_wrapped(wrap_pyfunction!(syncserver)).unwrap();
Ok(())
}