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