Skip to content
This repository has been archived by the owner on Jun 27, 2023. It is now read-only.

Commit

Permalink
fix: use pubsub seenCache (#75)
Browse files Browse the repository at this point in the history
The inherited PubSubProtocol class already contains a time-cache for
latest seen messages. Use the existing cache instead of creating a
duplicate.
  • Loading branch information
wemeetagain authored and vasco-santos committed Apr 22, 2019
1 parent 36a5696 commit 19d9a96
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 13 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@
"protons": "^1.0.1",
"pull-length-prefixed": "^1.3.2",
"pull-pushable": "^2.2.0",
"pull-stream": "^3.6.9",
"time-cache": "~0.3.0"
"pull-stream": "^3.6.9"
},
"contributors": [
"Alan Shaw <alan.shaw@protocol.ai>",
Expand Down
14 changes: 3 additions & 11 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict'

const TimeCache = require('time-cache')
const pull = require('pull-stream')
const lp = require('pull-length-prefixed')
const assert = require('assert')
Expand All @@ -27,13 +26,6 @@ class FloodSub extends BaseProtocol {
constructor (libp2p) {
super('libp2p:floodsub', multicodec, libp2p)

/**
* Time based cache for sequence numbers.
*
* @type {TimeCache}
*/
this.cache = new TimeCache()

/**
* List of our subscriptions
* @type {Set<string>}
Expand Down Expand Up @@ -109,11 +101,11 @@ class FloodSub extends BaseProtocol {
msgs.forEach((msg) => {
const seqno = utils.msgId(msg.from, msg.seqno)
// 1. check if I've seen the message, if yes, ignore
if (this.cache.has(seqno)) {
if (this.seenCache.has(seqno)) {
return
}

this.cache.put(seqno)
this.seenCache.put(seqno)

// 2. emit to self
this._emitMessages(msg.topicIDs, [msg])
Expand Down Expand Up @@ -182,7 +174,7 @@ class FloodSub extends BaseProtocol {

const buildMessage = (msg) => {
const seqno = utils.randomSeqno()
this.cache.put(utils.msgId(from, seqno))
this.seenCache.put(utils.msgId(from, seqno))

return {
from: from,
Expand Down

0 comments on commit 19d9a96

Please sign in to comment.