packet_encoder.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. // Collections
  15. putBytes(in []byte) error
  16. putVarintBytes(in []byte) error
  17. putRawBytes(in []byte) error
  18. putString(in string) error
  19. putStringArray(in []string) error
  20. putInt32Array(in []int32) error
  21. putInt64Array(in []int64) error
  22. // Provide the current offset to record the batch size metric
  23. offset() int
  24. // Stacks, see PushEncoder
  25. push(in pushEncoder)
  26. pop() error
  27. // To record metrics when provided
  28. metricRegistry() metrics.Registry
  29. }
  30. // PushEncoder is the interface for encoding fields like CRCs and lengths where the value
  31. // of the field depends on what is encoded after it in the packet. Start them with PacketEncoder.Push() where
  32. // the actual value is located in the packet, then PacketEncoder.Pop() them when all the bytes they
  33. // depend upon have been written.
  34. type pushEncoder interface {
  35. // Saves the offset into the input buffer as the location to actually write the calculated value when able.
  36. saveOffset(in int)
  37. // Returns the length of data to reserve for the output of this encoder (eg 4 bytes for a CRC32).
  38. reserveLength() int
  39. // Indicates that all required data is now available to calculate and write the field.
  40. // SaveOffset is guaranteed to have been called first. The implementation should write ReserveLength() bytes
  41. // of data to the saved offset, based on the data between the saved offset and curOffset.
  42. run(curOffset int, buf []byte) error
  43. }