use nisshi_sans_io::{ApiKey, EndTxnRequest, EndTxnResponse};
use rama::{Context, Service};
use tracing::instrument;
use crate::{Error, Result, Storage};
#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct EndService;
impl ApiKey for EndService {
const KEY: i16 = EndTxnRequest::KEY;
}
impl<G> Service<G, EndTxnRequest> for EndService
where
G: Storage,
{
type Response = EndTxnResponse;
type Error = Error;
#[instrument(skip(ctx, req))]
async fn serve(
&self,
ctx: Context<G>,
req: EndTxnRequest,
) -> Result<Self::Response, Self::Error> {
ctx.state()
.txn_end(
req.transactional_id.as_str(),
req.producer_id,
req.producer_epoch,
req.committed,
)
.await
.map(|error_code| EndTxnResponse::default().error_code(i16::from(error_code)))
}
}