Skip to main content

CompositeCursor

Struct CompositeCursor 

Source
pub struct CompositeCursor {
    pub positions: HashMap<u16, Arc<str>>,
}
Expand description

Composite cursor tracking position across multiple shards.

Fields§

§positions: HashMap<u16, Arc<str>>

Per-shard positions (shard_id -> stream_id).

Stored as Arc<str> so internal copies (e.g. cursor.clone() inside the poll merger) bump a refcount instead of duplicating each id’s bytes.

Implementations§

Source§

impl CompositeCursor

Source

pub fn new() -> Self

Create an empty cursor.

Source

pub fn encode(&self) -> Result<String, ConsumerError>

Encode the cursor as a base64 string.

Pre-fix used unwrap_or_default(), which silently produced an empty string on serialization failure. The empty cursor then base64-encoded to an empty string and the consumer’s next poll restarted from the beginning of the stream — silent rewind. For the current positions schema (HashMap<u16, Arc<str>>), serialization is infallible, so the failure path is unreachable.

We surface that as a ConsumerError::InvalidCursor rather than a panic. poll() is an async fn, and a panic that propagates from there can abort the surrounding tokio runtime worker. Returning Err lets poll() stay non-panicking even if a future schema change breaks the invariant; the caller will see a structured error and can retry / log instead of taking down a worker.

Source

pub fn decode(s: &str) -> Result<Self, ConsumerError>

Decode a cursor from a base64 string.

Source

pub fn get(&self, shard_id: u16) -> Option<&str>

Get the position for a specific shard.

Source

pub fn set(&mut self, shard_id: u16, position: impl Into<Arc<str>>)

Set the position for a specific shard.

Accepts anything that converts into an Arc<str> — notably String, &str, and Arc<str> itself. This lets adapters hand us a freshly-allocated String (becomes a single boxed allocation) without forcing a second copy for the cursor.

Unconditional: skips the backend-format guard and the monotonicity guard. Use Self::set_checked from production poll paths — the documented “refuse to advance across a JetStream → Redis cursor format change” protection lives in the checked variant, not here. The unchecked variant is kept public for tests that need to seed a cursor directly.

Source

pub fn set_checked(&mut self, shard_id: u16, new_id: &str) -> bool

Set the position for a specific shard, applying the same backend-format mismatch guard as Self::update_from_events. Returns true if the write happened, false if it was refused (format mismatch — error-logged so operators see the migration). No-existing-position is treated as accept (first write).

Production poll paths must use this instead of Self::set so a mid-stream backend migration (JetStream → Redis or vice versa) is surfaced and refused at the cursor write site rather than silently overwriting the format and stalling the caller.

Source

pub fn update_from_events(&mut self, events: &[StoredEvent])

Update positions from consumed events.

Per-shard CAS routed through compare_stream_ids, which understands the Redis (<ms>-<seq>) and JetStream (<u64>) formats numerically and falls back to lex for opaque ids (ULID, UUIDv7, hex digests). The cursor cannot regress and decade-rollovers cannot freeze it. Unconditional inserts would let whichever event for a given shard_id appeared last in the slice win regardless of stream order; a plain str::cmp CAS would wedge on the unpadded numeric ids both built-in adapters emit ("9" > "10" lexicographically).

Trait Implementations§

Source§

impl Clone for CompositeCursor

Source§

fn clone(&self) -> CompositeCursor

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for CompositeCursor

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for CompositeCursor

Source§

fn default() -> CompositeCursor

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for CompositeCursor

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Serialize for CompositeCursor

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,