packet_encoder.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package sarama
  2. import "github.com/rcrowley/go-metrics"
  3. // PacketEncoder is the interface providing helpers for writing with Kafka's encoding rules.
  4. // Types implementing Encoder only need to worry about calling methods like PutString,
  5. // not about how a string is represented in Kafka.
  6. type packetEncoder interface {
  7. // Primitives
  8. putInt8(in int8)
  9. putInt16(in int16)
  10. putInt32(in int32)
  11. putInt64(in int64)
  12. putVarint(in int64)
  13. putArrayLength(in int) error
  14. putBool(in bool)
  15. // Collections
  16. putBytes(in []byte) error
  17. putVarintBytes(in []byte) error
  18. putRawBytes(in []byte) error
  19. putString(in string) error
  20. putNullableString(in *string) error
  21. putStringArray(in []string) error
  22. putInt32Array(in []int32) error
  23. putInt64Array(in []int64) error
  24. // Provide the current offset to record the batch size metric
  25. offset() int
  26. // Stacks, see PushEncoder
  27. push(in pushEncoder)
  28. pop() error
  29. // To record metrics when provided
  30. metricRegistry() metrics.Registry
  31. }
  32. // PushEncoder is the interface for encoding fields like CRCs and lengths where the value
  33. // of the field depends on what is encoded after it in the packet. Start them with PacketEncoder.Push() where
  34. // the actual value is located in the packet, then PacketEncoder.Pop() them when all the bytes they
  35. // depend upon have been written.
  36. type pushEncoder interface {
  37. // Saves the offset into the input buffer as the location to actually write the calculated value when able.
  38. saveOffset(in int)
  39. // Returns the length of data to reserve for the output of this encoder (eg 4 bytes for a CRC32).
  40. reserveLength() int
  41. // Indicates that all required data is now available to calculate and write the field.
  42. // SaveOffset is guaranteed to have been called first. The implementation should write ReserveLength() bytes
  43. // of data to the saved offset, based on the data between the saved offset and curOffset.
  44. run(curOffset int, buf []byte) error
  45. }
  46. // dynamicPushEncoder extends the interface of pushEncoder for uses cases where the length of the
  47. // fields itself is unknown until its value was computed (for instance varint encoded length
  48. // fields).
  49. type dynamicPushEncoder interface {
  50. pushEncoder
  51. // Called during pop() to adjust the length of the field.
  52. // It should return the difference in bytes between the last computed length and current length.
  53. adjustLength(currOffset int) int
  54. }