use std::time::Duration;
use super::{GroupConsumerCycleAdmission, GroupConsumerCyclePortErrorCategory, GroupConsumerPort};
use crate::clock::DeadlineCapture;
#[must_use = "a captured group start should be admitted or deliberately discarded"]
pub struct GroupConsumerStartCapture {
port: GroupConsumerPort,
capture: DeadlineCapture,
}
impl GroupConsumerStartCapture {
pub(in crate::consumer) fn capture(
port: GroupConsumerPort,
timeout: Duration,
) -> Result<Self, GroupConsumerStartError> {
let capture = port.capture_cycle_deadline(timeout).map_err(|_error| {
GroupConsumerStartError::from_port(GroupConsumerCyclePortErrorCategory::InvalidTimeout)
})?;
if timeout.is_zero() {
return Err(GroupConsumerStartError::from_port(
GroupConsumerCyclePortErrorCategory::InvalidTimeout,
));
}
Ok(Self { port, capture })
}
pub(in crate::consumer) fn admit(
self,
port: &GroupConsumerPort,
group_id: kafka_client_core::GroupId,
) -> Result<GroupConsumerCycleAdmission, GroupConsumerStartError> {
if !self.port.shares_registry_with(port) {
return Err(GroupConsumerStartError::from_port(
GroupConsumerCyclePortErrorCategory::InternalInvariant,
));
}
port.admit_captured_cycle(group_id, self.capture)
.map_err(|error| GroupConsumerStartError::from_port(error.public_category()))
}
}
impl core::fmt::Debug for GroupConsumerStartCapture {
fn fmt(&self, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
formatter
.debug_struct("GroupConsumerStartCapture")
.finish_non_exhaustive()
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum GroupConsumerStartErrorKind {
Closed,
Contended,
AlreadyStarted,
GroupUnavailable,
InvalidTimeout,
Internal,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct GroupConsumerStartError {
kind: GroupConsumerStartErrorKind,
}
impl GroupConsumerStartError {
pub(in crate::consumer) const fn from_port(
category: GroupConsumerCyclePortErrorCategory,
) -> Self {
let kind = match category {
GroupConsumerCyclePortErrorCategory::InvalidTimeout => {
GroupConsumerStartErrorKind::InvalidTimeout
}
GroupConsumerCyclePortErrorCategory::Closed => GroupConsumerStartErrorKind::Closed,
GroupConsumerCyclePortErrorCategory::Contended => {
GroupConsumerStartErrorKind::Contended
}
GroupConsumerCyclePortErrorCategory::AlreadyStarted => {
GroupConsumerStartErrorKind::AlreadyStarted
}
GroupConsumerCyclePortErrorCategory::GroupUnavailable => {
GroupConsumerStartErrorKind::GroupUnavailable
}
GroupConsumerCyclePortErrorCategory::InternalInvariant => {
GroupConsumerStartErrorKind::Internal
}
};
Self { kind }
}
pub const fn kind(self) -> GroupConsumerStartErrorKind {
self.kind
}
}
impl core::fmt::Display for GroupConsumerStartError {
fn fmt(&self, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(
formatter,
"group-consumer membership start rejected: {:?}",
self.kind
)
}
}
impl std::error::Error for GroupConsumerStartError {}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[must_use = "accepted membership remains owned even when advisory progress degraded"]
pub struct GroupConsumerStartAccepted {
entry_faulted: bool,
wake_failed: bool,
}
impl GroupConsumerStartAccepted {
pub(in crate::consumer) const fn new(entry_faulted: bool, wake_failed: bool) -> Self {
Self {
entry_faulted,
wake_failed,
}
}
pub const fn entry_faulted(self) -> bool {
self.entry_faulted
}
pub const fn wake_failed(self) -> bool {
self.wake_failed
}
}