Skip to content

Commit

Permalink
Simpler and testable timestamp() function
Browse files Browse the repository at this point in the history
  • Loading branch information
ycombinator committed Dec 10, 2019
1 parent e4f9fe9 commit f2faf14
Showing 1 changed file with 16 additions and 24 deletions.
40 changes: 16 additions & 24 deletions libbeat/processors/add_id/generator/es_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,36 +78,28 @@ func nextIDData() (uint64, uint32) {
// We only use bottom 3 bytes for the sequence number.
s := sequenceNumber & 0xffffff

t := timestamp()
return t, s
}

// timestamp returns the next timestamp to use, while accounting for system time going
// backwards (e.g. due to a DST change).
func timestamp() uint64 {
now := uint64(time.Now().UnixNano() / 1000)
if lastTimestamp == 0 {
// Last timestamp has not been previously initialized
lastTimestamp = now
return lastTimestamp
}
lastTimestamp = timestamp(now, lastTimestamp)
return lastTimestamp, s
}

// Normally now should be later than lastTimestamp, but if the system time went backwards
// (e.g. due to DST change), we should compute a delta to account for this change, so we always return
// a value that's greater than the last call to this function.
if now < lastTimestamp {
delta = lastTimestamp - now + 1
lastTimestamp = now
return now + delta
// timestamp returns a monotonically-increasing timestamp (in ms) to use,
// while accounting for system time going backwards (e.g. due to a DST change).
func timestamp(now, lastTS uint64) uint64 {
// Last timestamp has not been previously initialized.
if lastTS == 0 {
return now
}

// If the system time was reset, reset delta as well
if now-lastTimestamp >= delta {
delta = 0
// Normally now should be later than lastTimestamp. If that's the case, we can simply
// return now as the new timestamp.
if now > lastTS {
return now
}

lastTimestamp = now
return lastTimestamp
// At this point, we know the system clock has gone backwards. So we increment the
// lastTimestamp by 1 (ms) and return it.
return lastTS + 1
}

func packID(buf []byte, ts uint64, seq uint32) {
Expand Down

0 comments on commit f2faf14

Please sign in to comment.