Skip to main content

tansu_storage/service/txn/
offset_commit.rs

1// Copyright ⓒ 2024-2025 Peter Morgan <peter.james.morgan@gmail.com>
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use rama::{Context, Service};
16use tansu_sans_io::{ApiKey, TxnOffsetCommitResponse};
17use tracing::instrument;
18
19use crate::{Error, Result, Storage};
20
21/// A [`Service`] using [`Storage`] as [`Context`] taking [`tansu_sans_io::TxnOffsetCommitRequest`] returning [`TxnOffsetCommitResponse`].
22#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
23pub struct OffsetCommitService;
24
25impl ApiKey for OffsetCommitService {
26    const KEY: i16 = tansu_sans_io::TxnOffsetCommitRequest::KEY;
27}
28
29impl<G> Service<G, tansu_sans_io::TxnOffsetCommitRequest> for OffsetCommitService
30where
31    G: Storage,
32{
33    type Response = TxnOffsetCommitResponse;
34    type Error = Error;
35
36    #[instrument(skip(ctx, req))]
37    async fn serve(
38        &self,
39        ctx: Context<G>,
40        req: tansu_sans_io::TxnOffsetCommitRequest,
41    ) -> Result<Self::Response, Self::Error> {
42        let responses = ctx
43            .state()
44            .txn_offset_commit(crate::TxnOffsetCommitRequest {
45                transaction_id: req.transactional_id.to_owned(),
46                group_id: req.group_id.to_owned(),
47                producer_id: req.producer_id,
48                producer_epoch: req.producer_epoch,
49                generation_id: req.generation_id,
50                member_id: req.member_id,
51                group_instance_id: req.group_instance_id,
52                topics: req.topics.unwrap_or_default(),
53            })
54            .await?;
55
56        Ok(TxnOffsetCommitResponse::default()
57            .throttle_time_ms(0)
58            .topics(Some(responses)))
59    }
60}