@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.) hq.recaptime.dev/wiki/Phorge
phorge phabricator
at upstream/main 86 lines 2.2 kB view raw
1'use strict'; 2 3var JX = require('./javelin').JX; 4 5JX.install('AphlictPeerList', { 6 7 construct: function() { 8 this._peers = []; 9 10 // Generate a new unique identify for this server. We just use this to 11 // identify messages we have already seen and figure out which peer is 12 // actually us, so we don't bounce messages around the cluster forever. 13 this._fingerprint = this._generateFingerprint(); 14 }, 15 16 properties: { 17 }, 18 19 members: { 20 _peers: null, 21 _fingerprint: null, 22 23 addPeer: function(peer) { 24 this._peers.push(peer); 25 return this; 26 }, 27 28 addFingerprint: function(message) { 29 var fingerprint = this.getFingerprint(); 30 31 // Check if we've already touched this message. If we have, we do not 32 // broadcast it again. If we haven't, we add our fingerprint and then 33 // broadcast the modified version. 34 var touched = message.touched || []; 35 for (var ii = 0; ii < touched.length; ii++) { 36 if (touched[ii] == fingerprint) { 37 return null; 38 } 39 } 40 touched.push(fingerprint); 41 42 message.touched = touched; 43 return message; 44 }, 45 46 broadcastMessage: function(instance, message) { 47 var ii; 48 49 var touches = {}; 50 var touched = message.touched; 51 for (ii = 0; ii < touched.length; ii++) { 52 touches[touched[ii]] = true; 53 } 54 55 var peers = this._peers; 56 for (ii = 0; ii < peers.length; ii++) { 57 var peer = peers[ii]; 58 59 // If we know the peer's fingerprint and it has already touched 60 // this message, don't broadcast it. 61 var fingerprint = peer.getFingerprint(); 62 if (fingerprint && touches[fingerprint]) { 63 continue; 64 } 65 66 peer.broadcastMessage(instance, message); 67 } 68 }, 69 70 getFingerprint: function() { 71 return this._fingerprint; 72 }, 73 74 _generateFingerprint: function() { 75 var src = '23456789abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ'; 76 var len = 16; 77 var out = []; 78 for (var ii = 0; ii < len; ii++) { 79 var idx = Math.floor(Math.random() * src.length); 80 out.push(src[idx]); 81 } 82 return out.join(''); 83 } 84 } 85 86});