Skip to content

Commit

Permalink
fix: encode now-timestamps to avoid overwrites
Browse files Browse the repository at this point in the history
What was happening was that when a timestamp, which are
monotonic-timestamps, included an e.g. ".001" suffix, it would be
considered as "in the future" if the indexer was quick enough to pick it
up, and then be converted to "new Date().getTime()". So if two messages
were written to a channel fast enough, BOTH would get set to "new
Date().getTime()" and the second would overwrite the first in leveldb.

This change ensures that proper monotonic-timestamps are used
throughout.
  • Loading branch information
hackergrrl committed Nov 15, 2019
1 parent 3770334 commit db6f09e
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions views/messages.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var View = require('kappa-view-level')
var timestamp = require('monotonic-timestamp')
var through = require('through2')
var readonly = require('read-only-stream')
var charwise = require('charwise')
Expand All @@ -15,12 +16,11 @@ module.exports = function (lvl) {
if (!msg.value.timestamp) return []

// If the message is from <<THE FUTURE>>, index it at _now_.
var timestamp = msg.value.timestamp
var now = new Date().getTime()
if (timestamp > now) timestamp = now
var ts = msg.value.timestamp
if (isFutureMonotonicTimestamp(ts)) ts = timestamp()

if (msg.value.type.startsWith('chat/') && msg.value.content.channel) {
var key = 'msg!' + msg.value.content.channel + '!' + charwise.encode(timestamp)
var key = 'msg!' + msg.value.content.channel + '!' + charwise.encode(ts)
return [
[key, msg]
]
Expand Down Expand Up @@ -89,3 +89,17 @@ function sanitize (msg) {
if (typeof msg.value.content.text !== 'string') return null
return msg
}

function monotonicTimestampToTimestamp (timestamp) {
if (/^[0-9]+\.[0-9]+$/.test(String(timestamp))) {
return Number(String(timestamp).split('.')[0])
} else {
return timestamp
}
}

function isFutureMonotonicTimestamp () {
var timestamp = monotonicTimestampToTimestamp(timestamp)
var now = new Date().getTime()
return timestamp > now
}

0 comments on commit db6f09e

Please sign in to comment.