github.com/deanMdreon/kafka-go@v0.4.32/endtxn.go (about)

     1  package kafka
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"net"
     7  	"time"
     8  
     9  	"github.com/deanMdreon/kafka-go/protocol/endtxn"
    10  )
    11  
    12  // EndTxnRequest represets a request sent to a kafka broker to end a transaction.
    13  type EndTxnRequest 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  	ProducerID int
    22  
    23  	// The epoch associated with the current producer session for the given PID
    24  	ProducerEpoch int
    25  
    26  	// Committed should be set to true if the transaction was committed, false otherwise.
    27  	Committed bool
    28  }
    29  
    30  // EndTxnResponse represents a resposne from a kafka broker to an end transaction request.
    31  type EndTxnResponse struct {
    32  	// The amount of time that the broker throttled the request.
    33  	Throttle time.Duration
    34  
    35  	// Error is non-nil if an error occureda and contains the kafka error code.
    36  	// Programs may use the standard errors.Is function to test the error
    37  	// against kafka error codes.
    38  	Error error
    39  }
    40  
    41  // EndTxn sends an EndTxn request to a kafka broker and returns its response.
    42  func (c *Client) EndTxn(ctx context.Context, req *EndTxnRequest) (*EndTxnResponse, error) {
    43  	m, err := c.roundTrip(ctx, req.Addr, &endtxn.Request{
    44  		TransactionalID: req.TransactionalID,
    45  		ProducerID:      int64(req.ProducerID),
    46  		ProducerEpoch:   int16(req.ProducerEpoch),
    47  		Committed:       req.Committed,
    48  	})
    49  	if err != nil {
    50  		return nil, fmt.Errorf("kafka.(*Client).EndTxn: %w", err)
    51  	}
    52  
    53  	r := m.(*endtxn.Response)
    54  
    55  	res := &EndTxnResponse{
    56  		Throttle: makeDuration(r.ThrottleTimeMs),
    57  		Error:    makeError(r.ErrorCode, ""),
    58  	}
    59  
    60  	return res, nil
    61  }