Skip to content

Commit 19d9a96

Browse files
wemeetagainvasco-santos
authored andcommitted
fix: use pubsub seenCache (#75)
The inherited PubSubProtocol class already contains a time-cache for latest seen messages. Use the existing cache instead of creating a duplicate.
1 parent 36a5696 commit 19d9a96

File tree

2 files changed

+4
-13
lines changed

2 files changed

+4
-13
lines changed

package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,7 @@
6565
"protons": "^1.0.1",
6666
"pull-length-prefixed": "^1.3.2",
6767
"pull-pushable": "^2.2.0",
68-
"pull-stream": "^3.6.9",
69-
"time-cache": "~0.3.0"
68+
"pull-stream": "^3.6.9"
7069
},
7170
"contributors": [
7271
"Alan Shaw <[email protected]>",

src/index.js

+3-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
'use strict'
22

3-
const TimeCache = require('time-cache')
43
const pull = require('pull-stream')
54
const lp = require('pull-length-prefixed')
65
const assert = require('assert')
@@ -27,13 +26,6 @@ class FloodSub extends BaseProtocol {
2726
constructor (libp2p) {
2827
super('libp2p:floodsub', multicodec, libp2p)
2928

30-
/**
31-
* Time based cache for sequence numbers.
32-
*
33-
* @type {TimeCache}
34-
*/
35-
this.cache = new TimeCache()
36-
3729
/**
3830
* List of our subscriptions
3931
* @type {Set<string>}
@@ -109,11 +101,11 @@ class FloodSub extends BaseProtocol {
109101
msgs.forEach((msg) => {
110102
const seqno = utils.msgId(msg.from, msg.seqno)
111103
// 1. check if I've seen the message, if yes, ignore
112-
if (this.cache.has(seqno)) {
104+
if (this.seenCache.has(seqno)) {
113105
return
114106
}
115107

116-
this.cache.put(seqno)
108+
this.seenCache.put(seqno)
117109

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

183175
const buildMessage = (msg) => {
184176
const seqno = utils.randomSeqno()
185-
this.cache.put(utils.msgId(from, seqno))
177+
this.seenCache.put(utils.msgId(from, seqno))
186178

187179
return {
188180
from: from,

0 commit comments

Comments
 (0)