Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/SantaClaas/gigit. git client
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136//! One actor per open repository.//!//! `gigit_git::Repository` is blocking and thread-affine, so each one gets a//! thread of its own that owns it outright. Nothing else ever holds a reference//! to it, which is why there is no lock anywhere in this file. Callers get a//! [`RepositoryHandle`], which is just a channel sender and is cheap to clone.
use std::path::{Path, PathBuf};
use gigit_git::{RefEntry, Repository, RepositorySummary};use tokio::sync::{mpsc, oneshot};
use crate::error::{Error, Result};
/// How many requests can be in flight before callers start waiting. Requests/// are answered in microseconds to milliseconds, so this only ever fills up if/// something is badly wrong.const INBOX_CAPACITY: usize = 32;
/// A request for the thread that owns a repository.////// Every variant carries the channel its answer goes back on, so the actor/// never needs to know who asked.enum Message { Summary { respond: oneshot::Sender<gigit_git::Result<RepositorySummary>>, }, References { respond: oneshot::Sender<gigit_git::Result<Vec<RefEntry>>>, },}
/// A cheap, clonable way to talk to one repository's thread.#[derive(Clone, Debug)]pub struct RepositoryHandle { outbox: mpsc::Sender<Message>,}
impl RepositoryHandle { /// Open a repository on a thread of its own. /// /// Opening is itself blocking, so it happens on the new thread rather than /// on the caller's: the repository is owned by its thread from the very /// first moment, and the async runtime is never blocked. pub async fn open(path: PathBuf) -> Result<(Self, RepositorySummary)> { let (outbox, inbox) = mpsc::channel(INBOX_CAPACITY); let (ready, opened) = oneshot::channel();
std::thread::Builder::new() .name(format!("gigit-repository {}", path.display())) .spawn(move || run(&path, inbox, ready)) .map_err(Error::SpawnThread)?;
// The thread drops `ready` without sending only if it panics. let summary = opened.await.map_err(|_| Error::WorkerStopped)??;
Ok((Self { outbox }, summary)) }
pub async fn summary(&self) -> Result<RepositorySummary> { self.request(|respond| Message::Summary { respond }).await }
pub async fn references(&self) -> Result<Vec<RefEntry>> { self.request(|respond| Message::References { respond }) .await }
/// Send a request and wait for its answer. /// /// Both channels can only fail by the actor being gone, which for a handle /// held by a caller means the thread panicked or the repository was closed /// underneath them. async fn request<T>( &self, build: impl FnOnce(oneshot::Sender<gigit_git::Result<T>>) -> Message, ) -> Result<T> { let (respond, answer) = oneshot::channel();
self.outbox .send(build(respond)) .await .map_err(|_| Error::WorkerStopped)?;
answer .await .map_err(|_| Error::WorkerStopped)? .map_err(Error::Git) }}
/// The body of a repository thread: open, report, then serve until the last/// handle is dropped and the channel closes.fn run( path: &Path, mut inbox: mpsc::Receiver<Message>, ready: oneshot::Sender<Result<RepositorySummary>>,) { let repository = match open_and_summarise(path) { Ok(opened) => opened, Err(error) => { // Nobody waiting for the answer is not an error worth reporting — // it just means the caller gave up first. let _ = ready.send(Err(error)); return; } };
let (repository, summary) = repository; if ready.send(Ok(summary)).is_err() { // The caller went away before we finished opening, so there is nobody // left to serve. return; }
// `blocking_recv` is the point of running on a dedicated thread: the // blocking git work below never touches the async runtime's workers. while let Some(message) = inbox.blocking_recv() { match message { Message::Summary { respond } => { let _ = respond.send(repository.summary()); } Message::References { respond } => { let _ = respond.send(repository.references()); } } }}
fn open_and_summarise(path: &Path) -> Result<(Repository, RepositorySummary)> { let repository = Repository::discover(path)?; let summary = repository.summary()?;
Ok((repository, summary))}