diff --git a/apps/origin-src/src/cli.rs b/apps/origin-src/src/cli.rs index 8e5c9743..1139c894 100644 --- a/apps/origin-src/src/cli.rs +++ b/apps/origin-src/src/cli.rs @@ -9,7 +9,6 @@ use std::str::FromStr; use anyhow::{Context, Result}; use clap::error::ErrorKind; use clap::{Args, CommandFactory, Parser, Subcommand}; -use origin_shared::event::Filter; use origin_shared::policy::KindPattern; use origin_shared::{Digest, Inputs, Kind, RepoPath, TargetName, Verdict}; @@ -498,13 +497,7 @@ impl WorkspaceCommand { kind, subject, since, - } => workspace.watch( - &Filter { - kinds: kind, - subjects: subject, - }, - since, - )?, + } => workspace.watch(kind, subject, since)?, } Ok(()) } diff --git a/apps/origin-src/src/events.rs b/apps/origin-src/src/events.rs index f6385eea..89b601a6 100644 --- a/apps/origin-src/src/events.rs +++ b/apps/origin-src/src/events.rs @@ -32,6 +32,7 @@ use origin_platform::{Native, Platform}; use origin_shared::event::{Event, EventKind, Filter, Page}; use origin_shared::landing::{Blocked, Pending}; use origin_shared::{Object, ObjectId}; +use parking_lot::{Condvar, Mutex}; use crate::index; use crate::oplog::{Effect, Entry}; @@ -42,6 +43,39 @@ use crate::store::{Store, StoreError}; #[derive(Debug)] pub struct Events<'a, P = Native> { store: &'a Store
,
+ /// Wakes [`Events::wait`] as soon as this process writes, if set.
+ /// Otherwise it only polls.
+ wakeup: Option<&'a Wakeup>,
+}
+
+/// Wakes the readers in this process that wait for events as soon as the
+/// process writes, instead of at their next poll of the op log. Writes from
+/// other processes are still seen only at the next poll.
+#[derive(Debug, Default)]
+pub struct Wakeup {
+ /// How many writes this process has made.
+ writes: Mutex ) -> Self {
- Self { store }
+ Self {
+ store,
+ wakeup: None,
+ }
+ }
+
+ /// Returns these events with [`Events::wait`] also woken by `wakeup`.
+ #[must_use]
+ pub fn waking_on(self, wakeup: &'a Wakeup) -> Self {
+ Self {
+ wakeup: Some(wakeup),
+ ..self
+ }
}
/// Returns the cursor after every event so far. Reading from it delivers
@@ -100,8 +146,10 @@ impl<'a, P: Platform> Events<'a, P> {
Ok(page)
}
- /// Like [`Events::after`], but waits up to `timeout` for events, reading
- /// the op log every [`Events::POLL`]. Returns an empty page if none came.
+ /// Like [`Events::after`], but waits up to `timeout` for events. It reads
+ /// the op log every [`Events::POLL`], and also as soon as the [`Wakeup`],
+ /// if any, reports a write by this process. Returns an empty page if none
+ /// came.
///
/// # Errors
///
@@ -116,6 +164,8 @@ impl<'a, P: Platform> Events<'a, P> {
let start = Instant::now();
let mut cursor = cursor;
loop {
+ // Read before the op log, so no write after it is missed.
+ let seen = self.wakeup.map(Wakeup::writes);
let page = self.after(cursor, filter, limit)?;
let left = timeout.saturating_sub(start.elapsed());
if !page.events.is_empty() || left.is_zero() {
@@ -123,7 +173,11 @@ impl<'a, P: Platform> Events<'a, P> {
}
// Entries with no matching events are read past.
cursor = page.next;
- thread::sleep(Self::POLL.min(left));
+ let pause = Self::POLL.min(left);
+ match (self.wakeup, seen) {
+ (Some(wakeup), Some(seen)) => wakeup.sleep(seen, pause),
+ _ => thread::sleep(pause),
+ }
}
}
diff --git a/apps/origin-src/src/service.rs b/apps/origin-src/src/service.rs
index 5f439db6..c154ff26 100644
--- a/apps/origin-src/src/service.rs
+++ b/apps/origin-src/src/service.rs
@@ -4,7 +4,8 @@
//! Plugins use it in the same two ways.
//!
//! A service lives as long as its process. It keeps the record index open and
-//! catches it up with the op log before each use. Calls don't wait for each
+//! catches it up with the op log before each use, and wakes the event
+//! long-polls waiting on it whenever it writes. Calls don't wait for each
//! other: each takes one of the open indexes, or opens a new one, up to
//! [`OriginService::MAX_INDEXES`]; past that, calls wait for one, since each
//! index holds everything in memory. Landings on one target still run one at a
@@ -17,12 +18,14 @@ use std::collections::hash_map::Entry;
use std::collections::{HashMap, HashSet};
use std::path::PathBuf;
use std::sync::Arc;
+use std::time::Duration;
use origin_platform::Native;
use origin_shared::api::{
- Annotation, ErrorCode, LandRequest, Landings, LogEntry, NewTarget, RevisionEntry, Revisions,
- Stored, TargetInfo,
+ Annotation, ErrorCode, EventsQuery, LandRequest, Landings, LogEntry, NewTarget, RevisionEntry,
+ Revisions, Stored, TargetInfo,
};
+use origin_shared::event::{Filter, Page};
use origin_shared::landing::{Pending, Proposal};
use origin_shared::object::DecodeError;
use origin_shared::pack::Pack;
@@ -33,6 +36,7 @@ use origin_shared::{
use parking_lot::{Condvar, Mutex, MutexGuard};
use crate::annotations::{AnnotateError, Annotations};
+use crate::events::{Events, Wakeup};
use crate::index::Index;
use crate::policy::{Policy, PolicyError};
use crate::queue::{LandError, Queue};
@@ -72,9 +76,16 @@ pub struct OriginService {
node: Node,
/// The record indexes kept open for calls to use.
indexes: Indexes,
+ wakeup: Wakeup,
}
impl OriginService {
+ /// The most events a page holds, which is also the default.
+ const MAX_EVENTS: u32 = 1024;
+
+ /// The longest an event long-poll waits.
+ pub const MAX_WAIT: Duration = Duration::from_mins(1);
+
/// The most indexes open at once, in use or kept free for calls.
pub const MAX_INDEXES: usize = 8;
@@ -85,6 +96,7 @@ impl OriginService {
store,
node,
indexes: Indexes::default(),
+ wakeup: Wakeup::default(),
}
}
@@ -286,6 +298,7 @@ impl OriginService {
Ok(())
})?;
}
+ self.wakeup.notify();
Ok(Stored { new: new as u64 })
}
@@ -411,7 +424,7 @@ impl OriginService {
/// be read or written.
pub fn land(&self, _caller: &Caller, request: &LandRequest) -> Result