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

     1  package kafka
     2  
     3  import (
     4  	"bufio"
     5  	"context"
     6  	"fmt"
     7  	"net"
     8  	"time"
     9  
    10  	"github.com/segmentio/kafka-go/protocol/leavegroup"
    11  )
    12  
    13  // LeaveGroupRequest is the request structure for the LeaveGroup function.
    14  type LeaveGroupRequest struct {
    15  	// Address of the kafka broker to sent he request to.
    16  	Addr net.Addr
    17  
    18  	// GroupID of the group to leave.
    19  	GroupID string
    20  
    21  	// List of leaving member identities.
    22  	Members []LeaveGroupRequestMember
    23  }
    24  
    25  // LeaveGroupRequestMember represents the indentify of a member leaving a group.
    26  type LeaveGroupRequestMember struct {
    27  	// The member ID to remove from the group.
    28  	ID string
    29  
    30  	// The group instance ID to remove from the group.
    31  	GroupInstanceID string
    32  }
    33  
    34  // LeaveGroupResponse is the response structure for the LeaveGroup function.
    35  type LeaveGroupResponse struct {
    36  	// An error that may have occurred when attempting to leave the group.
    37  	//
    38  	// The errors contain the kafka error code. Programs may use the standard
    39  	// errors.Is function to test the error against kafka error codes.
    40  	Error error
    41  
    42  	// The amount of time that the broker throttled the request.
    43  	Throttle time.Duration
    44  
    45  	// List of leaving member responses.
    46  	Members []LeaveGroupResponseMember
    47  }
    48  
    49  // LeaveGroupResponseMember represents a member leaving the group.
    50  type LeaveGroupResponseMember struct {
    51  	// The member ID of the member leaving the group.
    52  	ID string
    53  
    54  	// The group instance ID to remove from the group.
    55  	GroupInstanceID string
    56  
    57  	// An error that may have occured when attempting to remove the member from the group.
    58  	//
    59  	// The errors contain the kafka error code. Programs may use the standard
    60  	// errors.Is function to test the error against kafka error codes.
    61  	Error error
    62  }
    63  
    64  func (c *Client) LeaveGroup(ctx context.Context, req *LeaveGroupRequest) (*LeaveGroupResponse, error) {
    65  	leaveGroup := leavegroup.Request{
    66  		GroupID: req.GroupID,
    67  		Members: make([]leavegroup.RequestMember, 0, len(req.Members)),
    68  	}
    69  
    70  	for _, member := range req.Members {
    71  		leaveGroup.Members = append(leaveGroup.Members, leavegroup.RequestMember{
    72  			MemberID:        member.ID,
    73  			GroupInstanceID: member.GroupInstanceID,
    74  		})
    75  	}
    76  
    77  	m, err := c.roundTrip(ctx, req.Addr, &leaveGroup)
    78  	if err != nil {
    79  		return nil, fmt.Errorf("kafka.(*Client).LeaveGroup: %w", err)
    80  	}
    81  
    82  	r := m.(*leavegroup.Response)
    83  
    84  	res := &LeaveGroupResponse{
    85  		Error:    makeError(r.ErrorCode, ""),
    86  		Throttle: makeDuration(r.ThrottleTimeMS),
    87  	}
    88  
    89  	if len(r.Members) == 0 {
    90  		// If we're using a version of the api without the
    91  		// members array in the response, just add a member
    92  		// so the api is consistent across versions.
    93  		r.Members = []leavegroup.ResponseMember{
    94  			{
    95  				MemberID:        req.Members[0].ID,
    96  				GroupInstanceID: req.Members[0].GroupInstanceID,
    97  			},
    98  		}
    99  	}
   100  
   101  	res.Members = make([]LeaveGroupResponseMember, 0, len(r.Members))
   102  	for _, member := range r.Members {
   103  		res.Members = append(res.Members, LeaveGroupResponseMember{
   104  			ID:              member.MemberID,
   105  			GroupInstanceID: member.GroupInstanceID,
   106  			Error:           makeError(member.ErrorCode, ""),
   107  		})
   108  	}
   109  
   110  	return res, nil
   111  }
   112  
   113  type leaveGroupRequestV0 struct {
   114  	// GroupID holds the unique group identifier
   115  	GroupID string
   116  
   117  	// MemberID assigned by the group coordinator or the zero string if joining
   118  	// for the first time.
   119  	MemberID string
   120  }
   121  
   122  func (t leaveGroupRequestV0) size() int32 {
   123  	return sizeofString(t.GroupID) + sizeofString(t.MemberID)
   124  }
   125  
   126  func (t leaveGroupRequestV0) writeTo(wb *writeBuffer) {
   127  	wb.writeString(t.GroupID)
   128  	wb.writeString(t.MemberID)
   129  }
   130  
   131  type leaveGroupResponseV0 struct {
   132  	// ErrorCode holds response error code
   133  	ErrorCode int16
   134  }
   135  
   136  func (t leaveGroupResponseV0) size() int32 {
   137  	return sizeofInt16(t.ErrorCode)
   138  }
   139  
   140  func (t leaveGroupResponseV0) writeTo(wb *writeBuffer) {
   141  	wb.writeInt16(t.ErrorCode)
   142  }
   143  
   144  func (t *leaveGroupResponseV0) readFrom(r *bufio.Reader, size int) (remain int, err error) {
   145  	remain, err = readInt16(r, size, &t.ErrorCode)
   146  	return
   147  }