github.com/segmentio/kafka-go@v0.4.48-0.20240318174348-3f6244eb34fd/protocol/roundtrip.go (about)

     1  package protocol
     2  
     3  import (
     4  	"io"
     5  )
     6  
     7  // RoundTrip sends a request to a kafka broker and returns the response.
     8  func RoundTrip(rw io.ReadWriter, apiVersion int16, correlationID int32, clientID string, req Message) (Message, error) {
     9  	if err := WriteRequest(rw, apiVersion, correlationID, clientID, req); err != nil {
    10  		return nil, err
    11  	}
    12  	if !hasResponse(req) {
    13  		return nil, nil
    14  	}
    15  	id, res, err := ReadResponse(rw, req.ApiKey(), apiVersion)
    16  	if err != nil {
    17  		return nil, err
    18  	}
    19  	if id != correlationID {
    20  		return nil, Errorf("correlation id mismatch (expected=%d, found=%d)", correlationID, id)
    21  	}
    22  	return res, nil
    23  }
    24  
    25  func hasResponse(msg Message) bool {
    26  	x, _ := msg.(interface{ HasResponse() bool })
    27  	return x == nil || x.HasResponse()
    28  }