github.com/QuangHoangHao/kafka-go@v0.4.36/addoffsetstotxn.go (about)

     1  package kafka
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"net"
     7  	"time"
     8  
     9  	"github.com/QuangHoangHao/kafka-go/protocol/addoffsetstotxn"
    10  )
    11  
    12  // AddOffsetsToTxnRequest is the request structure for the AddOffsetsToTxn function.
    13  type AddOffsetsToTxnRequest struct {
    14  	// Address of the kafka broker to send the request to.
    15  	Addr net.Addr
    16  
    17  	// The transactional id key
    18  	TransactionalID string
    19  
    20  	// The Producer ID (PID) for the current producer session;
    21  	// received from an InitProducerID request.
    22  	ProducerID int
    23  
    24  	// The epoch associated with the current producer session for the given PID
    25  	ProducerEpoch int
    26  
    27  	// The unique group identifier.
    28  	GroupID string
    29  }
    30  
    31  // AddOffsetsToTxnResponse is the response structure for the AddOffsetsToTxn function.
    32  type AddOffsetsToTxnResponse struct {
    33  	// The amount of time that the broker throttled the request.
    34  	Throttle time.Duration
    35  
    36  	// An error that may have occurred when attempting to add the offsets
    37  	// to a transaction.
    38  	//
    39  	// The errors contain the kafka error code. Programs may use the standard
    40  	// errors.Is function to test the error against kafka error codes.
    41  	Error error
    42  }
    43  
    44  // AddOffsetsToTnx sends an add offsets to txn request to a kafka broker and returns the response.
    45  func (c *Client) AddOffsetsToTxn(
    46  	ctx context.Context,
    47  	req *AddOffsetsToTxnRequest,
    48  ) (*AddOffsetsToTxnResponse, error) {
    49  	m, err := c.roundTrip(ctx, req.Addr, &addoffsetstotxn.Request{
    50  		TransactionalID: req.TransactionalID,
    51  		ProducerID:      int64(req.ProducerID),
    52  		ProducerEpoch:   int16(req.ProducerEpoch),
    53  		GroupID:         req.GroupID,
    54  	})
    55  	if err != nil {
    56  		return nil, fmt.Errorf("kafka.(*Client).AddOffsetsToTxn: %w", err)
    57  	}
    58  
    59  	r := m.(*addoffsetstotxn.Response)
    60  
    61  	res := &AddOffsetsToTxnResponse{
    62  		Throttle: makeDuration(r.ThrottleTimeMs),
    63  		Error:    makeError(r.ErrorCode, ""),
    64  	}
    65  
    66  	return res, nil
    67  }