Skip to content

Commit

Permalink
Automatically set Time when publishing the messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Pryz committed Feb 23, 2018
1 parent c4aa0f7 commit e2f3c83
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
9 changes: 7 additions & 2 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -762,15 +762,20 @@ func (c *Conn) WriteMessages(msgs ...Message) (int, error) {
return 0, nil
}

// users may believe they can set the Topic and/or Partition
// on the kafka message.
writeTime := time.Now()
for _, msg := range msgs {
// users may believe they can set the Topic and/or Partition
// on the kafka message.
if msg.Topic != "" && msg.Topic != c.topic {
return 0, errInvalidWriteTopic
}
if msg.Partition != 0 {
return 0, errInvalidWritePartition
}

if msg.Time.IsZero() {
msg.Time = writeTime
}
}

n := 0
Expand Down
5 changes: 4 additions & 1 deletion message.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ type Message struct {
Offset int64
Key []byte
Value []byte
Time time.Time

// If not set at the creation, Time will be automatically set when
// writing the message.
Time time.Time
}

func (msg Message) item() messageSetItem {
Expand Down

0 comments on commit e2f3c83

Please sign in to comment.