message_set.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package sarama
  2. type MessageBlock struct {
  3. Offset int64
  4. Msg *Message
  5. }
  6. // Messages convenience helper which returns either all the
  7. // messages that are wrapped in this block
  8. func (msb *MessageBlock) Messages() []*MessageBlock {
  9. if msb.Msg.Set != nil {
  10. return msb.Msg.Set.Messages
  11. }
  12. return []*MessageBlock{msb}
  13. }
  14. func (msb *MessageBlock) encode(pe packetEncoder) error {
  15. pe.putInt64(msb.Offset)
  16. pe.push(&lengthField{})
  17. err := msb.Msg.encode(pe)
  18. if err != nil {
  19. return err
  20. }
  21. return pe.pop()
  22. }
  23. func (msb *MessageBlock) decode(pd packetDecoder) (err error) {
  24. if msb.Offset, err = pd.getInt64(); err != nil {
  25. return err
  26. }
  27. lengthDecoder := acquireLengthField()
  28. defer releaseLengthField(lengthDecoder)
  29. if err = pd.push(lengthDecoder); err != nil {
  30. return err
  31. }
  32. msb.Msg = new(Message)
  33. if err = msb.Msg.decode(pd); err != nil {
  34. return err
  35. }
  36. if err = pd.pop(); err != nil {
  37. return err
  38. }
  39. return nil
  40. }
  41. type MessageSet struct {
  42. PartialTrailingMessage bool // whether the set on the wire contained an incomplete trailing MessageBlock
  43. OverflowMessage bool // whether the set on the wire contained an overflow message
  44. Messages []*MessageBlock
  45. }
  46. func (ms *MessageSet) encode(pe packetEncoder) error {
  47. for i := range ms.Messages {
  48. err := ms.Messages[i].encode(pe)
  49. if err != nil {
  50. return err
  51. }
  52. }
  53. return nil
  54. }
  55. func (ms *MessageSet) decode(pd packetDecoder) (err error) {
  56. ms.Messages = nil
  57. for pd.remaining() > 0 {
  58. magic, err := magicValue(pd)
  59. if err != nil {
  60. if err == ErrInsufficientData {
  61. ms.PartialTrailingMessage = true
  62. return nil
  63. }
  64. return err
  65. }
  66. if magic > 1 {
  67. return nil
  68. }
  69. msb := new(MessageBlock)
  70. err = msb.decode(pd)
  71. switch err {
  72. case nil:
  73. ms.Messages = append(ms.Messages, msb)
  74. case ErrInsufficientData:
  75. // As an optimization the server is allowed to return a partial message at the
  76. // end of the message set. Clients should handle this case. So we just ignore such things.
  77. if msb.Offset == -1 {
  78. // This is an overflow message caused by chunked down conversion
  79. ms.OverflowMessage = true
  80. } else {
  81. ms.PartialTrailingMessage = true
  82. }
  83. return nil
  84. default:
  85. return err
  86. }
  87. }
  88. return nil
  89. }
  90. func (ms *MessageSet) addMessage(msg *Message) {
  91. block := new(MessageBlock)
  92. block.Msg = msg
  93. ms.Messages = append(ms.Messages, block)
  94. }