-
Notifications
You must be signed in to change notification settings - Fork 0
/
gossip.js
110 lines (90 loc) · 2.31 KB
/
gossip.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
const EventEmitter = require('events').EventEmitter
const LRU = require('lru-cache')
function shuffle(arr) {
for (let i = 0; i < arr.length; i++) {
const j = Math.floor(Math.random() * arr.length)
const tmp = arr[i]
arr[i] = arr[j]
arr[j] = tmp
}
return arr
}
class Gossip {
constructor({ node }) {
this.node = node
this.emitter = new EventEmitter()
this.protocol = '/gossip/0.1.0'
this.publishLimit = 15
this.seq = Date.now()
this.cache = new LRU({ max: 100000, maxAge: 30 * 60 * 1000 })
}
async initialize() {
this.node.on('message', this.receiveMessage.bind(this))
}
async finalize() {
}
start() {
}
stop() {
}
publish(topic, data) {
const msg = this.buildMessage(topic, data)
const key = this.getMessageKey(msg)
const filteredPeers = this.node.getPeers().filter(p =>
!(p === msg.source || (this.cache.has(key) && this.cache.get(key).has(p)))
)
const peers = shuffle(filteredPeers).slice(0, this.publishLimit)
for (const peer of peers) {
this.node.send(peer, msg)
}
}
forward(msg) {
const key = this.getMessageKey(msg)
const filteredPeers = this.node.getPeers().filter(p =>
!(p === msg.source || (this.cache.has(key) && this.cache.get(key).has(p)))
)
const peers = shuffle(filteredPeers).slice(0, this.publishLimit)
for (const peer of peers) {
this.node.send(peer, msg)
}
}
subscribe(topic, handler) {
this.emitter.on(topic, handler)
}
//********************************************
// Private methods
//********************************************
buildMessage(topic, data) {
const msg = {
protocol: this.protocol,
source: this.node.id,
topic,
data,
seq: this.getSeq()
}
return msg
}
getMessageKey(msg) {
return `${msg.source}:${msg.seq}`
}
getSeq() {
if (this.seq < Number.MAX_SAFE_INTEGER) {
this.seq++
} else {
this.seq = 1
}
return this.seq
}
receiveMessage(msg, peer) {
if (msg.protocol !== this.protocol) return
const key = this.getMessageKey(msg)
if (this.cache.has(key)) {
this.cache.get(key).add(peer)
return
}
this.cache.set(key, new Set([peer]))
const { topic } = msg
this.emitter.emit(topic, msg, peer)
}
}
module.exports = Gossip