-
Notifications
You must be signed in to change notification settings - Fork 10
/
peers.js
246 lines (208 loc) · 7.84 KB
/
peers.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
module.exports = Peers
function Peers(name, server, cache, opts = {}){
const { client, hosts, ports, hash, from, retries } = opts
, constants = key(['commands', 'connections', 'connect', 'dht', 'multicast', 'types', 'change', 'partitions', 'outbox'])(opts)
def(this, 'name' , FERO_GEN ? `${FERO_GEN}:${name}` : name)
def(this, 'logical' , name)
def(this, 'me' , server)
def(this, 'cache' , cache)
def(this, 'constants' , constants)
def(this, 'group' , is.str(client) ? client : '')
def(this, 'pool' , debounce(50)(pool))
def(this, 'dht' , new DHT(this, hash))
def(this, 'retries' , new Retries(retries))
def(this, 'connections' , this.constants.connections.max[server ? 'server' : 'client'])
def(this, 'lists' , { disconnected: [], connected: [], connecting: [], throttled: [], client: [], all: [] })
def(this, 'sindex' , 0, 1)
def(this, 'uuids' , 0, 1)
def(this, 'uuid' , uuid())
def(this, 'timeouts' , { })
def(this, 'ready' , true, 1)
def(this, 'destroyed' , false, 1)
def(this, 'discover' , {
scan: require('./discovery/scan')(this, hosts, ports)
, multicast: require('./discovery/multicast')(this)
})
if (from)
define(this, 'from', { value: from })
if (server)
server.raw.on('connection', this.join.bind(this))
cache
// TODO (perf): test if using consts is faster
.on('proxy', this.proxy.bind(this))
.on('init' , init.bind(this))
.on('sync' , sync.bind(this))
.on('done' , done.bind(this))
cache
.on('status', (status, peer) => {
const text = status == 'disconnected' ? status.cyan
: status == 'connecting' ? status.yellow
: status == 'connected' ? status.green
: status == 'removed' ? status.red
: status == 'client' ? status.green
: status
deb(text, peer.id.grey, name, peer.uuid.bgRed)
cache.emit(status, peer)
this.pool()
})
}
Peers.prototype.create = function(host, port, socket, server) {
const id = formatID(`${host}:${port}`)
, peer = id in this ? !deb('duplicate peer', id)
: this.me && id === this.me.id ? !deb('duplicate self', id)
: new Peer(this, host, port, socket, server)
}
Peers.prototype.remove = function(peer) {
return new Promise(async resolve => {
if (is.str(peer))
peer = this[peer]
if (this[peer.id] != peer) // TODO: Confirm if this is still possible
return deb('mismatch - already removed'.red, peer.id, peer.uuid, peer/*.bgRed*/)
for (let timeout in peer.timeouts) {
clearTimeout(peer.timeouts[timeout])
delete peer.timeouts[timeout]
}
if (peer.socket && !peer.socket.destroyed) {
deb('closing socket'.red, peer.uuid.bgRed)
peer.socket.removeAllListeners()
peer.socket.destroy() // TODO: should destroy gracefully
}
peer.setStatus('removed')
remove(peer.id)(this)
resolve() // TODO: should remove gracefully from cluster
})
}
Peers.prototype.multicast = function(buffer, command, audience = 'client', results = []) {
for (let i = 0; i < this.lists[audience].length; i++)
results.push(this.lists[audience][i].send(buffer, command))
return results
}
Peers.prototype.broadcast = function(buffer, command, results = []){
if (buffer === false) return false
this.multicast(buffer, command, 'connected', results)
this.multicast(buffer, command, 'client', results)
return results
}
Peers.prototype.anycast = Peers.prototype.send = function(message) {
if (this.lists.connected.length)
return this.lists.connected[this.sindex = (this.sindex+1) % this.lists.connected.length].send(message, this.constants.commands.proxy)
else
deb('sending with no-one connected', this.name)
}
Peers.prototype.join = function(socket) {
this.create(socket.remoteAddress, socket.remotePort, socket)
}
Peers.prototype.owner = function(change) {
// TODO: mark cached owner by generation
if (change.owner) return change.owner
if (change.type == 'stop') {
if (change.peer.server) {
delete change.value.owner
delete change._buffer
}
return change.owner = (change.value.owner
? [...this].find(d => d.uuid == change.value.owner)
: this.me
)
}
return change.owner = this.dht.lookup(this.cache.partitions.lookup(change))
}
Peers.prototype.proxy = function(message){
const owner = this.owner(message)
if (/*!owner || */owner === this.me) {
if (message.type == 'stop') {
if (!(message.value.tack in message.peer.subscriptions))
return console.error("no subscription", message.value)
message.peer.subscriptions[message.value.tack].source.emit('stop')
delete message.peer.subscriptions[message.value.tack]
return message.reply('stopped')
}
const reply = this.from(message, this.cache)
// TODO: perf this
const finish = reply =>
reply === false ? false
: !is.def(reply) ? message.reply('sack')
: is.stream(reply) ? (message.peer.subscriptions[message.tack] = reply.each(d => message.reply(d))).source.emit('start')
: is.promise(reply) ? reply.then(finish)
: message.reply(reply)
return finish(reply)
} else /*if (owner)*/ {
if (!owner)
return console.error("no owner", message, owner)
message.reply(new Change('subscribe', '', message.owner.uuid))
owner
.send(message.buffer, this.constants.commands.proxy)
.on('reply')
.each(reply => message.reply(reply.buffer))
// if (this.tracer)
// update(`${this.me.port}-${owner.port}-${owner.tacks}.pid`, `${buffer.peer.client ? 'client' : buffer.peer.port}-${this.me.port}-${buffer.tack}`)(this.tracer)
}
}
Peers.prototype.accept = function(change){
const reply = emitterify().on('reply')
, message = {
reply: value => reply.next({ value })
, value: change
, tack: ++this.me.tacks
, owner: this.me
, peer: this.me
}
reply
.on('stop')
.map(d => this.proxy({
type: 'stop'
, owner: this.me
, peer: this.me
, value: { tack: message.tack }
, reply: d => d
}))
Promise
.resolve()
.then(d => this.proxy(message))
return { on: d => reply }
}
Peers.prototype.from = function(change){
// TODO: Should probably reuse this.cache.change, check perf impact
if (!this.cache.partitions.append(change))
return false
// NOTE: This could be .replicate - broadcast and await replication factor before acking
this.broadcast(change.buffer, this.constants.commands.commit)
return change.value && change.value.id || true
}
Peers.prototype.next = function(change){
return (change.peer.server || change.peer.client == 'monitor') && Peers.prototype.from.call(this, change)
}
Peers.prototype[Symbol.iterator] = function*(){
for (id in this)
if (this[id] instanceof Peer)
yield this[id]
}
async function pool() {
while (
this.lists.disconnected.length && (
+ this.lists.connected.length
+ this.lists.connecting.length
+ this.lists.throttled.length
) < this.connections) {
const peer = this
.lists
.disconnected
.shift()
if (peer) {
peer.connect()
await this.cache.on('status').filter(([s, p]) => peer == p && s != 'connecting')
}
}
}
const { def, emitterify, debounce, remove, key, keys, values, is, time } = require('utilise/pure')
, { emit, last, formatID } = require('./utils')
, { init, sync, done } = require('./handshake')
, { Message, Change } = require('./messages')
, Partitions = require('./partitions')
, Retries = require('./retries')
, uuid = () => require('uuid/v4')()
, Peer = require('./peer')
, DHT = require('./dht')
, deb = require('./deb')('per'.bgGreen.bold)
, define = Object.defineProperty
, { FERO_GEN } = process.env