git ssb

0+

alanz / patchwork



forked from Matt McKegg / patchwork

Commit f8a3919796796fb604a4d98decee02f962ee7cee

fix connecting to local peers - you now stay permanently connected

Matt McKegg committed on 11/3/2016, 3:23:31 PM
Parent: 4085b6135d2bff24ac3c2b0818dedc49e03a87dd

Files changed

lib/persistent-gossip/index.jschanged
lib/persistent-gossip/schedule.jschanged
lib/persistent-gossip/init.jsadded
package.jsonchanged
server-process.jschanged
lib/persistent-gossip/index.jsView
@@ -6,11 +6,14 @@
66 var apidoc = require('scuttlebot/lib/apidocs').gossip
77 var u = require('scuttlebot/lib/util')
88 var ref = require('ssb-ref')
99 var ping = require('pull-ping')
10-var Stats = require('statistics')
10+var stats = require('statistics')
1111 var Schedule = require('./schedule')
12-var Init = require('scuttlebot/plugins/gossip/init')
12+var Init = require('./init')
13+var AtomicFile = require('atomic-file')
14+var path = require('path')
15+var deepEqual = require('deep-equal')
1316
1417 function isFunction (f) {
1518 return 'function' === typeof f
1619 }
@@ -18,8 +21,12 @@
1821 function stringify(peer) {
1922 return [peer.host, peer.port, peer.key].join(':')
2023 }
2124
25+function isFriends (friends, a, b) {
26+ return friends[a] && friends[b] && friends[a][b] && friends[b][a]
27+}
28+
2229 /*
2330 Peers : [{
2431 key: id,
2532 host: ip,
@@ -42,8 +49,10 @@
4249 var notify = Notify()
4350 var conf = config.gossip || {}
4451 var home = ref.parseAddress(server.getAddress())
4552
53+ var stateFile = AtomicFile(path.join(config.path, 'gossip.json'))
54+
4655 //Known Peers
4756 var peers = []
4857
4958 function getPeer(id) {
@@ -58,8 +67,18 @@
5867 wakeup: 0,
5968 peers: function () {
6069 return peers
6170 },
71+ connectToFriends: function () {
72+ server.friends.all((err, friends) => {
73+ if (err) return
74+ peers.forEach(function(peer) {
75+ if (!Schedule.isConnect(peer) && isFriends(friends, server.id, peer.key)) {
76+ gossip.connect(peer, function() {})
77+ }
78+ })
79+ })
80+ },
6281 get: function (addr) {
6382 addr = ref.parseAddress(addr)
6483 return u.find(peers, function (a) {
6584 return (
@@ -88,9 +107,9 @@
88107 p.failure = (p.failure || 0) + 1
89108 p.stateChange = Date.now()
90109 notify({ type: 'connect-failure', peer: p })
91110 server.emit('log:info', ['SBOT', p.host+':'+p.port+p.key, 'connection failed', err.message || err])
92- p.duration.value(0)
111+ p.duration = stats(p.duration, 0)
93112 return (cb && cb(err))
94113 }
95114 else {
96115 p.state = 'connected'
@@ -131,12 +150,15 @@
131150 if(!f) {
132151 // new peer
133152 addr.source = source
134153 addr.announcers = 1
135- addr.duration = Stats()
154+ addr.duration = addr.duration || null
136155 peers.push(addr)
137156 notify({ type: 'discover', peer: addr, source: source || 'manual' })
138157 return addr
158+ } else if (source === 'local') {
159+ // this peer is local to us, override old source
160+ addr.source = 'local'
139161 }
140162 //don't count local over and over
141163 else if(f.source != 'local')
142164 f.announcers ++
@@ -196,17 +218,45 @@
196218 //track whether we have successfully connected.
197219 //or how many failures there have been.
198220 var since = peer.stateChange
199221 peer.stateChange = Date.now()
200- if(peer.state === 'connected') //may be "disconnecting"
201- peer.duration.value(peer.stateChange - since)
222+// if(peer.state === 'connected') //may be "disconnecting"
223+ peer.duration = stats(peer.duration, peer.stateChange - since)
224+// console.log(peer.duration)
202225 peer.state = undefined
203226 notify({ type: 'disconnect', peer: peer })
204227 server.emit('log:info', ['SBOT', rpc.id, 'disconnect'])
205228 })
206229
207230 notify({ type: 'connect', peer: peer })
208231 })
209232
233+ var last
234+ stateFile.get(function (err, ary) {
235+ last = ary || []
236+ if(Array.isArray(ary))
237+ ary.forEach(function (v) {
238+ delete v.state
239+ // don't add local peers (wait to rediscover)
240+ if(v.source !== 'local') {
241+ gossip.add(v, 'stored')
242+ }
243+ })
244+ })
245+
246+ var int = setInterval(function () {
247+ var copy = JSON.parse(JSON.stringify(peers))
248+ copy.forEach(function (e) {
249+ delete e.state
250+ })
251+ if(deepEqual(copy, last)) return
252+ last = copy
253+ stateFile.set(copy, function(err) {
254+ if (err) console.log(err)
255+ })
256+ }, 10*1000)
257+
258+ if(int.unref) int.unref()
259+
210260 return gossip
211261 }
212262 }
lib/persistent-gossip/schedule.jsView
@@ -1,17 +1,11 @@
1-var nonPrivate = require('non-private-ip')
21 var ip = require('ip')
32 var onWakeup = require('on-wakeup')
43 var onNetwork = require('on-change-network')
54 var hasNetwork = require('has-network')
65
76 var pull = require('pull-stream')
8-var u = require('scuttlebot/lib/util')
97
10-function rand(array) {
11- return array[~~(Math.random()*array.length)]
12-}
13-
148 function not (fn) {
159 return function (e) { return !fn(e) }
1610 }
1711
@@ -35,11 +29,8 @@
3529 function peerNext(peer, opts) {
3630 return (peer.stateChange|0) + delay(peer.failure|0, opts.factor, opts.max)
3731 }
3832
39-function isFollowing (e) {
40- return e.source === 'self'
41-}
4233
4334 //detect if not connected to wifi or other network
4435 //(i.e. if there is only localhost)
4536
@@ -52,9 +43,9 @@
5243
5344 function isLocal (e) {
5445 // don't rely on private ip address, because
5546 // cjdns creates fake private ip addresses.
56- return ip.isPrivate(e.host) && e.type === 'local'
47+ return ip.isPrivate(e.host) && e.source === 'local'
5748 }
5849
5950 function isUnattempted (e) {
6051 return !e.stateChange
@@ -157,14 +148,13 @@
157148 quota: 3, factor: 2e3, max: 10*min, groupMin: 1e3,
158149 disable: !conf('local', true)
159150 })
160151
161- if(connected < 3) {
152+ if(connected < 3)
162153 connect(peers, ts, 'attempt', exports.isUnattempted, {
163154 min: 0, quota: 1, factor: 0, max: 0, groupMin: 0,
164155 disable: !conf('global', true)
165156 })
166- }
167157
168158 //quota, groupMin, min, factor, max
169159 connect(peers, ts, 'retry', exports.isInactive, {
170160 min: 0,
@@ -179,12 +169,12 @@
179169 disable: !conf('global', true)
180170 })
181171
182172 peers.filter(isConnect).forEach(function (e) {
183- if((!exports.isLongterm(e) || e.state === 'connecting') && e.stateChange + 10e3 < ts) {
173+ var permanent = exports.isLongterm(e) || exports.isLocal(e)
174+ if((!permanent || e.state === 'connecting') && e.stateChange + 10e3 < ts) {
184175 gossip.disconnect(e)
185176 }
186-
187177 })
188178
189179 }, 100*Math.random())
190180
@@ -206,9 +196,8 @@
206196 }
207197
208198 exports.isUnattempted = isUnattempted
209199 exports.isInactive = isInactive
210-exports.isFollowing = isFollowing
211200 exports.isLongterm = isLongterm
212201 exports.isLegacy = isLegacy
213202 exports.isLocal = isLocal
214203 exports.isConnectedOrConnecting = isConnect
lib/persistent-gossip/init.jsView
@@ -1,0 +1,34 @@
1+var isArray = Array.isArray
2+var pull = require('pull-stream')
3+var ref = require('ssb-ref')
4+
5+module.exports = function (gossip, config, server) {
6+
7+ // populate peertable with configured seeds (mainly used in testing)
8+ var seeds = config.seeds
9+
10+ ;(isArray(seeds) ? seeds : [seeds]).filter(Boolean)
11+ .forEach(function (addr) { gossip.add(addr, 'seed') })
12+
13+ // populate peertable with pub announcements on the feed
14+ pull(
15+ server.messagesByType({
16+ type: 'pub', live: true, keys: false
17+ }),
18+ pull.drain(function (msg) {
19+ if(msg.sync) return
20+ if(!msg.content.address) return
21+ if(ref.isAddress(msg.content.address))
22+ gossip.add(msg.content.address, 'pub')
23+ }, gossip.connectToFriends)
24+ )
25+
26+ // populate peertable with announcements on the LAN multicast
27+ server.on('local', function (_peer) {
28+ gossip.add(_peer, 'local')
29+ })
30+
31+}
32+
33+
34+
package.jsonView
@@ -12,9 +12,11 @@
1212 "author": "",
1313 "license": "GPL",
1414 "dependencies": {
1515 "@mmckegg/mutant": "~3.7.1",
16+ "atomic-file": "^0.1.0",
1617 "data-uri-to-buffer": "0.0.4",
18+ "deep-equal": "^1.0.1",
1719 "electron": "~1.4.4",
1820 "electron-default-menu": "~1.0.0",
1921 "has-network": "0.0.0",
2022 "insert-css": "~1.0.0",
@@ -27,9 +29,11 @@
2729 "pull-abortable": "^4.1.0",
2830 "pull-file": "~1.0.0",
2931 "pull-identify-filetype": "^1.1.0",
3032 "pull-next": "0.0.2",
33+ "pull-notify": "^0.1.1",
3134 "pull-pause": "0.0.0",
35+ "pull-ping": "^2.0.2",
3236 "pull-pushable": "^2.0.1",
3337 "pull-stream": "~3.4.5",
3438 "scuttlebot": "~9.2.0",
3539 "sorted-array-functions": "~1.0.0",
@@ -37,7 +41,8 @@
3741 "ssb-keys": "~7.0.0",
3842 "ssb-links": "~2.0.0",
3943 "ssb-query": "~0.1.1",
4044 "ssb-ref": "~2.6.2",
41- "ssb-sort": "^1.0.0"
45+ "ssb-sort": "^1.0.0",
46+ "statistics": "^3.3.0"
4247 }
4348 }
server-process.jsView
@@ -24,24 +24,6 @@
2424 }
2525 ssbConfig.manifest = context.sbot.getManifest()
2626 serveBlobs(context)
2727 fs.writeFileSync(Path.join(ssbConfig.path, 'manifest.json'), JSON.stringify(ssbConfig.manifest))
28- connectToFriendlyPubs(context.sbot)
2928 electron.ipcRenderer.send('server-started', ssbConfig)
3029 }
31-
32-function connectToFriendlyPubs (sbot) {
33- sbot.gossip.peers((err, peers) => {
34- if (err) return console.log(err)
35- sbot.friends.all((err, friends) => {
36- if (err) return console.log(err)
37- console.log('Attempting to connect to friendly pubs...')
38- peers.filter((p) => p.state !== 'connected' && isFriends(friends, sbot.id, p.key)).forEach((peer) => {
39- sbot.gossip.connect(peer, () => {})
40- })
41- })
42- })
43-}
44-
45-function isFriends (friends, a, b) {
46- return friends[a] && friends[b] && friends[a][b] && friends[b][a]
47-}

Built with git-ssb-web