This repository has been archived by the owner on Aug 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
103 lines (86 loc) · 2.58 KB
/
index.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
const debugRaft = require('diagnostics')('socketio:raftadapter');
const debugRaftRaft = require('diagnostics')('socketio:raftadapter:raft');
const Log = require('liferaft/log');
const LifeRaft = require('liferaft');
const Adapter = require('socket.io-adapter');
const TCPRaft = require('./raft');
module.exports = raftAdapter;
function raftAdapter({ port, peers }) {
return class Raft extends Adapter {
constructor(nsp) {
super(nsp);
const raft = new TCPRaft(port, {
// Log
Log: Log,
path: `./raft-log/${port}`,
// Generic
'election min': 2000,
'election max': 5000,
'heartbeat': 1000
});
raft.on('error', (e) => {
console.error('something went wrong', e);
});
raft.on('heartbeat timeout', () => {
debugRaft('heartbeat timeout, starting election');
});
raft.on('term change', (to, from) => {
debugRaft('were now running on term %s -- was %s', to, from);
});
raft.on('leader change', (to, from) => {
debugRaft('we have a new leader to: %s -- was %s', to, from);
});
raft.on('state change', (to, from) => {
debugRaft('we have a state to: %s -- was %s', to, from);
});
raft.on('commit', (cmd) => {
console.log('new command', cmd);
});
raft.on('follower', () => {
console.log('Became follower');
});
raft.on('leader', () => {
console.log('Became Leader');
});
raft.on('rpc', (packet) => {
try {
console.log('got rpc', packet);
if (!packet || !packet.data || !packet.data.type) {
return;
}
switch (packet.data.type) {
case 'broadcast':
super.broadcast(packet.data.wsPacket, packet.data.opts);
break;
default:
console.error('unhandled msg type', packet.data.type);
}
} catch (e) {
console.error(e);
}
});
raft.on('candidate', () => {
console.log('Became Candidate, No Quorum!');
});
peers.forEach(nr => {
raft.join(nr);
});
this.raft = raft;
}
async broadcast(wsPacket, opts) {
try {
console.log('broadcast', wsPacket);
const payload = {
type: 'broadcast',
wsPacket,
opts,
};
const packet = await this.raft.packet('rpc', payload);
console.log('sending RPC', payload);
await this.raft.message(LifeRaft.CHILD, packet);
} catch(err) {
console.error(err);
}
}
};
}