github.com/streamdal/segmentio-kafka-go@v0.4.47-streamdal/initproducerid.go (about)

     1  package kafka
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"net"
     7  	"time"
     8  
     9  	"github.com/segmentio/kafka-go/protocol/initproducerid"
    10  )
    11  
    12  // InitProducerIDRequest is the request structure for the InitProducerId function.
    13  type InitProducerIDRequest 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  	// Time after which a transaction should time out
    21  	TransactionTimeoutMs int
    22  
    23  	// The Producer ID (PID).
    24  	// This is used to disambiguate requests if a transactional id is reused following its expiration.
    25  	// Only supported in version >=3 of the request, will be ignore otherwise.
    26  	ProducerID int
    27  
    28  	// The producer's current epoch.
    29  	// This will be checked against the producer epoch on the broker,
    30  	// and the request will return an error if they do not match.
    31  	// Only supported in version >=3 of the request, will be ignore otherwise.
    32  	ProducerEpoch int
    33  }
    34  
    35  // ProducerSession contains useful information about the producer session from the broker's response.
    36  type ProducerSession struct {
    37  	// The Producer ID (PID) for the current producer session
    38  	ProducerID int
    39  
    40  	// The epoch associated with the current producer session for the given PID
    41  	ProducerEpoch int
    42  }
    43  
    44  // InitProducerIDResponse is the response structure for the InitProducerId function.
    45  type InitProducerIDResponse struct {
    46  	// The Transaction/Group Coordinator details
    47  	Producer *ProducerSession
    48  
    49  	// The amount of time that the broker throttled the request.
    50  	Throttle time.Duration
    51  
    52  	// An error that may have occurred while attempting to retrieve initProducerId
    53  	//
    54  	// The error contains both the kafka error code, and an error message
    55  	// returned by the kafka broker.
    56  	Error error
    57  }
    58  
    59  // InitProducerID sends a initProducerId request to a kafka broker and returns the
    60  // response.
    61  func (c *Client) InitProducerID(ctx context.Context, req *InitProducerIDRequest) (*InitProducerIDResponse, error) {
    62  	m, err := c.roundTrip(ctx, req.Addr, &initproducerid.Request{
    63  		TransactionalID:      req.TransactionalID,
    64  		TransactionTimeoutMs: int32(req.TransactionTimeoutMs),
    65  		ProducerID:           int64(req.ProducerID),
    66  		ProducerEpoch:        int16(req.ProducerEpoch),
    67  	})
    68  	if err != nil {
    69  		return nil, fmt.Errorf("kafka.(*Client).InitProducerId: %w", err)
    70  	}
    71  
    72  	res := m.(*initproducerid.Response)
    73  
    74  	return &InitProducerIDResponse{
    75  		Producer: &ProducerSession{
    76  			ProducerID:    int(res.ProducerID),
    77  			ProducerEpoch: int(res.ProducerEpoch),
    78  		},
    79  		Throttle: makeDuration(res.ThrottleTimeMs),
    80  		Error:    makeError(res.ErrorCode, ""),
    81  	}, nil
    82  }